Group.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. // Copied from Grav source because it has issues which has been fixed here
  3. namespace AdminAddonUserManager;
  4. use Grav\Common\Data\Blueprints;
  5. use Grav\Common\Data\Data;
  6. use Grav\Common\File\CompiledYamlFile;
  7. use Grav\Common\Grav;
  8. use Grav\Common\Utils;
  9. class Group extends Data {
  10. /**
  11. * Get the groups list
  12. *
  13. * @return array
  14. */
  15. public static function groups() {
  16. $groups = Grav::instance()['config']->get('groups', []);
  17. $blueprints = new Blueprints;
  18. $blueprint = $blueprints->get('user/group');
  19. foreach ($groups as $groupname => &$content) {
  20. if (!isset($content['groupname'])) {
  21. $content['groupname'] = $groupname;
  22. }
  23. $content = new Group($content, $blueprint);
  24. }
  25. return $groups;
  26. }
  27. /**
  28. * Get the groups list
  29. *
  30. * @return array
  31. */
  32. public static function groupNames() {
  33. $groups = [];
  34. foreach(Grav::instance()['config']->get('groups', []) as $groupname => $group) {
  35. $groups[$groupname] = isset($group['readableName']) ? $group['readableName'] : $groupname;
  36. }
  37. return $groups;
  38. }
  39. /**
  40. * Checks if a group exists
  41. *
  42. * @param string $groupname
  43. *
  44. * @return bool
  45. */
  46. public static function groupExists($groupname) {
  47. return isset(self::groups()[$groupname]);
  48. }
  49. /**
  50. * Get a group by name
  51. *
  52. * @param string $groupname
  53. *
  54. * @return object
  55. */
  56. public static function load($groupname) {
  57. if (self::groupExists($groupname)) {
  58. $group = self::groups()[$groupname];
  59. } else {
  60. $blueprints = new Blueprints;
  61. $blueprint = $blueprints->get('user/group');
  62. $content = ['groupname' => $groupname];
  63. $group = new Group($content, $blueprint);
  64. }
  65. return $group;
  66. }
  67. /**
  68. * Save a group
  69. */
  70. public function save() {
  71. $grav = Grav::instance();
  72. $config = $grav['config'];
  73. $blueprints = new Blueprints;
  74. $blueprint = $blueprints->get('user/group');
  75. $fields = $blueprint->fields();
  76. $config->set("groups.$this->groupname", []);
  77. foreach ($fields as $field) {
  78. if ($field['type'] == 'text') {
  79. $value = $field['name'];
  80. if (isset($this->items[$value])) {
  81. $config->set("groups.$this->groupname.$value", $this->items[$value]);
  82. }
  83. }
  84. if ($field['type'] == 'array' || $field['type'] == 'permissions') {
  85. $value = $field['name'];
  86. $arrayValues = Utils::getDotNotation($this->items, $field['name']);
  87. if ($arrayValues) {
  88. foreach ($arrayValues as $arrayIndex => $arrayValue) {
  89. $config->set("groups.$this->groupname.$value.$arrayIndex", $arrayValue);
  90. }
  91. }
  92. }
  93. }
  94. $type = 'groups';
  95. $obj = new Data($config->get($type), $blueprint);
  96. $file = CompiledYamlFile::instance($grav['locator']->findResource('config://') . DS . "{$type}.yaml");
  97. $obj->file($file);
  98. $obj->save();
  99. }
  100. /**
  101. * Remove a group
  102. *
  103. * @param string $groupname
  104. *
  105. * @return bool True if the action was performed
  106. */
  107. public static function remove($groupname) {
  108. $grav = Grav::instance();
  109. $config = $grav['config'];
  110. $blueprints = new Blueprints;
  111. $blueprint = $blueprints->get('user/group');
  112. $groups = $config->get('groups', []);
  113. if (!isset($groups[$groupname])) {
  114. return false;
  115. }
  116. unset($groups[$groupname]);
  117. $config->set('groups', $groups);
  118. $type = 'groups';
  119. $obj = new Data($config->get($type), $blueprint);
  120. $file = CompiledYamlFile::instance($grav['locator']->findResource("config://{$type}.yaml"));
  121. $obj->file($file);
  122. $obj->save();
  123. return true;
  124. }
  125. public function authorize($access) {
  126. if (empty($this->items)) {
  127. return false;
  128. }
  129. if (!isset($this->items['access'])) {
  130. return false;
  131. }
  132. $val = Utils::getDotNotation($this->items['access'], $access);
  133. return Utils::isPositive($val) === true;
  134. }
  135. }