Group.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @package Grav\Common\User
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common\User;
  9. use Grav\Common\Config\Config;
  10. use Grav\Common\Data\Blueprints;
  11. use Grav\Common\Data\Data;
  12. use Grav\Common\File\CompiledYamlFile;
  13. use Grav\Common\Grav;
  14. use Grav\Common\Utils;
  15. class Group extends Data
  16. {
  17. /**
  18. * Get the groups list
  19. *
  20. * @return array
  21. */
  22. private static function groups()
  23. {
  24. return Grav::instance()['config']->get('groups', []);
  25. }
  26. /**
  27. * Get the groups list
  28. *
  29. * @return array
  30. */
  31. public static function groupNames()
  32. {
  33. $groups = [];
  34. foreach(static::groups() as $groupname => $group) {
  35. $groups[$groupname] = $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. {
  48. return isset(self::groups()[$groupname]);
  49. }
  50. /**
  51. * Get a group by name
  52. *
  53. * @param string $groupname
  54. *
  55. * @return object
  56. */
  57. public static function load($groupname)
  58. {
  59. $groups = self::groups();
  60. $content = $groups[$groupname] ?? [];
  61. $content += ['groupname' => $groupname];
  62. $blueprints = new Blueprints();
  63. $blueprint = $blueprints->get('user/group');
  64. return new Group($content, $blueprint);
  65. }
  66. /**
  67. * Save a group
  68. */
  69. public function save()
  70. {
  71. $grav = Grav::instance();
  72. /** @var Config $config */
  73. $config = $grav['config'];
  74. $blueprints = new Blueprints();
  75. $blueprint = $blueprints->get('user/group');
  76. $config->set("groups.{$this->get('groupname')}", []);
  77. $fields = $blueprint->fields();
  78. foreach ($fields as $field) {
  79. if ($field['type'] === 'text') {
  80. $value = $field['name'];
  81. if (isset($this->items['data'][$value])) {
  82. $config->set("groups.{$this->get('groupname')}.{$value}", $this->items['data'][$value]);
  83. }
  84. }
  85. if ($field['type'] === 'array' || $field['type'] === 'permissions') {
  86. $value = $field['name'];
  87. $arrayValues = Utils::getDotNotation($this->items['data'], $field['name']);
  88. if ($arrayValues) {
  89. foreach ($arrayValues as $arrayIndex => $arrayValue) {
  90. $config->set("groups.{$this->get('groupname')}.{$value}.{$arrayIndex}", $arrayValue);
  91. }
  92. }
  93. }
  94. }
  95. $type = 'groups';
  96. $blueprints = $this->blueprints();
  97. $filename = CompiledYamlFile::instance($grav['locator']->findResource("config://{$type}.yaml"));
  98. $obj = new Data($config->get($type), $blueprints);
  99. $obj->file($filename);
  100. $obj->save();
  101. }
  102. /**
  103. * Remove a group
  104. *
  105. * @param string $groupname
  106. *
  107. * @return bool True if the action was performed
  108. */
  109. public static function remove($groupname)
  110. {
  111. $grav = Grav::instance();
  112. /** @var Config $config */
  113. $config = $grav['config'];
  114. $blueprints = new Blueprints();
  115. $blueprint = $blueprints->get('user/group');
  116. $type = 'groups';
  117. $groups = $config->get($type);
  118. unset($groups[$groupname]);
  119. $config->set($type, $groups);
  120. $filename = CompiledYamlFile::instance($grav['locator']->findResource("config://{$type}.yaml"));
  121. $obj = new Data($groups, $blueprint);
  122. $obj->file($filename);
  123. $obj->save();
  124. return true;
  125. }
  126. }