Completed
Push — master ( 20774e...ca994b )
by Nazar
04:30
created

User::guest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   CleverStyle Framework
4
 * @author    Nazar Mokrynskyi <[email protected]>
5
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
6
 * @license   MIT License, see license.txt
7
 */
8
namespace cs;
9
use
10
	cs\DB\Accessor,
11
	cs\User\Data as User_data,
12
	cs\User\Group as User_group,
13
	cs\User\Management as User_management,
14
	cs\User\Permission as User_permission,
15
	cs\User\Profile as User_profile;
16
17
/**
18
 * Class for users manipulating
19
 *
20
 * Provides next events:
21
 *  System/User/construct/before
22
 *
23
 *  System/User/construct/after
24
 *
25
 *  System/User/registration/before
26
 *  ['email' => <i>email</i>]
27
 *
28
 *  System/User/registration/after
29
 *  ['id' => <i>user_id</i>]
30
 *
31
 *  System/User/registration/confirmation/before
32
 *  ['reg_key' => <i>reg_key</i>]
33
 *
34
 *  System/User/registration/confirmation/after
35
 *  ['id' => <i>user_id</i>]
36
 *
37
 *  System/User/del/before
38
 *  ['id' => <i>user_id</i>]
39
 *
40
 *  System/User/del/after
41
 *  ['id' => <i>user_id</i>]
42
 *
43
 * @property int    $id
44
 * @property string $login
45
 * @property string $login_hash    sha224 hash
46
 * @property string $username
47
 * @property string $password_hash sha512 hash
48
 * @property string $email
49
 * @property string $email_hash    sha224 hash
50
 * @property string $language
51
 * @property string $timezone
52
 * @property int    $reg_date      unix timestamp
53
 * @property string $reg_ip        hex value, obtained by function ip2hex()
54
 * @property string $reg_key       random md5 hash, generated during registration
55
 * @property int    $status        '-1' - not activated (for example after registration), 0 - inactive, 1 - active
56
 * @property string $avatar
57
 *
58
 * @method static $this instance($check = false)
59
 */
60
class User {
61
	use
62
		Accessor,
63
		Singleton,
64
		User_data,
65
		User_group,
66
		User_management,
67
		User_permission,
68
		User_profile;
69
	/**
70
	 * Id of system guest user
71
	 */
72
	const GUEST_ID = 1;
73
	/**
74
	 * Id of first, primary system administrator
75
	 */
76
	const ROOT_ID = 2;
77
	/**
78
	 * Id of system group for administrators
79
	 */
80
	const ADMIN_GROUP_ID = 1;
81
	/**
82
	 * Id of system group for users
83
	 */
84
	const USER_GROUP_ID = 2;
85
	/**
86
	 * Status of active user
87
	 */
88
	const STATUS_ACTIVE = 1;
89
	/**
90
	 * Status of inactive user
91
	 */
92
	const STATUS_INACTIVE = 0;
93
	/**
94
	 * Status of not activated user
95
	 */
96
	const STATUS_NOT_ACTIVATED = -1;
97
	/**
98
	 * @var Cache\Prefix
99
	 */
100
	protected $cache;
101
	/**
102
	 * Whether to use memory cache (locally, inside object, may require a lot of memory if working with many users together)
103
	 * @var bool
104
	 */
105
	protected $memory_cache = true;
106
	/**
107
	 * Returns database index
108
	 *
109
	 * @return int
110
	 */
111 40
	protected function cdb () {
112 40
		return Config::instance()->module('System')->db('users');
113
	}
114 46
	protected function construct () {
115 46
		$this->cache = Cache::prefix('users');
116 46
		Event::instance()->fire('System/User/construct/before');
117 46
		$this->initialize_data();
118
		/**
119
		 * Initialize session
120
		 */
121 46
		Session::instance();
122 46
		Event::instance()->fire('System/User/construct/after');
123 46
	}
124
	/**
125
	 * Get data item of current user
126
	 *
127
	 * @param string|string[] $item
128
	 *
129
	 * @return false|int|mixed[]|string|User\Properties If <i>$item</i> is integer - cs\User\Properties object will be returned
130
	 */
131 36
	public function __get ($item) {
132 36
		if ($item == 'id') {
133 36
			return Session::instance()->get_user();
134
		}
135 8
		return $this->get($item);
136
	}
137
	/**
138
	 * Set data item of current user
139
	 *
140
	 * @param array|int|string $item Item-value array may be specified for setting several items at once
141
	 * @param mixed|null       $value
142
	 */
143 2
	public function __set ($item, $value = null) {
144 2
		$this->set($item, $value);
145 2
	}
146
	/**
147
	 * Is admin
148
	 *
149
	 * Proxy to \cs\Session::instance()->admin() for convenience
150
	 *
151
	 * @return bool
152
	 */
153 8
	public function admin () {
154 8
		return Session::instance()->admin();
155
	}
156
	/**
157
	 * Is user
158
	 *
159
	 * Proxy to \cs\Session::instance()->user() for convenience
160
	 *
161
	 * @return bool
162
	 */
163 2
	public function user () {
164 2
		return Session::instance()->user();
165
	}
166
	/**
167
	 * Is guest
168
	 *
169
	 * Proxy to \cs\Session::instance()->guest() for convenience
170
	 *
171
	 * @return bool
172
	 */
173 14
	public function guest () {
174 14
		return Session::instance()->guest();
175
	}
176
	/**
177
	 * Disable memory cache
178
	 *
179
	 * Memory cache stores users data inside User class in order to get data faster next time.
180
	 * But in case of working with large amount of users this cache can be too large. Disabling will cause some performance drop, but save a lot of RAM.
181
	 */
182 18
	public function disable_memory_cache () {
183 18
		$this->memory_cache = false;
184 18
		$this->data         = [];
185 18
		$this->permissions  = [];
186 18
	}
187
}
188