PageRenderTime 59ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bican/roles/src/Bican/Roles/Traits/HasRoleAndPermission.php

https://gitlab.com/puleeno/fiona-fashion
PHP | 397 lines | 179 code | 47 blank | 171 comment | 23 complexity | fd9a3e00f6587787535f0076203ab130 MD5 | raw file
  1. <?php
  2. namespace Bican\Roles\Traits;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Database\Eloquent\Model;
  5. use InvalidArgumentException;
  6. trait HasRoleAndPermission
  7. {
  8. /**
  9. * Property for caching roles.
  10. *
  11. * @var \Illuminate\Database\Eloquent\Collection|null
  12. */
  13. protected $roles;
  14. /**
  15. * Property for caching permissions.
  16. *
  17. * @var \Illuminate\Database\Eloquent\Collection|null
  18. */
  19. protected $permissions;
  20. /**
  21. * User belongs to many roles.
  22. *
  23. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  24. */
  25. public function roles()
  26. {
  27. return $this->belongsToMany(config('roles.models.role'))->withTimestamps();
  28. }
  29. /**
  30. * Get all roles as collection.
  31. *
  32. * @return \Illuminate\Database\Eloquent\Collection
  33. */
  34. public function getRoles()
  35. {
  36. return (!$this->roles) ? $this->roles = $this->roles()->get() : $this->roles;
  37. }
  38. /**
  39. * Check if the user has a role or roles.
  40. *
  41. * @param int|string|array $role
  42. * @param bool $all
  43. * @return bool
  44. */
  45. public function is($role, $all = false)
  46. {
  47. if ($this->isPretendEnabled()) {
  48. return $this->pretend('is');
  49. }
  50. return $this->{$this->getMethodName('is', $all)}($role);
  51. }
  52. /**
  53. * Check if the user has at least one role.
  54. *
  55. * @param int|string|array $role
  56. * @return bool
  57. */
  58. public function isOne($role)
  59. {
  60. foreach ($this->getArrayFrom($role) as $role) {
  61. if ($this->hasRole($role)) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. /**
  68. * Check if the user has all roles.
  69. *
  70. * @param int|string|array $role
  71. * @return bool
  72. */
  73. public function isAll($role)
  74. {
  75. foreach ($this->getArrayFrom($role) as $role) {
  76. if (!$this->hasRole($role)) {
  77. return false;
  78. }
  79. }
  80. return true;
  81. }
  82. /**
  83. * Check if the user has role.
  84. *
  85. * @param int|string $role
  86. * @return bool
  87. */
  88. public function hasRole($role)
  89. {
  90. return $this->getRoles()->contains(function ($key, $value) use ($role) {
  91. return $role == $value->id || Str::is($role, $value->slug);
  92. });
  93. }
  94. /**
  95. * Attach role to a user.
  96. *
  97. * @param int|\Bican\Roles\Models\Role $role
  98. * @return null|bool
  99. */
  100. public function attachRole($role)
  101. {
  102. return (!$this->getRoles()->contains($role)) ? $this->roles()->attach($role) : true;
  103. }
  104. /**
  105. * Detach role from a user.
  106. *
  107. * @param int|\Bican\Roles\Models\Role $role
  108. * @return int
  109. */
  110. public function detachRole($role)
  111. {
  112. $this->roles = null;
  113. return $this->roles()->detach($role);
  114. }
  115. /**
  116. * Detach all roles from a user.
  117. *
  118. * @return int
  119. */
  120. public function detachAllRoles()
  121. {
  122. $this->roles = null;
  123. return $this->roles()->detach();
  124. }
  125. /**
  126. * Get role level of a user.
  127. *
  128. * @return int
  129. */
  130. public function level()
  131. {
  132. return ($role = $this->getRoles()->sortByDesc('level')->first()) ? $role->level : 0;
  133. }
  134. /**
  135. * Get all permissions from roles.
  136. *
  137. * @return \Illuminate\Database\Eloquent\Builder
  138. */
  139. public function rolePermissions()
  140. {
  141. $permissionModel = app(config('roles.models.permission'));
  142. if (!$permissionModel instanceof Model) {
  143. throw new InvalidArgumentException('[roles.models.permission] must be an instance of \Illuminate\Database\Eloquent\Model');
  144. }
  145. return $permissionModel::select(['permissions.*', 'permission_role.created_at as pivot_created_at', 'permission_role.updated_at as pivot_updated_at'])
  146. ->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')->join('roles', 'roles.id', '=', 'permission_role.role_id')
  147. ->whereIn('roles.id', $this->getRoles()->lists('id')->toArray()) ->orWhere('roles.level', '<', $this->level())
  148. ->groupBy(['permissions.id', 'pivot_created_at', 'pivot_updated_at']);
  149. }
  150. /**
  151. * User belongs to many permissions.
  152. *
  153. * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
  154. */
  155. public function userPermissions()
  156. {
  157. return $this->belongsToMany(config('roles.models.permission'))->withTimestamps();
  158. }
  159. /**
  160. * Get all permissions as collection.
  161. *
  162. * @return \Illuminate\Database\Eloquent\Collection
  163. */
  164. public function getPermissions()
  165. {
  166. return (!$this->permissions) ? $this->permissions = $this->rolePermissions()->get()->merge($this->userPermissions()->get()) : $this->permissions;
  167. }
  168. /**
  169. * Check if the user has a permission or permissions.
  170. *
  171. * @param int|string|array $permission
  172. * @param bool $all
  173. * @return bool
  174. */
  175. public function can($permission, $all = false)
  176. {
  177. if ($this->isPretendEnabled()) {
  178. return $this->pretend('can');
  179. }
  180. return $this->{$this->getMethodName('can', $all)}($permission);
  181. }
  182. /**
  183. * Check if the user has at least one permission.
  184. *
  185. * @param int|string|array $permission
  186. * @return bool
  187. */
  188. public function canOne($permission)
  189. {
  190. foreach ($this->getArrayFrom($permission) as $permission) {
  191. if ($this->hasPermission($permission)) {
  192. return true;
  193. }
  194. }
  195. return false;
  196. }
  197. /**
  198. * Check if the user has all permissions.
  199. *
  200. * @param int|string|array $permission
  201. * @return bool
  202. */
  203. public function canAll($permission)
  204. {
  205. foreach ($this->getArrayFrom($permission) as $permission) {
  206. if (!$this->hasPermission($permission)) {
  207. return false;
  208. }
  209. }
  210. return true;
  211. }
  212. /**
  213. * Check if the user has a permission.
  214. *
  215. * @param int|string $permission
  216. * @return bool
  217. */
  218. public function hasPermission($permission)
  219. {
  220. return $this->getPermissions()->contains(function ($key, $value) use ($permission) {
  221. return $permission == $value->id || Str::is($permission, $value->slug);
  222. });
  223. }
  224. /**
  225. * Check if the user is allowed to manipulate with entity.
  226. *
  227. * @param string $providedPermission
  228. * @param \Illuminate\Database\Eloquent\Model $entity
  229. * @param bool $owner
  230. * @param string $ownerColumn
  231. * @return bool
  232. */
  233. public function allowed($providedPermission, Model $entity, $owner = true, $ownerColumn = 'user_id')
  234. {
  235. if ($this->isPretendEnabled()) {
  236. return $this->pretend('allowed');
  237. }
  238. if ($owner === true && $entity->{$ownerColumn} == $this->id) {
  239. return true;
  240. }
  241. return $this->isAllowed($providedPermission, $entity);
  242. }
  243. /**
  244. * Check if the user is allowed to manipulate with provided entity.
  245. *
  246. * @param string $providedPermission
  247. * @param \Illuminate\Database\Eloquent\Model $entity
  248. * @return bool
  249. */
  250. protected function isAllowed($providedPermission, Model $entity)
  251. {
  252. foreach ($this->getPermissions() as $permission) {
  253. if ($permission->model != '' && get_class($entity) == $permission->model
  254. && ($permission->id == $providedPermission || $permission->slug === $providedPermission)
  255. ) {
  256. return true;
  257. }
  258. }
  259. return false;
  260. }
  261. /**
  262. * Attach permission to a user.
  263. *
  264. * @param int|\Bican\Roles\Models\Permission $permission
  265. * @return null|bool
  266. */
  267. public function attachPermission($permission)
  268. {
  269. return (!$this->getPermissions()->contains($permission)) ? $this->userPermissions()->attach($permission) : true;
  270. }
  271. /**
  272. * Detach permission from a user.
  273. *
  274. * @param int|\Bican\Roles\Models\Permission $permission
  275. * @return int
  276. */
  277. public function detachPermission($permission)
  278. {
  279. $this->permissions = null;
  280. return $this->userPermissions()->detach($permission);
  281. }
  282. /**
  283. * Detach all permissions from a user.
  284. *
  285. * @return int
  286. */
  287. public function detachAllPermissions()
  288. {
  289. $this->permissions = null;
  290. return $this->userPermissions()->detach();
  291. }
  292. /**
  293. * Check if pretend option is enabled.
  294. *
  295. * @return bool
  296. */
  297. private function isPretendEnabled()
  298. {
  299. return (bool) config('roles.pretend.enabled');
  300. }
  301. /**
  302. * Allows to pretend or simulate package behavior.
  303. *
  304. * @param string $option
  305. * @return bool
  306. */
  307. private function pretend($option)
  308. {
  309. return (bool) config('roles.pretend.options.' . $option);
  310. }
  311. /**
  312. * Get method name.
  313. *
  314. * @param string $methodName
  315. * @param bool $all
  316. * @return string
  317. */
  318. private function getMethodName($methodName, $all)
  319. {
  320. return ((bool) $all) ? $methodName . 'All' : $methodName . 'One';
  321. }
  322. /**
  323. * Get an array from argument.
  324. *
  325. * @param int|string|array $argument
  326. * @return array
  327. */
  328. private function getArrayFrom($argument)
  329. {
  330. return (!is_array($argument)) ? preg_split('/ ?[,|] ?/', $argument) : $argument;
  331. }
  332. /**
  333. * Handle dynamic method calls.
  334. *
  335. * @param string $method
  336. * @param array $parameters
  337. * @return mixed
  338. */
  339. public function __call($method, $parameters)
  340. {
  341. if (starts_with($method, 'is')) {
  342. return $this->is(snake_case(substr($method, 2), config('roles.separator')));
  343. } elseif (starts_with($method, 'can')) {
  344. return $this->can(snake_case(substr($method, 3), config('roles.separator')));
  345. } elseif (starts_with($method, 'allowed')) {
  346. return $this->allowed(snake_case(substr($method, 7), config('roles.separator')), $parameters[0], (isset($parameters[1])) ? $parameters[1] : true, (isset($parameters[2])) ? $parameters[2] : 'user_id');
  347. }
  348. return parent::__call($method, $parameters);
  349. }
  350. }