core_algorithm.alc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. include "modelling.alh"
  2. include "library.alh"
  3. Element core = ?
  4. Void function main():
  5. // Initialize the Core Formalism
  6. String core_location
  7. String admin_group
  8. String admin_user
  9. core_location = "models/CoreFormalism"
  10. // Create the Model itself and make public
  11. core = instantiate_model(import_node(core_location))
  12. // Create admin group
  13. admin_group = instantiate_node(core, "Group", "")
  14. instantiate_attribute(core, admin_group, "name", "admin")
  15. // Create admin user
  16. admin_user = instantiate_node(core, "User", "")
  17. instantiate_attribute(core, admin_user, "name", get_username())
  18. instantiate_attribute(core, admin_user, "admin", True)
  19. // Create link between admin user and group
  20. instantiate_link(core, "ownedBy", "", admin_group, admin_user)
  21. instantiate_link(core, "belongsTo", "", admin_user, admin_group)
  22. // Add the core formalism already
  23. core_model = instantiate_node(core, "Model", "")
  24. instantiate_attribute(core, core_model, "name", "CoreFormalism")
  25. instantiate_attribute(core, core_model, "location", core_location)
  26. instantiate_attribute(core, core_model, "permissions", "330")
  27. // Make necessary links for the formalism to the owners
  28. instantiate_link(core, "group", "", core_model, admin_group)
  29. instantiate_link(core, "owner", "", core_model, admin_user)
  30. // Switch all new users to the user_function
  31. // This accesses the bootstrap level, so do not change this unless you know what you are doing
  32. Element root
  33. root = read_root()
  34. dict_del(root["__hierarchy"], "__IP")
  35. dict_add(root["__hierarchy"], "__IP", user_function)
  36. // Call this for ourselves as well
  37. user_function_skip_init(admin_user)
  38. // Done, so finish up
  39. // Admin user will have been deleted by the user_function as usual
  40. // Note that if there are no admin users left, it will be very difficult to manage, as nobody will have admin permissions!
  41. return !
  42. Integer function get_relation_to_model(user_id : String, model_id : String):
  43. if (set_in(allAssociationDestinations(core, model_id, "owner"), user_id)):
  44. // We are the owner
  45. return 0!
  46. else:
  47. String group_id
  48. group_id = set_pop(allAssociationDestinations(core, model_id, "group"))
  49. if (set_in(allAssociationDestinations(core, user_id, "belongsTo"), group_id)):
  50. // We are in the owning group
  51. return 1!
  52. else:
  53. // We are not related whatsoever
  54. return 2!
  55. Boolean function allow_read(user_id : String, model_id : String):
  56. if (read_attribute(core, user_id, "admin")):
  57. // Is admin, so always allow
  58. return True!
  59. else:
  60. // Check permissions
  61. String permission
  62. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  63. if (bool_or(permission == "1", permission == "3")):
  64. return True!
  65. else:
  66. return False!
  67. Boolean function allow_write(user_id : String, model_id : String):
  68. if (read_attribute(core, user_id, "admin")):
  69. // Is admin, so always allow
  70. return True!
  71. else:
  72. // Check permissions
  73. String permission
  74. permission = string_get(read_attribute(core, model_id, "permissions"), get_relation_to_model(user_id, model_id))
  75. if (bool_or(permission == "2", permission == "3")):
  76. return True!
  77. else:
  78. return False!
  79. Void function user_function():
  80. // Add user to Core Formalism
  81. String user_id
  82. user_id = instantiate_node(core, "User", "")
  83. instantiate_attribute(core, user_id, "name", get_username())
  84. instantiate_attribute(core, user_id, "admin", False)
  85. // Now call with user created
  86. user_function_skip_init(user_id)
  87. // User destroyed already, so just stop execution
  88. return !
  89. Void function user_function_skip_init(user_id : String)
  90. Boolean do_continue
  91. do_continue = True
  92. while (do_continue):
  93. // TODO model management interface with access control restrictions
  94. // Delete user from Core Formalism
  95. return !