]> BookStack Code Mirror - bookstack/blobdiff - app/Entity.php
Merge branch 'custom_role_system'
[bookstack] / app / Entity.php
index 4e85e252e3d15f82cdd4fb7594abcd059bbce22b..6bf29ca0fbb17b5650a653385edfd01211037441 100644 (file)
@@ -1,14 +1,9 @@
-<?php
+<?php namespace BookStack;
 
-namespace BookStack;
 
-use Illuminate\Database\Eloquent\Model;
-
-abstract class Entity extends Model
+abstract class Entity extends Ownable
 {
 
-    use Ownable;
-
     /**
      * Compares this entity to another given entity.
      * Matches by comparing class and id.
@@ -53,7 +48,6 @@ abstract class Entity extends Model
 
     /**
      * Get View objects for this entity.
-     * @return mixed
      */
     public function views()
     {
@@ -61,27 +55,37 @@ abstract class Entity extends Model
     }
 
     /**
-     * Allows checking of the exact class, Used to check entity type.
-     * Cleaner method for is_a.
-     * @param $type
+     * Get this entities restrictions.
+     */
+    public function restrictions()
+    {
+        return $this->morphMany('BookStack\Restriction', 'restrictable');
+    }
+
+    /**
+     * Check if this entity has a specific restriction set against it.
+     * @param $role_id
+     * @param $action
      * @return bool
      */
-    public static function isA($type)
+    public function hasRestriction($role_id, $action)
     {
-        return static::getClassName() === strtolower($type);
+        return $this->restrictions->where('role_id', $role_id)->where('action', $action)->count() > 0;
     }
 
     /**
-     * Gets the class name.
-     * @return string
+     * Allows checking of the exact class, Used to check entity type.
+     * Cleaner method for is_a.
+     * @param $type
+     * @return bool
      */
-    public static function getClassName()
+    public static function isA($type)
     {
-        return strtolower(array_slice(explode('\\', static::class), -1, 1)[0]);
+        return static::getClassName() === strtolower($type);
     }
 
     /**
-     *Gets a limited-length version of the entities name.
+     * Gets a limited-length version of the entities name.
      * @param int $length
      * @return string
      */