Browse Source

user add

tags/0.1.0
Robin Thoni 9 years ago
parent
commit
e8a371be89

+ 35
- 1
src/Auth/Business/LuticateUsersBusiness.php View File

@@ -28,13 +28,47 @@ class LuticateUsersBusiness extends LuBusiness {
28 28
         return password_verify($password, $hash);
29 29
     }
30 30
 
31
+    public static function getSalt($length = 10)
32
+    {
33
+        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
34
+        $charactersLength = strlen($characters);
35
+        $randomString = '';
36
+        for ($i = 0; $i < $length; $i++) {
37
+            $randomString .= $characters[rand(0, $charactersLength - 1)];
38
+        }
39
+        return $randomString;
40
+    }
41
+
31 42
     public static function login($username, $password)
32 43
     {
33
-        $user = LuticateUsersDataAccess::getByUsername($username);
44
+        $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
34 45
         if (is_null($user))
35 46
             self::badPassword();
36 47
         if (!self::verifyPassword($password, $user->getPassword()))
37 48
             self::badPassword();
38 49
         return $user;
39 50
     }
51
+
52
+    public static function add($username, $email, $password)
53
+    {
54
+        $hash = self::hashPassword($password);
55
+        if (filter_var($username, FILTER_VALIDATE_EMAIL))
56
+            self::badInput("Username can not be an email");
57
+        if (!filter_var($email, FILTER_VALIDATE_EMAIL))
58
+            self::badInput("Invalid email address");
59
+        $user = LuticateUsersDataAccess::getByUsernameOrEmail($username);
60
+        if (!is_null($user))
61
+            self::badInput("Username already exists");
62
+        $user = LuticateUsersDataAccess::getByUsernameOrEmail($email);
63
+        if (!is_null($user))
64
+            self::badInput("Email already used");
65
+
66
+        $user = new LuticateUsersDbo();
67
+        $user->setEmail($email);
68
+        $user->setPassword($hash);
69
+        $user->setUsername($username);
70
+        $user->setSalt(self::getSalt());
71
+
72
+        return LuticateUsersDataAccess::add($user);
73
+    }
40 74
 }

+ 11
- 2
src/Auth/DataAccess/LuticateUsersDataAccess.php View File

@@ -16,11 +16,20 @@ class LuticateUsersDataAccess extends LuDataAccess {
16 16
      * @param $username string
17 17
      * @return LuticateUsersDbo|null
18 18
      */
19
-    public static function getByUsername($username)
19
+    public static function getByUsernameOrEmail($username)
20 20
     {
21
-        $user = LuticateUsers::where("username", "=", $username)->first();
21
+        $user = LuticateUsers::where("username", "=", $username)->orWhere("email", "=", $username)->first();
22 22
         if (is_null($user))
23 23
             return null;
24 24
         return $user->toDbo();
25 25
     }
26
+
27
+    /**
28
+     * @param LuticateUsersDbo $user
29
+     * @return int
30
+     */
31
+    public static function add($user)
32
+    {
33
+        return self::addId($user);
34
+    }
26 35
 }

Loading…
Cancel
Save