+ /**
+ * Checks if a user has the given permission for any items in the system.
+ * Can be passed an entity instance to filter on a specific type.
+ * @param string $permission
+ * @param string $entityClass
+ * @return bool
+ */
+ public function checkUserHasPermissionOnAnything(string $permission, string $entityClass = null)
+ {
+ $userRoleIds = $this->currentUser()->roles()->select('id')->pluck('id')->toArray();
+ $userId = $this->currentUser()->id;
+
+ $permissionQuery = $this->db->table('joint_permissions')
+ ->where('action', '=', $permission)
+ ->whereIn('role_id', $userRoleIds)
+ ->where(function ($query) use ($userId) {
+ $query->where('has_permission', '=', 1)
+ ->orWhere(function ($query2) use ($userId) {
+ $query2->where('has_permission_own', '=', 1)
+ ->where('created_by', '=', $userId);
+ });
+ });
+
+ if (!is_null($entityClass)) {
+ $entityInstance = app()->make($entityClass);
+ $permissionQuery = $permissionQuery->where('entity_type', '=', $entityInstance->getMorphClass());
+ }
+
+ $hasPermission = $permissionQuery->count() > 0;
+ $this->clean();
+ return $hasPermission;
+ }
+