KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.5KB

  1. <?php
  2. /**
  3. * Class: Group
  4. * The Group model.
  5. *
  6. * See Also:
  7. * <Model>
  8. */
  9. class Group extends Model {
  10. public $has_many = "users";
  11. /**
  12. * Function: __construct
  13. * See Also:
  14. * <Model::grab>
  15. */
  16. public function __construct($group_id = null, $options = array()) {
  17. $options["left_join"][] = array("table" => "permissions",
  18. "where" => "group_id = groups.id");
  19. $options["select"][] = "groups.*";
  20. $options["select"][] = "permissions.id AS permissions";
  21. parent::grab($this, $group_id, $options);
  22. $this->permissions = (array) oneof(@$this->permissions, array());
  23. if ($this->no_results)
  24. return false;
  25. Trigger::current()->filter($this, "group");
  26. }
  27. /**
  28. * Function: find
  29. * See Also:
  30. * <Model::search>
  31. */
  32. static function find($options = array(), $options_for_object = array()) {
  33. $options["left_join"][] = array("table" => "permissions",
  34. "where" => "group_id = groups.id");
  35. $options["select"][] = "groups.*";
  36. $options["select"][] = "permissions.id AS permissions";
  37. return parent::search(get_class(), $options, $options_for_object);
  38. }
  39. /**
  40. * Function: can
  41. * Checks if the group can perform the specified actions.
  42. *
  43. * Parameters:
  44. * *$permissions - However many permissions to check for.
  45. * If the last argument is <true>, it will act as "and", otherwise it will act as "or".
  46. *
  47. * Returns:
  48. * @true@ or @false@
  49. */
  50. public function can() {
  51. if ($this->no_results)
  52. return false;
  53. $actions = func_get_args();
  54. if (end($actions) !== true) {# OR comparison
  55. foreach ($actions as $action)
  56. if (in_array($action, $this->permissions))
  57. return true;
  58. return false;
  59. } else { # AND comparison
  60. array_pop($actions);
  61. foreach ($actions as $action)
  62. if (!in_array($action, $this->permissions))
  63. return false;
  64. return true;
  65. }
  66. }
  67. /**
  68. * Function: add
  69. * Adds a group to the database with the passed Name and Permissions array.
  70. *
  71. * Calls the @add_group@ trigger with the inserted group.
  72. *
  73. * Parameters:
  74. * $name - The group's name
  75. * $permissions - An array of the permissions (IDs).
  76. *
  77. * Returns:
  78. * The newly created <Group>.
  79. *
  80. * See Also:
  81. * <update>
  82. */
  83. static function add($name, $permissions) {
  84. $sql = SQL::current();
  85. $trigger = Trigger::current();
  86. $trigger->filter($name, "before_group_add_name");
  87. $trigger->filter($permissions, "before_group_add_permissions");
  88. $sql->insert("groups", array("name" => $name));
  89. $group_id = $sql->latest("groups");
  90. foreach ($permissions as $id)
  91. $sql->insert("permissions",
  92. array("id" => $id,
  93. "name" => $sql->select("permissions", "name", array("id" => $id))->fetchColumn(),
  94. "group_id" => $group_id));
  95. $group = new self($group_id);
  96. $trigger->call("add_group", $group);
  97. return $group;
  98. }
  99. /**
  100. * Function: update
  101. * Updates a group with the given name and permissions.
  102. *
  103. * Calls the @update_group@ trigger with the updated object and the old object.
  104. *
  105. * Parameters:
  106. * $name - The new Name to set.
  107. * $permissions - An array of the new permissions to set.
  108. */
  109. public function update($name, $permissions) {
  110. if ($this->no_results)
  111. return false;
  112. $sql = SQL::current();
  113. $trigger = Trigger::current();
  114. $trigger->filter($name, "before_group_update_name");
  115. $trigger->filter($permissions, "before_group_update_permissions");
  116. $old = clone $this;
  117. $this->name = $name;
  118. $this->permissions = $permissions;
  119. $sql->update("groups",
  120. array("id" => $this->id),
  121. array("name" => $name));
  122. # Update their permissions
  123. $sql->delete("permissions", array("group_id" => $this->id));
  124. foreach ($permissions as $id) {
  125. $name = $sql->select("permissions",
  126. "name",
  127. array("id" => $id, "group_id" => 0),
  128. null,
  129. array(),
  130. 1)->fetchColumn();
  131. $sql->insert("permissions",
  132. array("id" => $id,
  133. "name" => $name,
  134. "group_id" => $this->id));
  135. }
  136. $trigger->call("update_group", $this, $old);
  137. }
  138. /**
  139. * Function: delete
  140. * Deletes a given group. Calls the @delete_group@ trigger and passes the <Group> as an argument.
  141. *
  142. * Parameters:
  143. * $id - The group to delete.
  144. */
  145. static function delete($id) {
  146. parent::destroy(get_class(), $id);
  147. }
  148. /**
  149. * Function: add_permission
  150. * Adds a permission to the Groups table.
  151. *
  152. * Parameters:
  153. * $id - The ID for the permission, like "can_do_something".
  154. * $name - The name for the permission, like "Can Do Something". Defaults to the camelized ID while keeping spaces.
  155. */
  156. static function add_permission($id, $name = null) {
  157. $sql = SQL::current();
  158. if ($sql->count("permissions", array("id" => $id, "group_id" => 0)))
  159. return; # Permission already exists.
  160. fallback($name, camelize($id, true));
  161. $sql->insert("permissions", array("id" => $id, "name" => $name, "group_id" => 0));
  162. }
  163. /**
  164. * Function: remove_permission
  165. * Removes a permission from the Groups table.
  166. *
  167. * Parameters:
  168. * $id - The ID of the permission to remove.
  169. */
  170. static function remove_permission($id) {
  171. SQL::current()->delete("permissions", array("id" => $id));
  172. }
  173. /**
  174. * Function: size
  175. * Returns the amount of users in the.
  176. */
  177. public function size() {
  178. if ($this->no_results)
  179. return false;
  180. return (isset($this->size)) ? $this->size :
  181. $this->size = SQL::current()->count("users",
  182. array("group_id" => $this->id)) ;
  183. }
  184. /**
  185. * Function: members
  186. * Returns all the members of the group.
  187. *
  188. * !! DEPRECATED AFTER 2.0 !!
  189. */
  190. public function members() {
  191. if ($this->no_results)
  192. return false;
  193. return User::find(array("where" => array("group_id" => $this->id)));
  194. }
  195. }