|
@@ -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
|
}
|