Group.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @package Grav\Common\User
  4. *
  5. * @copyright Copyright (c) 2015 - 2022 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. /**
  16. * @deprecated 1.7 Use $grav['user_groups'] instead of this class. In type hints, please use UserGroupInterface.
  17. */
  18. class Group extends Data
  19. {
  20. /**
  21. * Get the groups list
  22. *
  23. * @return array
  24. * @deprecated 1.7, use $grav['user_groups'] Flex UserGroupCollection instead
  25. */
  26. protected static function groups()
  27. {
  28. user_error(__METHOD__ . '() is deprecated since Grav 1.7, use $grav[\'user_groups\'] Flex UserGroupCollection instead', E_USER_DEPRECATED);
  29. return Grav::instance()['config']->get('groups', []);
  30. }
  31. /**
  32. * Get the groups list
  33. *
  34. * @return array
  35. * @deprecated 1.7, use $grav['user_groups'] Flex UserGroupCollection instead
  36. */
  37. public static function groupNames()
  38. {
  39. user_error(__METHOD__ . '() is deprecated since Grav 1.7, use $grav[\'user_groups\'] Flex UserGroupCollection instead', E_USER_DEPRECATED);
  40. $groups = [];
  41. foreach (static::groups() as $groupname => $group) {
  42. $groups[$groupname] = $group['readableName'] ?? $groupname;
  43. }
  44. return $groups;
  45. }
  46. /**
  47. * Checks if a group exists
  48. *
  49. * @param string $groupname
  50. * @return bool
  51. * @deprecated 1.7, use $grav['user_groups'] Flex UserGroupCollection instead
  52. */
  53. public static function groupExists($groupname)
  54. {
  55. user_error(__METHOD__ . '() is deprecated since Grav 1.7, use $grav[\'user_groups\'] Flex UserGroupCollection instead', E_USER_DEPRECATED);
  56. return isset(self::groups()[$groupname]);
  57. }
  58. /**
  59. * Get a group by name
  60. *
  61. * @param string $groupname
  62. * @return object
  63. * @deprecated 1.7, use $grav['user_groups'] Flex UserGroupCollection instead
  64. */
  65. public static function load($groupname)
  66. {
  67. user_error(__METHOD__ . '() is deprecated since Grav 1.7, use $grav[\'user_groups\'] Flex UserGroupCollection instead', E_USER_DEPRECATED);
  68. $groups = self::groups();
  69. $content = $groups[$groupname] ?? [];
  70. $content += ['groupname' => $groupname];
  71. $blueprints = new Blueprints();
  72. $blueprint = $blueprints->get('user/group');
  73. return new Group($content, $blueprint);
  74. }
  75. /**
  76. * Save a group
  77. *
  78. * @return void
  79. */
  80. public function save()
  81. {
  82. $grav = Grav::instance();
  83. /** @var Config $config */
  84. $config = $grav['config'];
  85. $blueprints = new Blueprints();
  86. $blueprint = $blueprints->get('user/group');
  87. $config->set("groups.{$this->get('groupname')}", []);
  88. $fields = $blueprint->fields();
  89. foreach ($fields as $field) {
  90. if ($field['type'] === 'text') {
  91. $value = $field['name'];
  92. if (isset($this->items['data'][$value])) {
  93. $config->set("groups.{$this->get('groupname')}.{$value}", $this->items['data'][$value]);
  94. }
  95. }
  96. if ($field['type'] === 'array' || $field['type'] === 'permissions') {
  97. $value = $field['name'];
  98. $arrayValues = Utils::getDotNotation($this->items['data'], $field['name']);
  99. if ($arrayValues) {
  100. foreach ($arrayValues as $arrayIndex => $arrayValue) {
  101. $config->set("groups.{$this->get('groupname')}.{$value}.{$arrayIndex}", $arrayValue);
  102. }
  103. }
  104. }
  105. }
  106. $type = 'groups';
  107. $blueprints = $this->blueprints();
  108. $filename = CompiledYamlFile::instance($grav['locator']->findResource("config://{$type}.yaml"));
  109. $obj = new Data($config->get($type), $blueprints);
  110. $obj->file($filename);
  111. $obj->save();
  112. }
  113. /**
  114. * Remove a group
  115. *
  116. * @param string $groupname
  117. * @return bool True if the action was performed
  118. * @deprecated 1.7, use $grav['user_groups'] Flex UserGroupCollection instead
  119. */
  120. public static function remove($groupname)
  121. {
  122. user_error(__METHOD__ . '() is deprecated since Grav 1.7, use $grav[\'user_groups\'] Flex UserGroupCollection instead', E_USER_DEPRECATED);
  123. $grav = Grav::instance();
  124. /** @var Config $config */
  125. $config = $grav['config'];
  126. $blueprints = new Blueprints();
  127. $blueprint = $blueprints->get('user/group');
  128. $type = 'groups';
  129. $groups = $config->get($type);
  130. unset($groups[$groupname]);
  131. $config->set($type, $groups);
  132. $filename = CompiledYamlFile::instance($grav['locator']->findResource("config://{$type}.yaml"));
  133. $obj = new Data($groups, $blueprint);
  134. $obj->file($filename);
  135. $obj->save();
  136. return true;
  137. }
  138. }