core_algorithm.alc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. include "modelling.alh"
  2. include "library.alh"
  3. include "primitives.alh"
  4. include "constructors.alh"
  5. include "object_operations.alh"
  6. include "mini_modify.alh"
  7. include "model_management.alh"
  8. include "ramify.alh"
  9. Element core = ?
  10. Void function main():
  11. // Initialize the Core Formalism
  12. String core_location
  13. String core_model
  14. String admin_group
  15. String admin_user
  16. core_location = "models/CoreFormalism"
  17. // Create the Model itself and make public
  18. core = instantiate_model(import_node(core_location))
  19. // Create admin group
  20. admin_group = instantiate_node(core, "Group", "")
  21. instantiate_attribute(core, admin_group, "name", "admin")
  22. // Create admin user
  23. admin_user = instantiate_node(core, "User", "")
  24. output("Desired username for admin user?")
  25. instantiate_attribute(core, admin_user, "name", input())
  26. instantiate_attribute(core, admin_user, "admin", True)
  27. // Create link between admin user and group
  28. instantiate_link(core, "ownedBy", "", admin_group, admin_user)
  29. instantiate_link(core, "belongsTo", "", admin_user, admin_group)
  30. // Add the core formalism already
  31. core_model = instantiate_node(core, "Model", "")
  32. instantiate_attribute(core, core_model, "name", "CoreFormalism")
  33. instantiate_attribute(core, core_model, "location", core_location)
  34. instantiate_attribute(core, core_model, "permissions", "220")
  35. // Make necessary links for the formalism to the owners
  36. instantiate_link(core, "group", "", core_model, admin_group)
  37. instantiate_link(core, "owner", "", core_model, admin_user)
  38. // Switch all new users to the user_function
  39. // This accesses the bootstrap level, so do not change this unless you know what you are doing
  40. Element root
  41. root = read_root()
  42. dict_delete(root["__hierarchy"], "__IP")
  43. dict_add(root["__hierarchy"], "__IP", user_function)
  44. // Call this for ourselves as well
  45. user_function_skip_init(admin_user)
  46. // Done, so finish up
  47. // Admin user will have been deleted by the user_function as usual
  48. // Note that if there are no admin users left, it will be very difficult to manage, as nobody will have admin permissions!
  49. return !
  50. Integer function get_relation_to_model(user_id : String, model_id : String):
  51. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  52. // We are the owner
  53. return 0!
  54. else:
  55. String group_id
  56. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  57. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  58. // We are in the owning group
  59. return 1!
  60. else:
  61. // We are not related whatsoever
  62. return 2!
  63. Boolean function is_admin(user_id : String):
  64. if (read_attribute(core, user_id, "admin")):
  65. return True!
  66. else:
  67. return False!
  68. Boolean function allow_read(user_id : String, model_id : String):
  69. if (is_admin(user_id)):
  70. // Is admin, so always allow
  71. return True!
  72. else:
  73. // Check permissions
  74. String permission
  75. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  76. if (bool_or(permission == "1", permission == "2")):
  77. return True!
  78. else:
  79. return False!
  80. Boolean function allow_write(user_id : String, model_id : String):
  81. if (is_admin(user_id)):
  82. // Is admin, so always allow
  83. return True!
  84. else:
  85. // Check permissions
  86. String permission
  87. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  88. if (permission == "2"):
  89. return True!
  90. else:
  91. return False!
  92. Boolean function allow_change_metadata(user_id : String, model_id : String):
  93. if (is_admin(user_id)):
  94. // Is admin, so always allow
  95. return True!
  96. else:
  97. if (get_relation_to_model(user_id, model_id) == 0):
  98. // Only owner can chmod
  99. return True!
  100. else:
  101. return False!
  102. Boolean function allow_group_modify(user_id : String, group_id : String):
  103. if (is_admin(user_id)):
  104. // Is admin, so always allow
  105. return True!
  106. else:
  107. if (read_nr_out(set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))) > 0):
  108. // We are an owner
  109. return True!
  110. else:
  111. return False!
  112. Boolean function check_login(user_id : String):
  113. // TODO
  114. return False!
  115. Element function user_function():
  116. String username
  117. String user_id
  118. output("Log on as which user?")
  119. output("non-existing user will be created")
  120. username = input()
  121. user_id = get_user_id(username)
  122. if (user_id == ""):
  123. // New user
  124. // Add user to Core Formalism
  125. user_id = instantiate_node(core, "User", "")
  126. instantiate_attribute(core, user_id, "name", username)
  127. instantiate_attribute(core, user_id, "admin", False)
  128. // Now call with user created
  129. user_function_skip_init(user_id)
  130. else:
  131. if (check_login(user_id)):
  132. user_function_skip_init(user_id)
  133. // User destroyed already, so just stop execution
  134. return create_node()!
  135. String function get_model_id(name : String):
  136. Element models
  137. String model
  138. models = allInstances(core, "Model")
  139. while (read_nr_out(models) > 0):
  140. model = set_pop(models)
  141. if (value_eq(name, read_attribute(core, model, "name"))):
  142. return model!
  143. return ""!
  144. String function get_user_id(name : String):
  145. Element users
  146. String user
  147. users = allInstances(core, "User")
  148. while (read_nr_out(users) > 0):
  149. user = set_pop(users)
  150. if (value_eq(read_attribute(core, user, "name"), name)):
  151. return user!
  152. return ""!
  153. String function get_group_id(name : String):
  154. Element groups
  155. String group
  156. groups = allInstances(core, "Group")
  157. while (read_nr_out(groups) > 0):
  158. group = set_pop(groups)
  159. if (value_eq(read_attribute(core, group, "name"), name)):
  160. return group!
  161. return ""!
  162. Void function user_function_skip_init(user_id : String):
  163. Boolean do_continue
  164. String cmd
  165. do_continue = True
  166. output("Welcome to the Model Management Interface v2.0!")
  167. output("Use the 'help' command for a list of possible commands")
  168. while (do_continue):
  169. output("Ready for command...")
  170. cmd = input()
  171. if (cmd == "help"):
  172. output("Model operations")
  173. output(" model_add -- Add a new model")
  174. output(" model_modify -- Modify an existing model")
  175. output(" model_delete -- [TODO] Delete a model and all related transformations")
  176. output(" model_list -- List all models")
  177. output(" model_list_full -- List all models with full info")
  178. output("")
  179. output("Transformation-specific operations")
  180. output(" transformation_add_MT_language -- TODO")
  181. output(" transformation_add_MT -- TODO")
  182. output(" transformation_add_AL -- TODO")
  183. output(" transformation_source_add -- TODO")
  184. output(" transformation_source_delete -- TODO")
  185. output(" transformation_target_add -- TODO")
  186. output(" transformation_target_delete -- TODO")
  187. output(" transformation_execute -- TODO")
  188. output("")
  189. output("Model permission operations")
  190. output(" permission_modify -- Change model permissions")
  191. output(" permission_owner -- Change model owner")
  192. output(" permission_group -- Change model group")
  193. output("")
  194. output("Group operations")
  195. output(" group_create -- Create a group")
  196. output(" group_delete -- Delete a group")
  197. output(" group_owner_add -- Add group owner")
  198. output(" group_owner_delete -- Remove group owner")
  199. output(" group_join -- Add someone to your group")
  200. output(" group_kick -- Kick someone from your group")
  201. output(" group_list -- List all groups you are a member of")
  202. output("")
  203. output("Admin operations")
  204. output(" admin_promote -- Promote a user to admin status")
  205. output(" admin_demote -- Demote a user to normal status")
  206. output("")
  207. output("General operations")
  208. output(" account_delete -- Remove current user and revoke all permissions ")
  209. elif (cmd == "model_add"):
  210. // Model addition operation, which uses model upload commands of the compiler
  211. String name
  212. String type
  213. String location
  214. String type_id
  215. Element new_model
  216. String new_model_id
  217. output("Creating new model!")
  218. output("Model type?")
  219. type_id = get_model_id(input())
  220. if (type_id != ""):
  221. // Type exists
  222. if (allow_read(user_id, type_id)):
  223. // And is readable
  224. output("Model name?")
  225. name = input()
  226. if (get_model_id(name) == ""):
  227. // Model doesn't exist yet
  228. output("Waiting for model constructors...")
  229. // TODO Update construct_model call to this interface
  230. // new_model = construct_model(read_attribute(core, type_id, "location"))
  231. output("Model upload success!")
  232. location = "/models/" + cast_id2s(new_model)
  233. export_node(new_model, location)
  234. // Manage meta-info
  235. new_model_id = instantiate_node(core, "Model", "")
  236. instantiate_attribute(core, new_model_id, "name", name)
  237. instantiate_attribute(core, new_model_id, "location", location)
  238. instantiate_attribute(core, new_model_id, "permissions", "200")
  239. instantiate_link(core, "owner", "", new_model_id, user_id)
  240. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  241. output("Meta-info correctly set!")
  242. else:
  243. output("Model with that name already exists!")
  244. else:
  245. output("You are not allowed to read this type model!")
  246. else:
  247. output("Could not find type model!")
  248. elif (cmd == "model_modify"):
  249. // Model modify operation, which uses the mini_modify.alc operations, though with extensions for access control
  250. String model_id
  251. String type_id
  252. output("Which model do you want to modify?")
  253. model_id = get_model_id(input())
  254. if (model_id != ""):
  255. if (allow_read(user_id, model_id)):
  256. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  257. if (allow_read(user_id, type_id)):
  258. modify(import_node(read_attribute(core, model_id, "location")), allow_write(user_id, model_id))
  259. else:
  260. output("You are not allowed to read the type model!")
  261. else:
  262. output("You are not allowed to read this model!")
  263. else:
  264. output("Could not find model!")
  265. elif (cmd == "model_delete"):
  266. // Delete a model and all of its related transformations
  267. String model_id
  268. output("=================================================")
  269. output("WARNING: Deletion is a very destructive operation")
  270. output(" as it also deletes all transformations ")
  271. output(" defined which make use of this model! ")
  272. output("=================================================")
  273. output("")
  274. output("Currently not supported!")
  275. elif (cmd == "model_list"):
  276. // List all models
  277. Element models
  278. String m
  279. models = allInstances(core, "Model")
  280. while (read_nr_out(models) > 0):
  281. m = set_pop(models)
  282. output(string_join((string_join(" ", read_attribute(core, m, "name")) + " : "), read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")))
  283. elif (cmd == "model_list_full"):
  284. // List all models with full info
  285. Element models
  286. String m
  287. String permissions
  288. String owner
  289. String group
  290. String name
  291. String type
  292. String size
  293. models = allInstances(core, "Model")
  294. while (read_nr_out(models) > 0):
  295. m = set_pop(models)
  296. permissions = read_attribute(core, m, "permissions")
  297. owner = read_attribute(core, set_pop(allAssociationDestinations(core, m, "owner")), "name")
  298. group = read_attribute(core, set_pop(allAssociationDestinations(core, m, "group")), "name")
  299. name = read_attribute(core, m, "name")
  300. size = cast_i2s(read_nr_out(dict_read(import_node(read_attribute(core, m, "location")), "model")))
  301. type = read_attribute(core, set_pop(allAssociationDestinations(core, m, "instanceOf")), "name")
  302. output(((((((((((" " + permissions) + " ") + owner) + " ") + group) + " ") + size) + " ") + name) + " : ") + type)
  303. elif (cmd == "transformation_add_MT_language"):
  304. // Create a model transformation language from a set of input and output formalisms
  305. String name
  306. String model_id
  307. Element source
  308. Element target
  309. Element all_formalisms
  310. Element merged_formalism
  311. Element ramified_formalism
  312. String old_type_id
  313. String type_id
  314. String location
  315. String new_model_id
  316. source = create_node()
  317. target = create_node()
  318. old_type_id = ""
  319. // Read source formalisms
  320. output("Source formalisms (terminate with empty string)?")
  321. name = input()
  322. while (name != ""):
  323. model_id = get_model_id(name)
  324. if (model_id != ""):
  325. if (allow_read(user_id, model_id)):
  326. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  327. if (bool_or(old_type_id == "", type_id == old_type_id)):
  328. set_add(source, model_id)
  329. set_add(all_formalisms, create_tuple(name, import_node(read_attribute(core, model_id, "location"))))
  330. old_type_id = type_id
  331. elif (old_type_id != type_id):
  332. // Already have a previous type_id and now another: CLASH
  333. output("Cannot add model as types not compatible with previous models; try again")
  334. else:
  335. output("Model not readable; try again")
  336. name = input()
  337. else:
  338. output("No such model; try again")
  339. // Read target formalisms
  340. output("Target formalisms (terminate with empty string)?")
  341. name = input()
  342. while (name != ""):
  343. model_id = get_model_id(name)
  344. if (model_id != ""):
  345. if (allow_read(user_id, model_id)):
  346. type_id = set_pop(allAssociationDestinations(core, model_id, "instanceOf"))
  347. if (bool_or(old_type_id == "", type_id == old_type_id)):
  348. set_add(target, model_id)
  349. if (bool_not(set_in(source, model_id))):
  350. set_add(all_formalisms, create_tuple(name, import_node(read_attribute(core, model_id, "location"))))
  351. old_type_id = type_id
  352. elif (old_type_id != type_id):
  353. // Already have a previous type_id and now another: CLASH
  354. output("Cannot add model as types not compatible with previous models; try again")
  355. else:
  356. output("Model not readable; try again")
  357. name = input()
  358. else:
  359. output("No such model; try again")
  360. // Merge both into a single metamodel
  361. if (read_nr_out(source) > 0):
  362. if (read_nr_out(target) > 0):
  363. output("Name of the RAMified tranformation metamodel?")
  364. name = input()
  365. if (get_model_id(name) == ""):
  366. // New location is available, so write
  367. merged_formalism = model_fuse(all_formalisms)
  368. location = "/models/" + cast_id2s(merged_formalism)
  369. export_node(merged_formalism, location)
  370. // Manage meta-info
  371. new_model_id = instantiate_node(core, "Model", "")
  372. instantiate_attribute(core, new_model_id, "name", "__merged_" + name)
  373. instantiate_attribute(core, new_model_id, "location", location)
  374. instantiate_attribute(core, new_model_id, "permissions", "200")
  375. instantiate_link(core, "owner", "", new_model_id, user_id)
  376. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  377. // Merge complete, now RAMify!
  378. ramified_formalism = ramify(merged_formalism)
  379. location = "/models/" + cast_id2s(ramified_formalism)
  380. export_node(ramified_formalism, location)
  381. // Manage meta-info
  382. new_model_id = instantiate_node(core, "Model", "")
  383. instantiate_attribute(core, new_model_id, "name", name)
  384. instantiate_attribute(core, new_model_id, "location", location)
  385. instantiate_attribute(core, new_model_id, "permissions", "200")
  386. instantiate_link(core, "owner", "", new_model_id, user_id)
  387. instantiate_link(core, "instanceOf", "", new_model_id, type_id)
  388. else:
  389. output("Model already exists!")
  390. else:
  391. output("At least one target formalism is required")
  392. else:
  393. output("At least one source formalism is required")
  394. elif (cmd == "permission_modify"):
  395. String permissions
  396. Integer permission
  397. String model_id
  398. output("Which model do you want to change permissions of?")
  399. model_id = get_model_id(input())
  400. if (model_id != ""):
  401. if (get_relation_to_model(user_id, model_id) == 0):
  402. output("New permissions?")
  403. permissions = input()
  404. Boolean fail
  405. Integer i
  406. i = 0
  407. if (string_len(permissions) != 3):
  408. fail = True
  409. while (bool_and(bool_not(fail), i < 3)):
  410. permission = cast_s2i(string_get(permissions, i))
  411. if (bool_or(permission < 0, permission > 2)):
  412. fail = True
  413. if (bool_not(fail)):
  414. unset_attribute(core, model_id, "permissions")
  415. instantiate_attribute(core, model_id, "permissions", permissions)
  416. else:
  417. output("Permissions must be a string of three characters with each character being a digit between 0 and 2")
  418. else:
  419. output("Permission denied!")
  420. else:
  421. output("No such model!")
  422. elif (cmd == "permission_owner"):
  423. String permissions
  424. String model_id
  425. String user_id
  426. output("Which model do you want to change owner of?")
  427. model_id = get_model_id(input())
  428. if (model_id != ""):
  429. if (get_relation_to_model(user_id, model_id) == 0):
  430. output("New owner?")
  431. user_id = get_user_id(input())
  432. if (user_id != ""):
  433. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "owner")))
  434. instantiate_link(core, "owner", "", model_id, user_id)
  435. else:
  436. output("No such user!")
  437. else:
  438. output("Permission denied!")
  439. else:
  440. output("No such model!")
  441. elif (cmd == "permission_group"):
  442. String permissions
  443. String model_id
  444. String group_id
  445. output("Which model do you want to change permissions of?")
  446. model_id = get_model_id(input())
  447. if (model_id != ""):
  448. if (get_relation_to_model(user_id, model_id) == 0):
  449. output("New group?")
  450. group_id = get_group_id(input())
  451. if (group_id != ""):
  452. model_delete_element(core, set_pop(allOutgoingAssociationInstances(core, model_id, "group")))
  453. instantiate_link(core, "group", "", model_id, group_id)
  454. else:
  455. output("No such group!")
  456. else:
  457. output("Permission denied!")
  458. else:
  459. output("No such model!")
  460. elif (cmd == "group_create"):
  461. // Create a new group and become its owner
  462. String group_id
  463. String name
  464. output("Which group do you want to create?")
  465. name = input()
  466. group_id = get_group_id(name)
  467. if (group_id == ""):
  468. group_id = instantiate_node(core, "Group", "")
  469. instantiate_attribute(core, group_id, "name", name)
  470. instantiate_link(core, "belongsTo", "", user_id, group_id)
  471. instantiate_link(core, "owner", "", group_id, user_id)
  472. output("Group created!")
  473. else:
  474. output("Group already exists")
  475. elif (cmd == "group_delete"):
  476. // Delete an existing group
  477. String group_id
  478. String name
  479. output("Which group do you want to delete?")
  480. name = input()
  481. group_id = get_group_id(name)
  482. if (group_id != ""):
  483. if (allow_group_modify(user_id, group_id)):
  484. model_delete_element(core, group_id)
  485. else:
  486. output("Permission denied")
  487. else:
  488. output("No such group")
  489. elif (cmd == "group_owner_add"):
  490. // Add an owner to your group
  491. String group_id
  492. String other_user_id
  493. output("Which group do you want to add an owner to?")
  494. group_id = get_group_id(input())
  495. if (group_id != ""):
  496. if (allow_group_modify(user_id, group_id)):
  497. output("Which user do you want to make an owner?")
  498. other_user_id = get_user_id(input())
  499. if (other_user_id != ""):
  500. Element overlap
  501. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  502. if (read_nr_out(overlap) == 0):
  503. instantiate_link(core, "owner", "", group_id, user_id)
  504. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  505. if (read_nr_out(overlap) == 0):
  506. instantiate_link(core, "belongsTo", "", user_id, group_id)
  507. output("New owner added to group!")
  508. else:
  509. output("User is already an owner!")
  510. else:
  511. output("No such user")
  512. else:
  513. output("Permission denied!")
  514. else:
  515. output("No such group")
  516. elif (cmd == "group_owner_delete"):
  517. // Remove an owner from your group
  518. String group_id
  519. String other_user_id
  520. output("Which group do you want to disown someone from?")
  521. group_id = get_group_id(input())
  522. if (group_id != ""):
  523. if (allow_group_modify(user_id, group_id)):
  524. output("Which user do you want to disown?")
  525. other_user_id = get_user_id(input())
  526. if (other_user_id != ""):
  527. Element overlap
  528. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  529. if (read_nr_out(overlap) > 0):
  530. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  531. if (read_nr_out(overlap) > 0):
  532. model_delete_element(core, set_pop(overlap))
  533. output("Disowned group from user!")
  534. else:
  535. output("User is not even an owner of the group!")
  536. else:
  537. output("User is not even a member of the group!")
  538. else:
  539. output("No such user")
  540. else:
  541. output("Permission denied!")
  542. else:
  543. output("No such group")
  544. elif (cmd == "group_join"):
  545. // Add someone to your group
  546. String group_id
  547. String other_user_id
  548. output("Which group do you want to add someone to?")
  549. group_id = get_group_id(input())
  550. if (group_id != ""):
  551. if (allow_group_modify(user_id, group_id)):
  552. output("Which user do you want to add?")
  553. other_user_id = get_user_id(input())
  554. if (other_user_id != ""):
  555. Element overlap
  556. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  557. if (read_nr_out(overlap) == 0):
  558. instantiate_link(core, "belongsTo", "", user_id, group_id)
  559. output("User added to the group!")
  560. else:
  561. output("User is already a member of the group!")
  562. else:
  563. output("No such user")
  564. else:
  565. output("Permission denied!")
  566. else:
  567. output("No such group")
  568. elif (cmd == "group_kick"):
  569. // Remove someone from your group
  570. String group_id
  571. String other_user_id
  572. output("Which group do you want to kick someone from?")
  573. group_id = get_group_id(input())
  574. if (group_id != ""):
  575. if (allow_group_modify(user_id, group_id)):
  576. output("Which user do you want to kick?")
  577. other_user_id = get_user_id(input())
  578. if (other_user_id != ""):
  579. Element overlap
  580. overlap = set_overlap(allOutgoingAssociationInstances(core, user_id, "belongsTo"), allIncomingAssociationInstances(core, group_id, "belongsTo"))
  581. if (read_nr_out(overlap) > 0):
  582. model_delete_element(core, set_pop(overlap))
  583. // Check if user was an owner as well
  584. overlap = set_overlap(allIncomingAssociationInstances(core, user_id, "owner"), allOutgoingAssociationInstances(core, group_id, "owner"))
  585. if (read_nr_out(overlap) > 0):
  586. model_delete_element(core, set_pop(overlap))
  587. output("User kicked!")
  588. else:
  589. output("User is not even a member of the group!")
  590. else:
  591. output("No such user")
  592. else:
  593. output("Permission denied!")
  594. else:
  595. output("No such group")
  596. elif (cmd == "group_list"):
  597. // List all groups you are a member of (and whether you are admin or not!)
  598. Element groups
  599. String group_id
  600. String admin
  601. groups = allAssociationDestinations(core, user_id, "belongsTo")
  602. while (True):
  603. group_id = set_pop(groups)
  604. if (set_in(allOutgoingAssociationInstances(core, group_id, "owner"), user_id)):
  605. admin = " A "
  606. else:
  607. admin = " "
  608. output(string_join(admin, read_attribute(core, group_id, "name")))
  609. elif (cmd == "admin_promote"):
  610. // Promote a user to admin status
  611. if (is_admin(user_id)):
  612. String other_user_id
  613. output("Which user do you want to promote?")
  614. other_user_id = get_user_id(input())
  615. if (other_user_id != ""):
  616. unset_attribute(core, other_user_id, "admin")
  617. instantiate_attribute(core, other_user_id, "admin", True)
  618. output("Permissions granted!")
  619. else:
  620. output("No such user!")
  621. else:
  622. output("Permission denied!")
  623. elif (cmd == "admin_demote"):
  624. // Demote a user to normal status
  625. if (is_admin(user_id)):
  626. String other_user_id
  627. output("Which user do you want to demote?")
  628. other_user_id = get_user_id(input())
  629. if (other_user_id != ""):
  630. unset_attribute(core, other_user_id, "admin")
  631. instantiate_attribute(core, other_user_id, "admin", False)
  632. output("Permissions revoked!")
  633. else:
  634. output("No such user!")
  635. else:
  636. output("Permission denied!")
  637. elif (cmd == "exit"):
  638. // Exit by actually removing the user and decoupling it from all of its models
  639. // Restarting with the same user name will NOT grant you access to anything of the previous user with that same name
  640. do_continue = False
  641. // Delete user from Core Formalism
  642. model_delete_element(core, user_id)
  643. output("Goodbye!")
  644. return !