@@ -59,6 +59,8 @@ public function checkOwnableUserAccess(Model $ownable, string $permission): bool
5959 */
6060 protected function hasEntityPermission (Entity $ entity , array $ userRoleIds , string $ action ): ?bool
6161 {
62+ $ this ->ensureValidEntityAction ($ action );
63+
6264 $ adminRoleId = Role::getSystemRole ('admin ' )->id ;
6365 if (in_array ($ adminRoleId , $ userRoleIds )) {
6466 return true ;
@@ -81,7 +83,7 @@ protected function hasEntityPermission(Entity $entity, array $userRoleIds, strin
8183 if ($ currentEntity ->restricted ) {
8284 return $ currentEntity ->permissions ()
8385 ->whereIn ('role_id ' , $ userRoleIds )
84- ->where (' action ' , '= ' , $ action )
86+ ->where ($ action , '= ' , true )
8587 ->count () > 0 ;
8688 }
8789 }
@@ -95,18 +97,16 @@ protected function hasEntityPermission(Entity $entity, array $userRoleIds, strin
9597 */
9698 public function checkUserHasEntityPermissionOnAny (string $ action , string $ entityClass = '' ): bool
9799 {
98- if (strpos ($ action , '- ' ) !== false ) {
99- throw new InvalidArgumentException ('Action should be a simple entity permission action, not a role permission ' );
100- }
100+ $ this ->ensureValidEntityAction ($ action );
101101
102102 $ permissionQuery = EntityPermission::query ()
103- ->where (' action ' , '= ' , $ action )
103+ ->where ($ action , '= ' , true )
104104 ->whereIn ('role_id ' , $ this ->getCurrentUserRoleIds ());
105105
106106 if (!empty ($ entityClass )) {
107107 /** @var Entity $entityInstance */
108108 $ entityInstance = app ()->make ($ entityClass );
109- $ permissionQuery = $ permissionQuery ->where ('restrictable_type ' , '= ' , $ entityInstance ->getMorphClass ());
109+ $ permissionQuery = $ permissionQuery ->where ('entity_type ' , '= ' , $ entityInstance ->getMorphClass ());
110110 }
111111
112112 $ hasPermission = $ permissionQuery ->count () > 0 ;
@@ -255,4 +255,16 @@ protected function getCurrentUserRoleIds(): array
255255
256256 return $ this ->currentUser ()->roles ->pluck ('id ' )->values ()->all ();
257257 }
258+
259+ /**
260+ * Ensure the given action is a valid and expected entity action.
261+ * Throws an exception if invalid otherwise does nothing.
262+ * @throws InvalidArgumentException
263+ */
264+ protected function ensureValidEntityAction (string $ action ): void
265+ {
266+ if (!in_array ($ action , EntityPermission::PERMISSIONS )) {
267+ throw new InvalidArgumentException ('Action should be a simple entity permission action, not a role permission ' );
268+ }
269+ }
258270}
0 commit comments