Browse Source

permissions add edit del

tags/0.1.0
Robin Thoni 9 years ago
parent
commit
8017370324

+ 13
- 2
src/Auth/Business/LuticateBusiness.php View File

@@ -88,14 +88,20 @@ class LuticateBusiness
88 88
 
89 89
     /**
90 90
      * @param $route LuRoute
91
+     * @param $prefix string
91 92
      */
92
-    public static function setupRoutes($route)
93
+    public static function setupRoutes($route, $prefix = "/luticate")
93 94
     {
94
-        $prefix = "/luticate";
95 95
         $ns = 'Luticate\Auth\Business\\';
96
+
96 97
         $int = "[0-9]+";
98
+        $name = "[A-Z0-9_]+";
99
+        $bool = "true|false";
100
+
97 101
         $group_id = "{group_id:$int}";
98 102
         $user_id = "{user_id:$int}";
103
+        $permission_name = "{permission_name:$name}";
104
+        $permission_value = "{permission_value:$bool}";
99 105
 
100 106
         $route->post("$prefix/users/login", "${ns}LuticateUsersBusiness", "login");
101 107
         $route->post("$prefix/users/logout", "${ns}LuticateUsersBusiness", "logout");
@@ -112,5 +118,10 @@ class LuticateBusiness
112 118
         $route->post("$prefix/groups/$group_id/edit", "${ns}LuticateGroupsBusiness", "edit", LuticatePermissions::GROUP_EDIT);
113 119
         $route->post("$prefix/groups/$group_id/addUser/$user_id", "${ns}LuticateGroupsBusiness", "addUser", LuticatePermissions::GROUP_ADD_USER);
114 120
         $route->post("$prefix/groups/$group_id/delUser/$user_id", "${ns}LuticateGroupsBusiness", "delUser", LuticatePermissions::GROUP_DEL_USER);
121
+
122
+
123
+        $route->post("$prefix/permissions/add/$permission_name/$permission_value", "${ns}LuticatePermissionsBusiness", "add", LuticatePermissions::PERM_ADD);
124
+        $route->post("$prefix/permissions/$permission_name/del", "${ns}LuticatePermissionsBusiness", "del", LuticatePermissions::PERM_DEL);
125
+        $route->post("$prefix/permissions/$permission_name/edit/$permission_value", "${ns}LuticatePermissionsBusiness", "edit", LuticatePermissions::PERM_EDIT);
115 126
     }
116 127
 }

+ 1
- 1
src/Auth/Business/LuticateGroupsBusiness.php View File

@@ -61,7 +61,7 @@ class LuticateGroupsBusiness extends LuBusiness {
61 61
             return LuticateGroupsDataAccess::addId($group);
62 62
         }
63 63
         self::badInput("Group name already exists");
64
-        return false;
64
+        return -1;
65 65
     }
66 66
 
67 67
     public static function edit($group_id, $group_name)

+ 33
- 2
src/Auth/Business/LuticatePermissionsBusiness.php View File

@@ -24,10 +24,41 @@ class LuticatePermissionsBusiness extends LuBusiness {
24 24
     public static function getUserDynamicPermission($user_id, $permission_name, $action, $resource_id, $default = false)
25 25
     {
26 26
         $dynamic_permission_name = "LU_DYN_" . $permission_name . "_" . $action . "_" . $resource_id;
27
-        $val = LuticatePermissionsDataAccess::getUserPermission($user_id, $dynamic_permission_name, $default);
27
+        $val = LuticatePermissionsDataAccess::getUserPermission($user_id, $dynamic_permission_name);
28 28
         if (is_null($val)) {
29
-            return self::getUserPermission($user_id, $permission_name);
29
+            return self::getUserPermission($user_id, $permission_name, $default);
30 30
         }
31 31
         return $val;
32 32
     }
33
+
34
+    public static function add($permission_name, $permission_value)
35
+    {
36
+        $perm = LuticatePermissionsDataAccess::getByName($permission_name);
37
+        if (is_null($perm)) {
38
+            $perm = new LuticatePermissionsDbo();
39
+            $perm->setName($permission_name);
40
+            $perm->setValue($permission_value);
41
+            LuticatePermissionsDataAccess::add($perm);
42
+            return true;
43
+        }
44
+        self::badInput("Permission name already exists");
45
+        return false;
46
+    }
47
+
48
+    public static function del($permission_name)
49
+    {
50
+        LuticatePermissionsDataAccess::delete($permission_name);
51
+        return true;
52
+    }
53
+
54
+    public static function edit($permission_name, $permission_value)
55
+    {
56
+        $perm = LuticatePermissionsDataAccess::getByName($permission_name);
57
+        if (!is_null($perm)) {
58
+            LuticatePermissionsDataAccess::edit($permission_name, $permission_value);
59
+            return true;
60
+        }
61
+        self::badInput("Permission not found");
62
+        return false;
63
+    }
33 64
 }

+ 10
- 0
src/Auth/DBO/LuticatePermissions.php View File

@@ -23,4 +23,14 @@ class LuticatePermissions
23 23
     const GROUP_DEL = "LU_GROUP_DEL";
24 24
     const GROUP_ADD_USER = "LU_GROUP_ADD_USER";
25 25
     const GROUP_DEL_USER = "LU_GROUP_DEL_USER";
26
+
27
+    const PERM_ADD = "LU_PERM_ADD";
28
+    const PERM_EDIT = "LU_PERM_EDIT";
29
+    const PERM_DEL = "LU_PERM_DEL";
30
+    const PERM_GROUP_ADD = "LU_PERM_GROUP_ADD";
31
+    const PERM_GROUP_EDIT = "LU_PERM_GROUP_EDIT";
32
+    const PERM_GROUP_DEL = "LU_PERM_GROUP_DEL";
33
+    const PERM_USER_ADD = "LU_PERM_USER_ADD";
34
+    const PERM_USER_EDIT = "LU_PERM_USER_EDIT";
35
+    const PERM_USER_DEL = "LU_PERM_USER_DEL";
26 36
 }

+ 31
- 0
src/Auth/DataAccess/LuticatePermissionsDataAccess.php View File

@@ -17,4 +17,35 @@ class LuticatePermissionsDataAccess extends LuDataAccess {
17 17
     {
18 18
         return SpLuGetUserPermission::execute($user_id, $permission_name)->getVal();
19 19
     }
20
+
21
+    public static function getModelByName($permission_name)
22
+    {
23
+        return LuticatePermissions::where("name", "=", $permission_name)->first();
24
+    }
25
+
26
+    public static function getByName($permission_name)
27
+    {
28
+        $perm = self::getModelByName($permission_name);
29
+        if (is_null($perm)) {
30
+            return null;
31
+        }
32
+        return $perm->toDbo();
33
+    }
34
+
35
+    public static function delete($permission_name)
36
+    {
37
+        $perm = self::getModelByName($permission_name);
38
+        if (is_null($perm))
39
+            return;
40
+        $perm->delete();
41
+    }
42
+
43
+    public static function edit($permission_name, $permission_value)
44
+    {
45
+        $perm = self::getModelByName($permission_name);
46
+        if (is_null($perm))
47
+            return;
48
+        $perm->value = $permission_value;
49
+        $perm->save();
50
+    }
20 51
 }

+ 1
- 0
src/Auth/DataAccess/Models/LuticatePermissions.php View File

@@ -6,4 +6,5 @@ use Luticate\Auth\DBO\LuticatePermissionsDbo;
6 6
 
7 7
 class LuticatePermissions extends LuticatePermissionsModel
8 8
 {
9
+    protected $primaryKey = "name";
9 10
 }

+ 1
- 0
src/Auth/DataAccess/Models/LuticatePermissionsGroups.php View File

@@ -6,4 +6,5 @@ use Luticate\Auth\DBO\LuticatePermissionsGroupsDbo;
6 6
 
7 7
 class LuticatePermissionsGroups extends LuticatePermissionsGroupsModel
8 8
 {
9
+    protected $primaryKey = "name";
9 10
 }

+ 1
- 0
src/Auth/DataAccess/Models/LuticatePermissionsUsers.php View File

@@ -6,4 +6,5 @@ use Luticate\Auth\DBO\LuticatePermissionsUsersDbo;
6 6
 
7 7
 class LuticatePermissionsUsers extends LuticatePermissionsUsersModel
8 8
 {
9
+    protected $primaryKey = "name";
9 10
 }

Loading…
Cancel
Save