Completed
Push — master ( c94440...b7f712 )
by vistart
20:04
created

getAdditionalAccountRules()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 0
crap 2
1
<?php
2
3
/**
4
 *   _   __ __ _____ _____ ___  ____  _____
5
 *  | | / // // ___//_  _//   ||  __||_   _|
6
 *  | |/ // /(__  )  / / / /| || |     | |
7
 *  |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\traits;
14
15
/**
16
 * Additional account features.
17
 * This trait should be used in blameable model or its extended class.
18
 * 
19
 * When an additional account is enabled with a separate password, this additional
20
 * account can only log in with its own unique password.
21
 * Note: The extra account password should not be the same as the master account
22
 * password, but we will not take the initiative to determine whether the two are the same.
23
 * 
24
 * @property boolean $seperateLogin determines whether this account could be used
25
 * for logging-in.
26
 * @property-read array $enableLoginAttributeRules
27
 * @property-read array $additionAccountRules
28
 * @version 1.0
29
 * @author vistart <[email protected]>
30
 */
31
trait AdditionalAccountTrait
32
{
33
    use PasswordTrait;
34
    
35
    /**
36
     * @var boolean|string The attribute of which determines whether enable to
37
     * login with current additional account. You can assign it to false if you
38
     * want to disable this feature, this is equivolent to not allow to login
39
     * with current additional account among all the users.
40
     */
41
    public $seperateLoginAttribute = false;
42
    
43
    /**
44
     * @var boolean  Determines whether login with current additional
45
     * account with a seperate password or not. If you set $enableLoginAttribute
46
     * to false, this feature will be skipped.
47
     */
48 5
    public function getPasswordIsSeperate()
49
    {
50 5
        return $this->getSeperateLogin() && !$this->getIsEmptyPassword();
51
    }
52
    
53
    /**
54
     * Get this additional account could be used for logging-in.
55
     * @return boolean
56
     */
57 5
    public function getSeperateLogin()
58
    {
59 5
        if (!$this->seperateLoginAttribute || empty($this->seperateLoginAttribute)) {
60
            return false;
61
        }
62 5
        $enableLoginAttribute = $this->seperateLoginAttribute;
63 5
        return $this->$enableLoginAttribute > 0;
64
    }
65
    
66
    /**
67
     * Set this additional accunt could be used for logging-in.
68
     * @param boolean $can
69
     * @return integer
70
     */
71 3
    public function setSeperateLogin($can)
72
    {
73 3
        if (!$this->seperateLoginAttribute || empty($this->seperateLoginAttribute)) {
74
            return;
75
        }
76 3
        $enableLoginAttribute = $this->seperateLoginAttribute;
77 3
        $this->$enableLoginAttribute = ($can ? 1 : 0);
78 3
    }
79
    
80
    /**
81
     * Get rules associated with enable login attribute.
82
     * If enable login feature by this additional account, it will return the rules
83
     * with true by default.
84
     * @return array rules.
85
     */
86 5
    public function getEnableLoginAttributeRules()
87
    {
88 5
        return $this->seperateLoginAttribute && is_string($this->seperateLoginAttribute) && !empty($this->seperateLoginAttribute) ? [
89 5
            [[$this->seperateLoginAttribute], 'boolean'],
90 5
            [[$this->seperateLoginAttribute], 'default', 'value' => true],
91 5
            ] : [];
92
    }
93
    
94
    /**
95
     * Get rules associated with additional account attributes.
96
     * @return array rules.
97
     */
98 5
    public function getAdditionalAccountRules()
99
    {
100 5
        $rules = $this->getEnableLoginAttributeRules();
101 5
        if ($this->getPasswordIsSeperate()) {
102 1
            $rules = array_merge($rules, $this->getPasswordHashRules());
103
        }
104 5
        return $rules;
105
    }
106
}