FRED™  3.0
FRED™: Framework for Rapid and Easy Development
User.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Rsi\Fred;
4 
5 class User extends Component{
6 
7  const RIGHT_LEVEL_READ = 'r';
8  const RIGHT_LEVEL_WRITE = 'w';
9  const RIGHT_LEVEL_EXECUTE = 'x';
10  const RIGHT_LEVEL_SEPARATOR = ':'; //!< Separator between right and level in a single string notation.
11 
12  const EVENT_RIGHT_LEVEL = 'user:rightLevel';
13  const EVENT_INVALIDATE = 'user:invalidate';
14  const EVENT_NAME = 'user:name';
15  const EVENT_RECORD = 'user:record';
16  const EVENT_ID = 'user:id';
17  const EVENT_SET = 'user:set';
18 
19  public $authorative = true;
20  public $authenticationControllerName = null; //!< Set if the user has to provide more information for authentication.
21  public $filesClassName = __CLASS__ . '\\Files'; //!< Class name for the file manager.
22 
23  protected $_id = null;
24  protected $_authenticators = null; //!< Available authentication checks.
25  protected $_record = null; //!< User data.
26  protected $_files = null;
27 
28  /**
29  * Check if a user is authenticated.
30  * @param array $auth_sets Possible sets of authentication checks (array of arrays).
31  * @return bool Null if the user has to provide some more information. The $authenticationControllerName can be used for
32  * this purpose.
33  */
34  public function authenticated($auth_sets){
35  $this->authenticationControllerName = null;
36  if(!$auth_sets) return true;
37  $best = null;
38  foreach($auth_sets as $set){
39  $missing = 0;
40  $controller_name = null;
41  foreach($set as $name){
42  $authenticated = $this->authenticators[$name]->authenticated;
43  if($authenticated === false){ //not possible with this set
44  $this->component('log')->debug('Negative authentication for ' . $name,__FILE__,__LINE__);
45  continue 2;
46  }
47  elseif(!$authenticated){
48  $missing++;
49  if(!$controller_name) $controller_name = $this->authenticators[$name]->controllerName;
50  }
51  }
52  if(!$missing) return true;
53  if(!$best || ($missing < $best)){
54  $best = $missing;
55  $this->authenticationControllerName = $controller_name;
56  }
57  }
58  return $best ? null : false;
59  }
60  /**
61  * Get the current user's right level.
62  * @param string $right Right to check
63  * @return string Level
64  */
65  public function level($right){
66  return $this->component('event')->trigger(self::EVENT_RIGHT_LEVEL,$this,$right);
67  }
68  /**
69  * Check if the authenticated user has a certain right.
70  * @param string $right Right to check, optionally completed with the minimal level the user must have.
71  * @param string $level Different level (overrules optional level in the right).
72  * @return bool
73  */
74  public function authorized($right,$level = null){
75  if(!$level) $level = \Rsi\Str::part($right,self::RIGHT_LEVEL_SEPARATOR,1) ?: self::RIGHT_LEVEL_READ;
76  $right = \Rsi\Str::part($right,self::RIGHT_LEVEL_SEPARATOR);
77  return $right ? $this->level($right) >= $level : true; //no right = always OK
78  }
79  /**
80  * Invalidate the current user.
81  */
82  public function invalidate(){
83  if($this->id){
84  $this->component('event')->trigger(self::EVENT_INVALIDATE,$this);
85  foreach($this->authenticators as $authenticator) $authenticator->invalidate();
86  $this->_id = $this->_record = null;
87  if($this->authorative) $this->session->id = null;
88  }
89  }
90  /**
91  * Returns a user friendly name for an user ID.
92  * @param mixed $id User ID.
93  * @return string
94  */
95  public function name($id){
96  return $this->component('event')->trigger(self::EVENT_NAME,$this,$id);
97  }
98  /**
99  * Get the record (data) for a user.
100  * @param mixed $id User ID.
101  * @return array Assoc.array (false when not found).
102  */
103  public function record($id){
104  return $this->component('event')->trigger(self::EVENT_RECORD,$this,$id);
105  }
106  /**
107  * Translate a user code (external ID) to a user ID (internal ID).
108  * @param string $code User code.
109  * @return string User ID.
110  */
111  public function id($code){
112  return $this->component('event')->trigger(self::EVENT_ID,$this,$code);
113  }
114 
115  protected function getAuthenticators(){
116  if($this->_authenticators === null){
117  $this->_authenticators = [];
118  foreach($this->config('authenticators',[]) as $name => $config){
119  $class_name = \Rsi\Record::get($config,'className',__CLASS__ . '\\Authenticator\\' . ucfirst($name));
120  $this->_authenticators[$name] = new $class_name($this->_fred,$config);
121  }
122  }
123  return $this->_authenticators;
124  }
125 
126  protected function setId($value){
127  if($value != $this->id){
128  $this->invalidate();
129  $this->_id = $value;
130  if($this->authorative) $this->session->id = $value;
131  }
132  }
133 
134  protected function getId(){
135  return $this->authorative ? $this->session->id : $this->_id;
136  }
137 
138  protected function getRecord(){
139  if($this->_record === null) $this->_record = $this->record($this->id);
140  return $this->_record;
141  }
142 
143  protected function getFiles(){
144  if(!$this->_files){
145  $class_name = $this->filesClassName;
146  $this->_files = new $class_name($this->_fred,$this->config('files',[]) + ['user' => $this]);
147  }
148  return $this->_files;
149  }
150 
151  protected function _get($key){
152  return \Rsi\Record::get($this->record,$key);
153  }
154 
155  protected function _set($key,$value){
156  $this->component('event')->trigger(self::EVENT_SET,$this,$key,$value);
157  }
158 
159  public function __clone(){
160  $this->authorative = false;
161  $this->_authenticators = null;
162  }
163 
164 }
getAuthenticators()
Definition: User.php:115
config($key, $default=null)
Retrieve a config value.
Definition: Component.php:53
_set($key, $value)
Definition: User.php:155
authorized($right, $level=null)
Check if the authenticated user has a certain right.
Definition: User.php:74
_get($key)
Definition: User.php:151
const EVENT_INVALIDATE
Definition: User.php:13
const EVENT_ID
Definition: User.php:16
const EVENT_NAME
Definition: User.php:14
$filesClassName
Class name for the file manager.
Definition: User.php:21
name($id)
Returns a user friendly name for an user ID.
Definition: User.php:95
$_record
User data.
Definition: User.php:25
const RIGHT_LEVEL_WRITE
Definition: User.php:8
record($id)
Get the record (data) for a user.
Definition: User.php:103
setId($value)
Definition: User.php:126
const EVENT_RECORD
Definition: User.php:15
authenticated($auth_sets)
Check if a user is authenticated.
Definition: User.php:34
invalidate()
Invalidate the current user.
Definition: User.php:82
const RIGHT_LEVEL_READ
Definition: User.php:7
$_authenticators
Available authentication checks.
Definition: User.php:24
const EVENT_RIGHT_LEVEL
Definition: User.php:12
$authenticationControllerName
Set if the user has to provide more information for authentication.
Definition: User.php:20
Basic component class.
Definition: Component.php:8
const RIGHT_LEVEL_SEPARATOR
Separator between right and level in a single string notation.
Definition: User.php:10
const RIGHT_LEVEL_EXECUTE
Definition: User.php:9
const EVENT_SET
Definition: User.php:17
id($code)
Translate a user code (external ID) to a user ID (internal ID).
Definition: User.php:111
level($right)
Get the current user&#39;s right level.
Definition: User.php:65
component($name)
Get a component (local or default).
Definition: Component.php:80