User.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /*
  3. @nom: User
  4. @auteur: Idleman (idleman@idleman.fr)
  5. @description: Classe de gestion des utilisateurs
  6. */
  7. class User extends SQLiteEntity{
  8. protected $id,$login,$password,$name,$firstname,$mail,$state,$groups,$rank,$rights,$phone,$token,$cookie;
  9. protected $TABLE_NAME = 'user';
  10. protected $CLASS_NAME = 'User';
  11. protected $object_fields =
  12. array(
  13. 'id'=>'key',
  14. 'login'=>'string',
  15. 'password'=>'string',
  16. 'name'=>'string',
  17. 'firstname'=>'string',
  18. 'mail'=>'string',
  19. 'rank'=>'int',
  20. 'token'=>'string',
  21. 'state'=>'int',
  22. 'cookie'=>'string'
  23. );
  24. function __construct(){
  25. parent::__construct();
  26. }
  27. function setId($id){
  28. $this->id = $id;
  29. }
  30. //Teste la validité d'un compte à l'identification
  31. static function exist($login,$password,$force = false,$checkRights = true ){
  32. $userManager = new User();
  33. $newUser = false;
  34. if($force){
  35. $newUser = $userManager->load(array('login'=>$login));
  36. }else{
  37. $newUser = $userManager->load(array('login'=>$login,'password'=>sha1(md5($password))));
  38. }
  39. Plugin::callHook("action_pre_login", array(&$newUser));
  40. if(is_object($newUser) && $checkRights) $newUser->loadRight();
  41. return $newUser;
  42. }
  43. //Récupère les droits en CRUD de l'utilisateur courant et les charge dans son tableau de droits interne
  44. function loadRight(){
  45. $rightManager = new Right();
  46. $rights = $rightManager->loadAll(array('rank'=>$this->getRank()));
  47. $sectionManager= new Section();
  48. foreach($rights as $right){
  49. $section = $sectionManager->getById($right->getSection());
  50. if(is_object($section)){
  51. $this->rights[$section->getLabel()]['c'] = ($right->getCreate()=='1'?true:false);
  52. $this->rights[$section->getLabel()]['r'] = ($right->getRead()=='1'?true:false);
  53. $this->rights[$section->getLabel()]['u'] = ($right->getUpdate()=='1'?true:false);
  54. $this->rights[$section->getLabel()]['d'] = ($right->getDelete()=='1'?true:false);
  55. }else{
  56. //Supression suite à des problèmes de perte de section quand la base plante
  57. //TODO - A modifier en evitant cette action sur les erreurs SQL type database locked
  58. //$rightManager->delete(array('section'=>$right->getSection()));
  59. }
  60. }
  61. }
  62. static function getByLogin($login){
  63. $returnedUser = new User();
  64. $users = User::getAllUsers();
  65. foreach($users as $user){
  66. if($user->getLogin()==$login) $returnedUser = $user;
  67. }
  68. return $returnedUser;
  69. }
  70. //Retourne une liste d'objets contenant tout les utilisateurs habilités à se connecter au programme
  71. //@return Liste d'objets User
  72. static function getAllUsers(){
  73. $userManager = new User();
  74. $users = $userManager->populate();
  75. Plugin::callHook("user_get_all", array(&$users));
  76. usort($users, "User::userorder");
  77. return $users;
  78. }
  79. static function userorder($a, $b)
  80. {
  81. return strcmp($a->getName(), $b->getName());
  82. }
  83. function getGravatar($size = 100){
  84. $gravatar = AVATAR_FOLDER.'/'.$this->getMail().'-'.$size.'.jpg';
  85. if(!file_exists($gravatar)){
  86. if (!file_exists(AVATAR_FOLDER)) mkdir(AVATAR_FOLDER);
  87. file_put_contents($gravatar, file_get_contents("http://www.gravatar.com/avatar/" . md5( strtolower( trim( $this->getMail() ) ) ) . "?&s=".$size));
  88. }
  89. return $gravatar;
  90. }
  91. function getGravatarImg($size = 100){
  92. return "<img class='avatar avatar-".$size."' src='".$this->getGravatar($size)."' />" ;
  93. }
  94. function can($section,$selectedRight){
  95. return (!isset($this->rights[$section])?false:$this->rights[$section][$selectedRight]);
  96. }
  97. function haveGroup($group){
  98. return in_array($group,$this->getGroups());
  99. }
  100. function getId(){
  101. return $this->id;
  102. }
  103. function getLogin(){
  104. return $this->login;
  105. }
  106. function setLogin($login){
  107. $this->login = $login;
  108. }
  109. function getFullName(){
  110. $fn = ucfirst($this->firstname).' '.strtoupper($this->name);
  111. return trim($fn)==''?'Anonymous Guy':$fn;
  112. }
  113. function getName(){
  114. return $this->name;
  115. }
  116. function getFirstName(){
  117. return $this->firstname;
  118. }
  119. function getMail(){
  120. return $this->mail;
  121. }
  122. function getState(){
  123. return $this->state;
  124. }
  125. function setName($name){
  126. $this->name = $name;
  127. }
  128. function setFirstName($firstname){
  129. $this->firstname = $firstname;
  130. }
  131. function setMail($mail){
  132. $this->mail = $mail;
  133. }
  134. function setState($state){
  135. $this->state = $state;
  136. }
  137. function setGroups($groups){
  138. $this->groups = $groups;
  139. }
  140. function getGroups(){
  141. return (is_array($this->groups)?$this->groups:array());
  142. }
  143. function getRank(){
  144. return $this->rank;
  145. }
  146. function setRank($rank){
  147. $this->rank = $rank;
  148. $this->loadRight();
  149. }
  150. function setPassword($password){
  151. $this->password = User::cryptPassword($password);
  152. }
  153. public static function cryptPassword($string){
  154. return sha1(md5($string));
  155. }
  156. function setPhone($phone){
  157. $this->phone = $phone;
  158. }
  159. function getPhone(){
  160. return $this->phone;
  161. }
  162. function setToken($token){
  163. $this->token = $token;
  164. }
  165. function getToken(){
  166. return $this->token;
  167. }
  168. function setCookie($cookie){
  169. $this->cookie = $cookie;
  170. }
  171. function getCookie(){
  172. return $this->cookie;
  173. }
  174. }
  175. ?>