Passed
Push — master ( 7bc27b...03e22e )
by Peder
01:39
created

UserService::checkLoggedin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Peto16\User;
4
5
class UserService
6
{
7
    private $userStorage;
8
    private $session;
9
10
11
12
    /**
13
     * Constructor for UserService
14
     * @param object            $di dependency injection.
15
     */
16 17
    public function __construct(\Anax\DI\DIFactoryConfig $di)
17
    {
18 17
        $this->userStorage = new UserStorage();
19 17
        $this->userStorage->setDb($di->get("db"));
20 17
        $this->session = $di->get("session");
21 17
    }
22
23
24
25
    /**
26
     * Create user.
27
     *
28
     * @param  object           $user User object to store.
29
     * @return void
30
     */
31 1
    public function createUser(User $user)
32
    {
33 1
        if ($this->userStorage->getUserByField("email", $user->email)) {
34 1
            throw new Exception("E-postadress används redan.");
35
        }
36 1
        if ($this->userStorage->getUserByField("username", $user->username)) {
37 1
            throw new Exception("Användarnamn redan taget.");
38
        }
39 1
        $this->userStorage->createUser($user);
40 1
    }
41
42
43
44
    /**
45
     * Update user.
46
     *
47
     * @param  object           $user User object to update.
48
     * @return void
49
     */
50 2
    public function updateUser($user)
51
    {
52 2
        $this->userStorage->updateUser($user);
53 2
    }
54
55
56
57
    /**
58
     * Delete user. Validates if user is admin to be able to delete
59
     *
60
     * @param  integer          $id user id.
61
     *
62
     * @return boolean
63
     */
64 2
    public function deleteUser($id)
65
    {
66 2
        if ($this->validLoggedInAdmin()) {
67 2
            return $this->userStorage->deleteUser($id);
68
        }
69 1
        return false;
70
    }
71
72
73
74
    /**
75
     * Dynamicly get user by propertie.
76
     *
77
     * @param string            $field field to search by.
78
     *
79
     * @param array             $data to search for.
80
     *
81
     * @return User
82
     *
83
     */
84 10
    public function getUserByField($field, $data)
85
    {
86 10
        $user = new User();
87 10
        $userVarArray = get_object_vars($user);
88 10
        $arrayKeys = array_keys($userVarArray);
89 10
        $userData = $this->userStorage->getUserByField($field, $data);
90 10
        if (empty($userData)) {
91 2
            return $user;
92
        }
93 9
        foreach ($arrayKeys as $key) {
94 9
            $user->{$key} = $userData->$key;
95 9
        }
96 9
        return $user;
97
    }
98
99
100
101
    /**
102
     * Find all users stored.
103
     *
104
     * @return array                Of users
105
     */
106 2
    public function findAllUsers()
107
    {
108 2
        return $this->userStorage->findAllUsers();
109
    }
110
111
112
113
    /**
114
     * Check if user is logged in.
115
     *
116
     * @return boolean
117
     */
118 2
    public function checkLoggedin()
119
    {
120 2
        return $this->session->has("user");
121
    }
122
123
124
125
    /**
126
     * Login user and redirect to admin.
127
     *
128
     * @return boolean
129
     */
130 2
    public function login($username, $password)
131
    {
132 2
        $user = $this->getUserByField("username", $username);
133
134 2
        if ($password === null) {
135 2
            throw new Exception("Empty password field.");
136
        }
137
138 1
        if ($user->id === null) {
139 1
            throw new Exception("Error, not valid credentials.");
140
        }
141
142 1
        if ((int)$user->enabled === 0) {
143 1
            throw new Exception("Error, disabled account.");
144
        }
145
146 1
        if ($this->validatePassword($password, $user->password)) {
147 1
            $this->session->set("user", $user);
148 1
            return true;
149
        }
150 1
        throw new Exception("Error, not valid credentials.");
151
    }
152
153
154
155
    /**
156
     * Check if a user is logged in and returns that user
157
     *
158
     * @return obj          user or null
159
     */
160 6
    public function getCurrentLoggedInUser()
161
    {
162 6
        return $this->session->get("user");
163
    }
164
165
166
167
    /**
168
     * Validate pasword
169
     *
170
     * @method              password_verify Method to verify password
171
     *
172
     * @param  string       $password Password to be validated.
173
     *
174
     * @return boolean      Return true if valid else false.
175
     */
176 1
    private function validatePassword($password, $dbpassword)
177
    {
178 1
        return password_verify($password, $dbpassword);
179
    }
180
181
182
183
    /**
184
     * Check if logged in user is valid and admin.
185
     *
186
     * @return boolean              Returns true or false if user is valid administrator.
187
     */
188 2
    public function validLoggedInAdmin()
189
    {
190 2
        $loggedInUser = $this->getCurrentLoggedInUser();
191
        if ($loggedInUser
192 2
            && $loggedInUser->administrator
193 2
            && $loggedInUser->deleted === null
194 2
            && $loggedInUser->enabled) {
195 2
                return true;
196
        }
197 1
        return false;
198
    }
199
200
201
202
    /**
203
     * Generate gravatar from email or return default avatar.
204
     *
205
     * @param  string           $email email adress
206
     * @return string           Gravatar url.
207
     */
208 1
    public function generateGravatarUrl($email = "")
209
    {
210 1
        if ($email === "") {
211 1
            return "http://www.gravatar.com/avatar/?d=identicon";
212
        }
213 1
        return "https://s.gravatar.com/avatar/" . md5(strtolower(trim($email)));
214
    }
215
}
216