User.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. /**
  3. * Define an application user.
  4. * @author valentin carruesco
  5. * @category Core
  6. * @license copyright
  7. */
  8. class User extends Entity
  9. {
  10. public $id,$login,$password,$function,$name,$firstname,$mail,
  11. $state,$rights,$firms,$superadmin,$token,
  12. $preferences,$phone,$mobile,
  13. $groups,$ranks,$manager,$service,$origin,$meta;
  14. protected $fields =
  15. array(
  16. 'id' => 'key',
  17. 'login' => 'string',
  18. 'password' => 'string',
  19. 'name' => 'string',
  20. 'function' => 'string',
  21. 'firstname' => 'string',
  22. 'token' => 'string',
  23. 'mail' => 'string',
  24. 'state' => 'string',
  25. 'phone' => 'string',
  26. 'mobile' => 'string',
  27. 'manager' => 'string',
  28. 'service' => 'string',
  29. 'superadmin' => 'int'
  30. );
  31. function __construct(){
  32. parent::__construct();
  33. $this->token = self::generateToken();
  34. $this->meta = array();
  35. }
  36. public static function getAll($loadRight = true,$force = false){
  37. $users = array();
  38. if(isset($_SESSION['users']))
  39. $users = unserialize($_SESSION['users']);
  40. if(empty($users) || $force){
  41. Plugin::callHook('user_load',array(&$users, $loadRight));
  42. $_SESSION['users'] = serialize($users);
  43. }
  44. foreach (self::loadAll() as $baseUser) {
  45. $existingKey = null;
  46. if($loadRight){
  47. if(!isset($baseUser->ranks)) $baseUser->ranks = array();
  48. if(!isset($baseUser->firms)) $baseUser->firms = array();
  49. $baseUser->loadRanks();
  50. $baseUser->loadRights();
  51. }
  52. foreach ($users as $key => $otherUser) {
  53. //permet la predominance des users db sur les user foreign type type
  54. if($otherUser->login == $baseUser->login){
  55. $existingKey = $key;
  56. break;
  57. }
  58. }
  59. if(isset($existingKey)){
  60. $users[$existingKey] = $baseUser;
  61. }else{
  62. $users[] = $baseUser;
  63. }
  64. }
  65. return $users;
  66. }
  67. public function __sleep(){
  68. return array_merge(array('rights','ranks','firms','preferences','origin','meta'),array_keys($this->toArray()));
  69. }
  70. public function can($section,$right){
  71. if($this->superadmin == 1) return true;
  72. global $myFirm;
  73. $firm = is_object($myFirm) && $myFirm->id !=0 ? $myFirm->id : 0;
  74. if(isset($this->rights[$section][$firm][$right])){
  75. return $this->rights[$section][$firm][$right]==1;
  76. }
  77. if(isset($this->rights[$section][0][$right])){
  78. return $this->rights[$section][0][$right]==1;
  79. }
  80. return false;
  81. }
  82. public function preference($key=null, $value=null){
  83. if(!isset($key) && !isset($value)) return $this->preferences;
  84. if(isset($key) && !isset($value)) return isset($this->preferences[$key])?$this->preferences[$key]:'';
  85. if(isset($key) && isset($value)){
  86. $this->preferences[$key] = $value;
  87. $preference = UserPreference::load(array('key'=>$key,'user'=>$this->login));
  88. if(!$preference) $preference = new UserPreference();
  89. $preference->key = $key;
  90. $preference->value = $value;
  91. $preference->user = $this->login;
  92. $preference->save();
  93. }
  94. }
  95. public function loadRanks(){
  96. foreach (UserFirmRank::loadAll(array('user'=>$this->login), null, null, array('*'),1) as $firmRank) {
  97. $rank = $firmRank->join('rank');
  98. $firm = $firmRank->join('firm');
  99. $this->firms[$firm->id] = $firm;
  100. if(!isset($this->ranks[$firmRank->firm])) $this->ranks[$firmRank->firm] = array();
  101. $this->ranks[$firmRank->firm][]= $rank;
  102. }
  103. }
  104. public function loadPreferences(){
  105. $this->preferences = array();
  106. foreach(UserPreference::loadAll(array('user'=>$this->login)) as $line):
  107. $this->preferences[$line->key] = $line->value;
  108. endforeach;
  109. }
  110. public function loadRights(){
  111. global $myFirm;
  112. $this->rights = array();
  113. if($this->superadmin) return;
  114. if(!isset($this->ranks)) $this->ranks = array();
  115. if(!isset($myFirm) || !isset($this->ranks[$myFirm->id]) || count($this->ranks[$myFirm->id])==0) return;
  116. $ranksId = array();
  117. foreach($this->ranks[$myFirm->id] as $rank){
  118. if(!isset($rank->id) || !is_numeric($rank->id)) continue;
  119. $ranksId[] = $rank->id;
  120. }
  121. if(count($ranksId)==0) return;
  122. $rights = Right::staticQuery('SELECT * FROM {{table}} WHERE rank IN('.implode(',',$ranksId).')',array(),true);
  123. foreach($rights as $right):
  124. //Pour le premier rang qui aborde cette section on met tous les droits à false
  125. if(!isset($this->rights[$right->section][$right->firm])){
  126. $this->rights[$right->section][$right->firm] = array(
  127. 'read' => false,
  128. 'edit' => false,
  129. 'delete' => false,
  130. 'configure' => false
  131. );
  132. }
  133. //Puis on complete uniquement les droits à true sur la section pour chaques rangs additionnels
  134. if($right->read) $this->rights[$right->section][$right->firm]['read'] = true;
  135. if($right->edit) $this->rights[$right->section][$right->firm]['edit'] = true;
  136. if($right->delete) $this->rights[$right->section][$right->firm]['delete'] = true;
  137. if($right->configure) $this->rights[$right->section][$right->firm]['configure'] = true;
  138. endforeach;
  139. }
  140. public function getFirms(){
  141. $this->firms = array();
  142. foreach(Firm::staticQuery('SELECT f.* FROM {{table}} f LEFT JOIN '.UserFirmRank::tableName().' uf ON uf.firm=f.id WHERE uf.user=?',array($this->login),true) as $firm):
  143. $this->firms[$firm->id] = $firm;
  144. endforeach;
  145. }
  146. public function haveFirm($id){
  147. return in_array($id, array_keys($this->firms));
  148. }
  149. public function getAvatar($getPath = false){
  150. $avatar = 'img/default-avatar.png';
  151. $files = glob(__ROOT__.FILE_PATH.AVATAR_PATH.$this->login.'.{jpg,png,jpeg,gif}',GLOB_BRACE);
  152. if(count($files)>0){
  153. if($getPath) return $files[0];
  154. preg_match("/\.(\w{3,4})$|\?/m", $files[0], $extension);
  155. $avatar = 'action.php?action=account_avatar_download&user='.$this->login.'&extension='.$extension[1];
  156. }
  157. return $avatar;
  158. }
  159. public static function check($login, $password, $loadRight = true) {
  160. global $myFirm;
  161. $user = self::load(array('login' => $login, 'password' => self::password_encrypt($password)));
  162. //load from db
  163. if($user!=false){
  164. $user->ranks = array();
  165. $user->firms = array();
  166. $user->loadRanks();
  167. $user->loadPreferences();
  168. if($user->superadmin == 1){
  169. foreach(Firm::loadAll() as $firm)
  170. $firms[$firm->id] = $firm;
  171. $user->setFirms($firms);
  172. }
  173. $defaultFirm = !empty($user->preference('default_firm')) ? $user->preferences['default_firm'] : key($user->firms);
  174. $myFirm = isset($user->firms[$defaultFirm]) ? $user->firms[$defaultFirm]:reset($user->firms);
  175. if(!isset($user->firms[$defaultFirm])) $user->preference('default_firm',$myFirm->id);
  176. if($loadRight) $user->loadRights();
  177. }
  178. //load from plugings
  179. Plugin::callHook("user_login", array(&$user,$login,$password,$loadRight));
  180. $user = is_object($user) ? $user : new self();
  181. return $user;
  182. }
  183. public static function byLogin($login,$loadRight = true){
  184. foreach(User::getAll($loadRight) as $user){
  185. if($user->login == $login) return $user;
  186. }
  187. return new User();
  188. }
  189. public function fullName()
  190. {
  191. $fullName = ucfirst($this->firstname).' '.mb_strtoupper($this->name);
  192. return trim($fullName) != '' ? $fullName : $this->login;
  193. }
  194. public function initials(){
  195. $firstname = str_replace(array(' ','\''),'-',$this->firstname);
  196. $firstname = explode('-',$firstname);
  197. $result = '';
  198. foreach($firstname as $pren){
  199. $result.= strtoupper(substr($pren,0,1));
  200. }
  201. return $result.strtoupper(substr($this->name,0,1));
  202. }
  203. public function subordinates(){
  204. $subordinates = array();
  205. foreach (User::getAll() as $user) {
  206. if(is_object($user->manager) && $user->manager->login == $myUser->login) $subordinates[] = $user;
  207. }
  208. return $subordinates;
  209. }
  210. public static function passwordFormats(){
  211. $formats = array(
  212. array('pattern'=>'|[0-9]|i','label'=>'Le mot de passe doit comporter au minimum 1 chiffre (norme anssi)'),
  213. array('pattern'=>'|[A-Z]|','label'=>'Le mot de passe doit comporter au minimum 1 majuscule (norme anssi)'),
  214. array('pattern'=>'|[^A-Za-z0-9éèêëàäâïîöôûüù]|i','label'=>'Le mot de passe doit comporter au minimum 1 caractère spécial (norme anssi)'),
  215. array('pattern'=>'|.{6,}|','label'=>'Le mot de passe doit comporter au minimum 6 caractères'),
  216. array('pattern'=>'|.{12,}|','label'=>'Le mot de passe doit comporter au minimum 12 caractères (norme anssi)'),
  217. );
  218. return $formats;
  219. }
  220. public static function checkPasswordFormat($password){
  221. global $conf;
  222. $errors = array();
  223. $formats = array();
  224. foreach (self::passwordFormats() as $format) {
  225. $formats[$format['pattern']] = $format;
  226. }
  227. $selectedFormats = json_decode($conf->get('password_format'),true);
  228. if(is_array($selectedFormats)){
  229. foreach($selectedFormats as $pattern){
  230. if(!isset($formats[$pattern])) continue;
  231. $format = $formats[$pattern];
  232. if(!preg_match($pattern, $password)) $errors[] = $format['label'];
  233. }
  234. }
  235. return $errors;
  236. }
  237. public static function password_encrypt($password)
  238. {
  239. return sha1(md5($password));
  240. }
  241. public function connected()
  242. {
  243. return !empty($this->login);
  244. }
  245. public function setLogin($login){
  246. $this->login = $login;
  247. }
  248. public function setName($name){
  249. $this->name = $name;
  250. }
  251. public function setFirstName($firstname){
  252. $this->firstname = $firstname;
  253. }
  254. public function setMail($mail){
  255. $this->mail = $mail;
  256. }
  257. public function setPhone($phone){
  258. $this->phone = $phone;
  259. }
  260. public function setMobile($mobile){
  261. $this->mobile = $mobile;
  262. }
  263. public function setFunction($function){
  264. $this->function = $function;
  265. }
  266. public function setGroups($groups){
  267. $this->groups = $groups;
  268. }
  269. public static function generateToken(){
  270. return substr(md5(uniqid(rand(), true)),0,10);
  271. }
  272. public function getGroups(){
  273. return (is_array($this->groups) ? $this->groups : array());
  274. }
  275. public function setFirms($firms){
  276. if(empty($firms)) return;
  277. $this->firms = $firms;
  278. }
  279. //Retourne un objet manager (User vide si pas de manager) quel que soit le provider d'entré (ad : objet, db: login)
  280. public function manager(){
  281. $manager = new User();
  282. if(!isset($this->manager)) return $manager;
  283. if(is_object($this->manager)) $manager = $this->manager;
  284. if(is_string($this->manager) && !empty($this->manager)) $manager = User::byLogin($this->manager);
  285. return is_object($manager) ? $manager: new User();
  286. }
  287. }