Browse Source

uset settings get/edit

tags/0.1.2
Robin Thoni 8 years ago
parent
commit
da64b326b1

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

@@ -164,11 +164,10 @@ class LuticateBusiness
164 164
             "get", LuticatePermissions::SETTING_USER_GET);
165 165
         $route->get("$prefix/settings/user/$user_id", "${ns}LuticateSettingsUsersController",
166 166
             "getAllByUser", LuticatePermissions::SETTING_USER_GET);
167
-        $route->post("$prefix/settings/user/$user_id/add/$setting_name", "${ns}LuticateSettingsUsersController",
168
-            "add", array(LuticatePermissions::SETTING_USER_ADD, LuticatePermissions::SETTING_USER_GET));
169
-        $route->post("$prefix/settings/user/$user_id/$setting_name/del", "${ns}LuticateSettingsUsersController",
170
-            "del", array(LuticatePermissions::SETTING_USER_DEL, LuticatePermissions::SETTING_USER_GET));
171 167
         $route->post("$prefix/settings/user/$user_id/$setting_name/edit", "${ns}LuticateSettingsUsersController",
172 168
             "edit", array(LuticatePermissions::SETTING_USER_EDIT, LuticatePermissions::SETTING_USER_GET));
169
+        $route->post("$prefix/settings/user/me/$setting_name/edit", "${ns}LuticateSettingsUsersController",
170
+            "editMe", LuticatePermissions::SETTING_EDIT_ME);
171
+
173 172
     }
174 173
 }

+ 27
- 28
src/Auth/Business/LuticateSettingsUsersBusiness.php View File

@@ -17,7 +17,11 @@ class LuticateSettingsUsersBusiness extends LuBusiness {
17 17
     {
18 18
         LuticateUsersBusiness::checkUserId($user_id);
19 19
         LuticateSettingsBusiness::checkSettingName($setting_name);
20
-        return LuticateSettingsUsersDataAccess::getByName($user_id, $setting_name);
20
+        $setting = LuticateSettingsUsersDataAccess::getByName($user_id, $setting_name);
21
+        if (is_null($setting)) {
22
+            self::notFound("User setting not found");
23
+        }
24
+        return $setting;
21 25
     }
22 26
 
23 27
     public static function getAllByUser($user_id, $page = 0, $perPage = 2000000000)
@@ -25,35 +29,30 @@ class LuticateSettingsUsersBusiness extends LuBusiness {
25 29
         return LuticateSettingsUsersDataAccess::getAllByUser($user_id, $page, $perPage);
26 30
     }
27 31
 
28
-    public static function get($user_id, $setting_name)
29
-    {
30
-        return LuticateSettingsUsersDataAccess::getByName($user_id, $setting_name);
31
-    }
32
-
33
-    public static function add($user_id, $setting_name, $setting_value)
32
+    public static function edit($user_id, $setting_name, $setting_value)
34 33
     {
35
-        $setting = self::checkSettingUserName($user_id, $setting_name);
36
-        if (!is_null($setting)) {
37
-            self::badInput("User setting already exists");
34
+        $setting_value = json_decode($setting_value);
35
+        LuticateUsersBusiness::checkUserId($user_id);
36
+        $setting = LuticateSettingsBusiness::checkSettingName($setting_name);
37
+        if (is_null($setting_value)) {
38
+            LuticateSettingsUsersDataAccess::delete($user_id, $setting_name);
39
+        } else {
40
+            $setting->setValue($setting_value);
41
+            LuticateSettingsBusiness::checkSetting($setting);
42
+            $setting_value = json_encode($setting_value);
43
+            $user_setting = LuticateSettingsUsersDataAccess::getByName($user_id, $setting_name);
44
+            if (is_null($user_setting)) {
45
+                $user_setting = new LuticateSettingsUsersDbo();
46
+                $user_setting->setName($setting_name);
47
+                $user_setting->setValue($setting_value);
48
+                $user_setting->setUserId($user_id);
49
+                LuticateSettingsUsersDataAccess::add($user_setting);
50
+            }
51
+            else {
52
+                $user_setting->setValue($setting_value);
53
+                LuticateSettingsUsersDataAccess::edit($user_setting);
54
+            }
38 55
         }
39
-        $setting = new LuticateSettingsUsersDbo();
40
-        $setting->setName($setting_name);
41
-        $setting->setValue($setting_value);
42
-        $setting->setUserId($user_id);
43
-        LuticateSettingsUsersDataAccess::add($setting);
44
-        return true;
45
-    }
46
-
47
-    public static function del($user_id, $setting_name)
48
-    {
49
-        self::checkSettingUserName($user_id, $setting_name);
50
-        LuticateSettingsUsersDataAccess::delete($user_id, $setting_name);
51 56
         return true;
52 57
     }
53
-
54
-    public static function edit($user_id, $setting_name, $setting_value)
55
-    {
56
-        self::del($user_id, $setting_name);
57
-        return self::add($user_id, $setting_name, $setting_value);
58
-    }
59 58
 }

+ 7
- 29
src/Auth/Controller/LuticateSettingsUsersController.php View File

@@ -33,52 +33,30 @@ class LuticateSettingsUsersController extends LuController {
33 33
      */
34 34
     public function get($user_id, $setting_name)
35 35
     {
36
-        return LuticateSettingsUsersBusiness::get($user_id, $setting_name);
36
+        return LuticateSettingsUsersBusiness::checkSettingUserName($user_id, $setting_name);
37 37
     }
38 38
 
39 39
     /**
40
-     * Add a setting for a user
40
+     * Edit a setting for a user
41 41
      * @param $user_id int The user id
42 42
      * @param $setting_name string The setting name
43 43
      * @param $setting_value bool The setting value
44 44
      * @return bool
45 45
      */
46
-    public function add($user_id, $setting_name, $setting_value)
46
+    public function edit($user_id, $setting_name, $setting_value = null)
47 47
     {
48
-        return LuticateSettingsUsersBusiness::add($user_id, $setting_name, $setting_value);
49
-    }
50
-
51
-    /**
52
-     * Delete a setting for a user
53
-     * @param $user_id int The user id
54
-     * @param $setting_name string The setting name
55
-     * @return bool
56
-     */
57
-    public function del($user_id, $setting_name)
58
-    {
59
-        return LuticateSettingsUsersBusiness::del($user_id, $setting_name);
48
+        return LuticateSettingsUsersBusiness::edit($user_id, $setting_name, $setting_value);
60 49
     }
61 50
 
62 51
     /**
63
-     * Delete a setting for the logged user
52
+     * Edit a setting for the logged user
64 53
      * @param $_user LuticateUsersDbo The logged user
65 54
      * @param $setting_name string The setting name
66
-     * @return bool
67
-     */
68
-    public function reset($_user, $setting_name)
69
-    {
70
-        return self::del($_user->getId(), $setting_name);
71
-    }
72
-
73
-    /**
74
-     * Edit the setting value for a user
75
-     * @param $user_id int The user id
76
-     * @param $setting_name string The setting name
77 55
      * @param $setting_value bool The setting value
78 56
      * @return bool
79 57
      */
80
-    public function edit($user_id, $setting_name, $setting_value)
58
+    public function editMe($_user, $setting_name, $setting_value = null)
81 59
     {
82
-        return LuticateSettingsUsersBusiness::edit($user_id, $setting_name, $setting_value);
60
+        return LuticateSettingsUsersBusiness::edit($_user->getId(), $setting_name, $setting_value);
83 61
     }
84 62
 }

+ 1
- 2
src/Auth/DBO/LuticatePermissions.php View File

@@ -46,7 +46,6 @@ class LuticatePermissions
46 46
     const SETTING_EDIT = "LU_SETTING_EDIT";
47 47
     const SETTING_DEL = "LU_SETTING_DEL";
48 48
     const SETTING_USER_GET = "LU_SETTING_USER_GET";
49
-    const SETTING_USER_ADD = "LU_SETTING_USER_ADD";
50 49
     const SETTING_USER_EDIT = "LU_SETTING_USER_EDIT";
51
-    const SETTING_USER_DEL = "LU_SETTING_USER_DEL";
50
+    const SETTING_EDIT_ME = "LU_SETTING_EDIT_ME";
52 51
 }

+ 4
- 0
src/Auth/DataAccess/LuticateSettingsDataAccess.php View File

@@ -39,6 +39,10 @@ class LuticateSettingsDataAccess extends LuDataAccess {
39 39
         return LuticateSettings::where("name", "=", $setting_name)->first();
40 40
     }
41 41
 
42
+    /**
43
+     * @param $setting_name string
44
+     * @return LuticateSettingsDbo
45
+     */
42 46
     public static function getByName($setting_name)
43 47
     {
44 48
         $perm = self::getModelByName($setting_name);

+ 17
- 0
src/Auth/DataAccess/LuticateSettingsUsersDataAccess.php View File

@@ -23,6 +23,11 @@ class LuticateSettingsUsersDataAccess extends LuDataAccess {
23 23
         return self::getMultiple(array(array('user_id', '=', $user_id)), array(array("name", "ASC")), $page, $perPage);
24 24
     }
25 25
 
26
+    /**
27
+     * @param $user_id int
28
+     * @param $setting_name string
29
+     * @return LuticateSettingsUsersDbo
30
+     */
26 31
     public static function getByName($user_id, $setting_name)
27 32
     {
28 33
         $setting = self::getModelByName($user_id, $setting_name);
@@ -36,4 +41,16 @@ class LuticateSettingsUsersDataAccess extends LuDataAccess {
36 41
     {
37 42
         LuticateUsers::find($user_id)->settings()->detach($setting_name);
38 43
     }
44
+
45
+    /**
46
+     * @param $user_setting LuticateSettingsUsersDbo
47
+     */
48
+    public static function edit($user_setting)
49
+    {
50
+        $user_setting_model = self::getModelByName($user_setting->getUserId(), $user_setting->getName());
51
+        if (is_null($user_setting))
52
+            return;
53
+        $user_setting_model->value = $user_setting->getValue();
54
+        $user_setting_model->save();
55
+    }
39 56
 }

Loading…
Cancel
Save