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
 
88
 
89
     /**
89
     /**
90
      * @param $route LuRoute
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
         $ns = 'Luticate\Auth\Business\\';
95
         $ns = 'Luticate\Auth\Business\\';
96
+
96
         $int = "[0-9]+";
97
         $int = "[0-9]+";
98
+        $name = "[A-Z0-9_]+";
99
+        $bool = "true|false";
100
+
97
         $group_id = "{group_id:$int}";
101
         $group_id = "{group_id:$int}";
98
         $user_id = "{user_id:$int}";
102
         $user_id = "{user_id:$int}";
103
+        $permission_name = "{permission_name:$name}";
104
+        $permission_value = "{permission_value:$bool}";
99
 
105
 
100
         $route->post("$prefix/users/login", "${ns}LuticateUsersBusiness", "login");
106
         $route->post("$prefix/users/login", "${ns}LuticateUsersBusiness", "login");
101
         $route->post("$prefix/users/logout", "${ns}LuticateUsersBusiness", "logout");
107
         $route->post("$prefix/users/logout", "${ns}LuticateUsersBusiness", "logout");
112
         $route->post("$prefix/groups/$group_id/edit", "${ns}LuticateGroupsBusiness", "edit", LuticatePermissions::GROUP_EDIT);
118
         $route->post("$prefix/groups/$group_id/edit", "${ns}LuticateGroupsBusiness", "edit", LuticatePermissions::GROUP_EDIT);
113
         $route->post("$prefix/groups/$group_id/addUser/$user_id", "${ns}LuticateGroupsBusiness", "addUser", LuticatePermissions::GROUP_ADD_USER);
119
         $route->post("$prefix/groups/$group_id/addUser/$user_id", "${ns}LuticateGroupsBusiness", "addUser", LuticatePermissions::GROUP_ADD_USER);
114
         $route->post("$prefix/groups/$group_id/delUser/$user_id", "${ns}LuticateGroupsBusiness", "delUser", LuticatePermissions::GROUP_DEL_USER);
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
             return LuticateGroupsDataAccess::addId($group);
61
             return LuticateGroupsDataAccess::addId($group);
62
         }
62
         }
63
         self::badInput("Group name already exists");
63
         self::badInput("Group name already exists");
64
-        return false;
64
+        return -1;
65
     }
65
     }
66
 
66
 
67
     public static function edit($group_id, $group_name)
67
     public static function edit($group_id, $group_name)

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

24
     public static function getUserDynamicPermission($user_id, $permission_name, $action, $resource_id, $default = false)
24
     public static function getUserDynamicPermission($user_id, $permission_name, $action, $resource_id, $default = false)
25
     {
25
     {
26
         $dynamic_permission_name = "LU_DYN_" . $permission_name . "_" . $action . "_" . $resource_id;
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
         if (is_null($val)) {
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
         return $val;
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
     const GROUP_DEL = "LU_GROUP_DEL";
23
     const GROUP_DEL = "LU_GROUP_DEL";
24
     const GROUP_ADD_USER = "LU_GROUP_ADD_USER";
24
     const GROUP_ADD_USER = "LU_GROUP_ADD_USER";
25
     const GROUP_DEL_USER = "LU_GROUP_DEL_USER";
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
     {
17
     {
18
         return SpLuGetUserPermission::execute($user_id, $permission_name)->getVal();
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
 
6
 
7
 class LuticatePermissions extends LuticatePermissionsModel
7
 class LuticatePermissions extends LuticatePermissionsModel
8
 {
8
 {
9
+    protected $primaryKey = "name";
9
 }
10
 }

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

6
 
6
 
7
 class LuticatePermissionsGroups extends LuticatePermissionsGroupsModel
7
 class LuticatePermissionsGroups extends LuticatePermissionsGroupsModel
8
 {
8
 {
9
+    protected $primaryKey = "name";
9
 }
10
 }

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

6
 
6
 
7
 class LuticatePermissionsUsers extends LuticatePermissionsUsersModel
7
 class LuticatePermissionsUsers extends LuticatePermissionsUsersModel
8
 {
8
 {
9
+    protected $primaryKey = "name";
9
 }
10
 }

Loading…
Cancel
Save