Completed
Branch master (99b5bb)
by WILMER
02:19
created

User   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 3
Dependencies 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 3
cbo 1
dl 0
loc 102
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A findIdentity() 0 4 2
A findIdentityByAccessToken() 0 10 3
A findByUsername() 0 10 3
A getId() 0 4 1
A getAuthKey() 0 4 1
A validateAuthKey() 0 4 1
A validatePassword() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the Cjt Terabyte LLC [yii2-extension].
5
 *
6
 * (c) Cjt Terabyte LLC [yii2-extension] <http://github.com/cjtterabytesoft>.
7
 * For the full copyright and license information, please view the LICENSE.md.
8
 * file that was distributed with this source code.
9
 *
10
 * @link http://www.cjtterabyte.com.
11
 * @author Wilmer Arámbula <[email protected]>.
12
 * @copyright (c) 2015 Cjt Terabyte LLC.
13
 * @Extension: [yii2-adminlte-basic].
14
 * @Models App [User].
15
 * @since 1.0
16
 */
17
18
namespace cjtterabytesoft\adminlte\basic\models;
19
20
class User extends \yii\base\Object implements \yii\web\IdentityInterface
21
{
22
    public $id;
23
    public $username;
24
    public $password;
25
    public $authKey;
26
    public $accessToken;
27
    public $created_at;
28
29
    private static $users = [
30
        '100' => [
31
            'id' => '100',
32
            'username' => 'admin',
33
            'password' => 'admin',
34
            'authKey' => 'test100key',
35
            'accessToken' => '100-token',
36
            'created_at' => '1434544238'
37
        ],
38
        '101' => [
39
            'id' => '101',
40
            'username' => 'demo',
41
            'password' => 'demo',
42
            'authKey' => 'test101key',
43
            'accessToken' => '101-token',
44
            'created_at' => '1434544238'
45
        ],
46
    ];
47
48
    /**
49
     * @inheritdoc
50
     */
51
    public static function findIdentity($id)
52
    {
53
        return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
54
    }
55
56
    /**
57
     * @inheritdoc
58
     */
59
    public static function findIdentityByAccessToken($token, $type = null)
60
    {
61
        foreach (self::$users as $user) {
62
            if ($user['accessToken'] === $token) {
63
                return new static($user);
64
            }
65
        }
66
67
        return null;
68
    }
69
70
    /**
71
     * Finds user by username
72
     *
73
     * @param  string      $username
74
     * @return static|null
75
     */
76
    public static function findByUsername($username)
77
    {
78
        foreach (self::$users as $user) {
79
            if (strcasecmp($user['username'], $username) === 0) {
80
                return new static($user);
81
            }
82
        }
83
84
        return null;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function getId()
91
    {
92
        return $this->id;
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function getAuthKey()
99
    {
100
        return $this->authKey;
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function validateAuthKey($authKey)
107
    {
108
        return $this->authKey === $authKey;
109
    }
110
111
    /**
112
     * Validates password
113
     *
114
     * @param  string  $password password to validate
115
     * @return boolean if password provided is valid for current user
116
     */
117
    public function validatePassword($password)
118
    {
119
        return $this->password === $password;
120
    }
121
}