Browse Source

user edition

tags/0.1.0
Robin Thoni 9 years ago
parent
commit
1837c8d68b

+ 21
- 3
src/Auth/Business/LuticateBusiness.php View File

52
             }
52
             }
53
         }
53
         }
54
         if (is_null($user)) {
54
         if (is_null($user)) {
55
+            if ($token != null && $token != "") {
56
+                return "Invalid token";
57
+            }
55
             $user = LuticateUsersBusiness::getById(0);
58
             $user = LuticateUsersBusiness::getById(0);
56
         }
59
         }
57
 
60
 
61
         $permissions[] = LuticatePermissions::USER_LOGIN;
64
         $permissions[] = LuticatePermissions::USER_LOGIN;
62
         foreach ($permissions as $permission) {
65
         foreach ($permissions as $permission) {
63
             if (!LuticatePermissionsBusiness::getUserPermission(self::$_currentUser->getId(), $permission)) {
66
             if (!LuticatePermissionsBusiness::getUserPermission(self::$_currentUser->getId(), $permission)) {
64
-                return false;
67
+                return "Permission denied";
65
             }
68
             }
66
         }
69
         }
67
-        return true;
70
+        return null;
68
     }
71
     }
69
 
72
 
70
     /**
73
     /**
74
     {
77
     {
75
         $route->setMiddleware(function($permissions, $request)
78
         $route->setMiddleware(function($permissions, $request)
76
         {
79
         {
77
-            return self::authFilter($permissions, $request);
80
+            $auth = self::authFilter($permissions, $request);
81
+            if (is_null($auth)) {
82
+                return true;
83
+            }
84
+            abort(401, $auth);
85
+            return false;
78
         });
86
         });
79
     }
87
     }
80
 
88
 
85
     {
93
     {
86
         $prefix = "/luticate";
94
         $prefix = "/luticate";
87
         $ns = 'Luticate\Auth\Business\\';
95
         $ns = 'Luticate\Auth\Business\\';
96
+        $int = "[0-9]+";
97
+        $group_id = "{group_id:$int}";
98
+        $user_id = "{user_id:$int}";
88
 
99
 
89
         $route->post("$prefix/users/login", "${ns}LuticateUsersBusiness", "login");
100
         $route->post("$prefix/users/login", "${ns}LuticateUsersBusiness", "login");
90
         $route->put("$prefix/users/logout", "${ns}LuticateUsersBusiness", "logout");
101
         $route->put("$prefix/users/logout", "${ns}LuticateUsersBusiness", "logout");
91
         $route->post("$prefix/users/add", "${ns}LuticateUsersBusiness", "add", LuticatePermissions::USER_ADD);
102
         $route->post("$prefix/users/add", "${ns}LuticateUsersBusiness", "add", LuticatePermissions::USER_ADD);
103
+        $route->post("$prefix/users/$user_id/edit", "${ns}LuticateUsersBusiness", "edit", LuticatePermissions::USER_EDIT);
104
+        $route->post("$prefix/users/me/edit", "${ns}LuticateUsersBusiness", "editMe", LuticatePermissions::USER_EDIT_MYSELF);
105
+        $route->post("$prefix/users/$user_id/setPassword", "${ns}LuticateUsersBusiness", "setPassword", LuticatePermissions::USER_SET_PASSWORD);
106
+        $route->post("$prefix/users/me/setPassword", "${ns}LuticateUsersBusiness", "setPasswordMe", LuticatePermissions::USER_SET_PASSWORD_MYSELF);
92
 
107
 
93
 
108
 
94
         $route->post("$prefix/groups/add", "${ns}LuticateGroupsBusiness", "add", LuticatePermissions::GROUP_ADD);
109
         $route->post("$prefix/groups/add", "${ns}LuticateGroupsBusiness", "add", LuticatePermissions::GROUP_ADD);
110
+        $route->post("$prefix/groups/del/$group_id", "${ns}LuticateGroupsBusiness", "del", LuticatePermissions::GROUP_DEL);
111
+        $route->post("$prefix/groups/$group_id/addUser/$user_id", "${ns}LuticateGroupsBusiness", "addUser", LuticatePermissions::GROUP_ADD_USER);
112
+        $route->post("$prefix/groups/$group_id/delUser/$user_id", "${ns}LuticateGroupsBusiness", "delUser", LuticatePermissions::GROUP_DEL_USER);
95
     }
113
     }
96
 }
114
 }

+ 32
- 2
src/Auth/Business/LuticateGroupsBusiness.php View File

12
         return new LuticateGroupsDataAccess();
12
         return new LuticateGroupsDataAccess();
13
     }
13
     }
14
 
14
 
15
+    public static function checkGroupId($group_id)
16
+    {
17
+        if (is_null(self::getById($group_id))) {
18
+            self::notFound("Group not found");
19
+        }
20
+    }
21
+
15
     public static function getByName($group_name)
22
     public static function getByName($group_name)
16
     {
23
     {
17
         return LuticateGroupsDataAccess::getByName($group_name);
24
         return LuticateGroupsDataAccess::getByName($group_name);
18
     }
25
     }
19
 
26
 
20
-    public static function addUserToGroup($user_id, $group_id)
27
+    public static function hasUser($user_id, $group_id)
21
     {
28
     {
22
-        return LuticateGroupsDataAccess::addUserToGroup($user_id, $group_id);
29
+        return LuticateGroupsDataAccess::hasUser($user_id, $group_id);
30
+    }
31
+
32
+    public static function addUser($user_id, $group_id)
33
+    {
34
+        self::checkGroupId($group_id);
35
+        LuticateUsersBusiness::checkUserId($user_id);
36
+        if (self::hasUser($user_id, $group_id)) {
37
+            return true;
38
+        }
39
+        return LuticateGroupsDataAccess::addUser($user_id, $group_id);
40
+    }
41
+
42
+    public static function delUser($user_id, $group_id)
43
+    {
44
+        self::checkGroupId($group_id);
45
+        LuticateUsersBusiness::checkUserId($user_id);
46
+        return LuticateGroupsDataAccess::delUser($user_id, $group_id);
23
     }
47
     }
24
 
48
 
25
     public static function add($group_name)
49
     public static function add($group_name)
32
         }
56
         }
33
         return $group->getId();
57
         return $group->getId();
34
     }
58
     }
59
+
60
+    public static function del($group_id)
61
+    {
62
+        self::checkGroupId($group_id);
63
+
64
+    }
35
 }
65
 }

+ 45
- 0
src/Auth/Business/LuticateUsersBusiness.php View File

60
             ));
60
             ));
61
     }
61
     }
62
 
62
 
63
+    /**
64
+     * @param $user_id
65
+     * @return LuticateUsersDbo|null
66
+     */
67
+    public static function checkUserId($user_id)
68
+    {
69
+        $user = self::getById($user_id);
70
+        if (is_null($user)) {
71
+            self::notFound("User not found");
72
+        }
73
+        return $user;
74
+    }
75
+
63
     public static function login($username, $password)
76
     public static function login($username, $password)
64
     {
77
     {
65
         $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
78
         $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
117
 
130
 
118
         return $user_id;
131
         return $user_id;
119
     }
132
     }
133
+
134
+    public static function edit($user_id, $email)
135
+    {
136
+        if ($user_id != 0) {
137
+            $user = self::checkUserId($user_id);
138
+            $user->setEmail($email);
139
+            LuticateUsersDataAccess::editById($user_id, $user);
140
+        }
141
+        return true;
142
+    }
143
+
144
+    public static function editMe($email, $user)
145
+    {
146
+        return self::edit($user->getId(), $email);
147
+    }
148
+
149
+    public static function setPassword($user_id, $password)
150
+    {
151
+        if ($user_id != 0) {
152
+            $user = self::checkUserId($user_id);
153
+            $user->setPassword(self::hashPassword($password));
154
+            $user->setSalt(self::getSalt());
155
+            LuticateUsersDataAccess::editById($user_id, $user);
156
+        }
157
+        return true;
158
+    }
159
+
160
+    public static function setPasswordMe($password, $oldPassword, $user)
161
+    {
162
+        self::login($user->getUsername(), $oldPassword);
163
+        return self::setPassword($user->getId(), $password);
164
+    }
120
 }
165
 }

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

13
     const USER_LOGIN = "LU_USER_LOGIN";
13
     const USER_LOGIN = "LU_USER_LOGIN";
14
     const USER_ADD = "LU_USER_ADD";
14
     const USER_ADD = "LU_USER_ADD";
15
     const USER_EDIT = "LU_USER_EDIT";
15
     const USER_EDIT = "LU_USER_EDIT";
16
+    const USER_EDIT_MYSELF = "LU_USER_EDIT_MYSELF";
16
     const USER_DEL = "LU_USER_DEL";
17
     const USER_DEL = "LU_USER_DEL";
18
+    const USER_SET_PASSWORD = "LU_USER_SET_PASSWORD";
19
+    const USER_SET_PASSWORD_MYSELF = "LU_USER_SET_PASSWORD_MYSELF";
17
 
20
 
18
     const GROUP_ADD = "LU_GROUP_ADD";
21
     const GROUP_ADD = "LU_GROUP_ADD";
19
     const GROUP_EDIT = "LU_GROUP_EDIT";
22
     const GROUP_EDIT = "LU_GROUP_EDIT";
20
     const GROUP_DEL = "LU_GROUP_DEL";
23
     const GROUP_DEL = "LU_GROUP_DEL";
24
+    const GROUP_ADD_USER = "LU_GROUP_ADD_USER";
25
+    const GROUP_DEL_USER = "LU_GROUP_DEL_USER";
21
 }
26
 }

+ 12
- 1
src/Auth/DataAccess/LuticateGroupsDataAccess.php View File

24
         return $group->toDbo();
24
         return $group->toDbo();
25
     }
25
     }
26
 
26
 
27
-    public static function addUserToGroup($user_id, $group_id)
27
+    public static function addUser($user_id, $group_id)
28
     {
28
     {
29
         LuticateGroups::find($group_id)->users()->attach($user_id);
29
         LuticateGroups::find($group_id)->users()->attach($user_id);
30
         return true;
30
         return true;
31
     }
31
     }
32
+
33
+    public static function delUser($user_id, $group_id)
34
+    {
35
+        LuticateGroups::find($group_id)->users()->detach($user_id);
36
+        return true;
37
+    }
38
+
39
+    public static function hasUser($user_id, $group_id)
40
+    {
41
+        return !is_null(LuticateGroups::find($group_id)->users()->find($user_id));
42
+    }
32
 }
43
 }

Loading…
Cancel
Save