1
0

UserRoleService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace App\Services;
  3. use App\Models\UserRoles;
  4. class UserRoleService
  5. {
  6. public static function can($action, $id, $useDefaultFallback = true)
  7. {
  8. $default = self::defaultRoles();
  9. $roles = self::get($id);
  10. return
  11. in_array($action, array_keys($roles)) ?
  12. $roles[$action] :
  13. (
  14. $useDefaultFallback ?
  15. $default[$action] :
  16. false
  17. );
  18. }
  19. public static function get($id)
  20. {
  21. if($roles = UserRoles::whereUserId($id)->first()) {
  22. return $roles->roles;
  23. }
  24. return self::defaultRoles();
  25. }
  26. public static function roleKeys()
  27. {
  28. return array_keys(self::defaultRoles());
  29. }
  30. public static function defaultRoles()
  31. {
  32. return [
  33. 'account-force-private' => true,
  34. 'account-ignore-follow-requests' => true,
  35. 'can-view-public-feed' => true,
  36. 'can-view-network-feed' => true,
  37. 'can-view-discover' => true,
  38. 'can-view-hashtag-feed' => false,
  39. 'can-post' => true,
  40. 'can-comment' => true,
  41. 'can-like' => true,
  42. 'can-share' => true,
  43. 'can-follow' => false,
  44. 'can-make-public' => false,
  45. 'can-direct-message' => false,
  46. 'can-use-stories' => false,
  47. 'can-view-sensitive' => false,
  48. 'can-bookmark' => false,
  49. 'can-collections' => false,
  50. 'can-federation' => false,
  51. ];
  52. }
  53. public static function getRoles($id)
  54. {
  55. $myRoles = self::get($id);
  56. $roleData = collect(self::roleData())
  57. ->map(function($role, $k) use($myRoles) {
  58. $role['value'] = $myRoles[$k];
  59. return $role;
  60. })
  61. ->toArray();
  62. return $roleData;
  63. }
  64. public static function roleData()
  65. {
  66. return [
  67. 'account-force-private' => [
  68. 'title' => 'Force Private Account',
  69. 'action' => 'Prevent changing account from private'
  70. ],
  71. 'account-ignore-follow-requests' => [
  72. 'title' => 'Ignore Follow Requests',
  73. 'action' => 'Hide follow requests and associated notifications'
  74. ],
  75. 'can-view-public-feed' => [
  76. 'title' => 'Hide Public Feed',
  77. 'action' => 'Hide the public feed timeline'
  78. ],
  79. 'can-view-network-feed' => [
  80. 'title' => 'Hide Network Feed',
  81. 'action' => 'Hide the network feed timeline'
  82. ],
  83. 'can-view-discover' => [
  84. 'title' => 'Hide Discover',
  85. 'action' => 'Hide the discover feature'
  86. ],
  87. 'can-post' => [
  88. 'title' => 'Can post',
  89. 'action' => 'Allows new posts to be shared'
  90. ],
  91. 'can-comment' => [
  92. 'title' => 'Can comment',
  93. 'action' => 'Allows new comments to be posted'
  94. ],
  95. 'can-like' => [
  96. 'title' => 'Can Like',
  97. 'action' => 'Allows the ability to like posts and comments'
  98. ],
  99. 'can-share' => [
  100. 'title' => 'Can Share',
  101. 'action' => 'Allows the ability to share posts and comments'
  102. ],
  103. 'can-follow' => [
  104. 'title' => 'Can Follow',
  105. 'action' => 'Allows the ability to follow accounts'
  106. ],
  107. 'can-make-public' => [
  108. 'title' => 'Can make account public',
  109. 'action' => 'Allows the ability to make account public'
  110. ],
  111. 'can-direct-message' => [
  112. 'title' => '',
  113. 'action' => ''
  114. ],
  115. 'can-use-stories' => [
  116. 'title' => '',
  117. 'action' => ''
  118. ],
  119. 'can-view-sensitive' => [
  120. 'title' => '',
  121. 'action' => ''
  122. ],
  123. 'can-bookmark' => [
  124. 'title' => '',
  125. 'action' => ''
  126. ],
  127. 'can-collections' => [
  128. 'title' => '',
  129. 'action' => ''
  130. ],
  131. 'can-federation' => [
  132. 'title' => '',
  133. 'action' => ''
  134. ],
  135. ];
  136. }
  137. public static function mapInvite($id, $data = [])
  138. {
  139. $roles = self::get($id);
  140. $map = [
  141. 'account-force-private' => 'private',
  142. 'account-ignore-follow-requests' => 'private',
  143. 'can-view-public-feed' => 'discovery_feeds',
  144. 'can-view-network-feed' => 'discovery_feeds',
  145. 'can-view-discover' => 'discovery_feeds',
  146. 'can-view-hashtag-feed' => 'discovery_feeds',
  147. 'can-post' => 'post',
  148. 'can-comment' => 'comment',
  149. 'can-like' => 'like',
  150. 'can-share' => 'share',
  151. 'can-follow' => 'follow',
  152. 'can-make-public' => '!private',
  153. 'can-direct-message' => 'dms',
  154. 'can-use-stories' => 'story',
  155. 'can-view-sensitive' => '!hide_cw',
  156. 'can-bookmark' => 'bookmark',
  157. 'can-collections' => 'collection',
  158. 'can-federation' => 'federation',
  159. ];
  160. foreach ($map as $key => $value) {
  161. if(!isset($data[$value]) && !isset($data[substr($value, 1)])) {
  162. $map[$key] = false;
  163. continue;
  164. }
  165. $map[$key] = str_starts_with($value, '!') ? !$data[substr($value, 1)] : $data[$value];
  166. }
  167. return $map;
  168. }
  169. public static function mapActions($id, $data = [])
  170. {
  171. $res = [];
  172. $map = [
  173. 'account-force-private' => 'private',
  174. 'account-ignore-follow-requests' => 'private',
  175. 'can-view-public-feed' => 'discovery_feeds',
  176. 'can-view-network-feed' => 'discovery_feeds',
  177. 'can-view-discover' => 'discovery_feeds',
  178. 'can-view-hashtag-feed' => 'discovery_feeds',
  179. 'can-post' => 'post',
  180. 'can-comment' => 'comment',
  181. 'can-like' => 'like',
  182. 'can-share' => 'share',
  183. 'can-follow' => 'follow',
  184. 'can-make-public' => '!private',
  185. 'can-direct-message' => 'dms',
  186. 'can-use-stories' => 'story',
  187. 'can-view-sensitive' => '!hide_cw',
  188. 'can-bookmark' => 'bookmark',
  189. 'can-collections' => 'collection',
  190. 'can-federation' => 'federation',
  191. ];
  192. foreach ($map as $key => $value) {
  193. if(!isset($data[$value]) && !isset($data[substr($value, 1)])) {
  194. $res[$key] = false;
  195. continue;
  196. }
  197. $res[$key] = str_starts_with($value, '!') ? !$data[substr($value, 1)] : $data[$value];
  198. }
  199. return $res;
  200. }
  201. }