User.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. class User extends AbstractModel
  3. {
  4. use DomainLimitTrait;
  5. /**
  6. * @inheritdoc
  7. */
  8. public static $table = DBT_USERS;
  9. /**
  10. * @inheritdoc
  11. */
  12. public static $idAttribute = DBC_USERS_ID;
  13. const ROLE_USER = 'user';
  14. const ROLE_ADMIN = 'admin';
  15. /**
  16. * @var AbstractRedirect
  17. */
  18. protected $conflictingRedirect = null;
  19. /**
  20. * @var ModelCollection|AbstractRedirect[]
  21. */
  22. protected $redirects = null;
  23. /**
  24. * @inheritdoc
  25. */
  26. protected function setupDbMapping($childMapping = array())
  27. {
  28. $thisMappings = array(
  29. 'username' => DBC_USERS_USERNAME,
  30. 'domain' => DBC_USERS_DOMAIN,
  31. 'password_hash' => DBC_USERS_PASSWORD,
  32. );
  33. if(defined('DBC_USERS_MAILBOXLIMIT')){
  34. $thisMappings['mailboxLimit'] = DBC_USERS_MAILBOXLIMIT;
  35. }
  36. return array_replace(
  37. parent::setupDbMapping($thisMappings),
  38. $childMapping
  39. );
  40. }
  41. /**
  42. * @inheritdoc
  43. */
  44. protected function __construct($data)
  45. {
  46. parent::__construct($data);
  47. $this->setUsername($data[DBC_USERS_USERNAME]);
  48. $this->setDomain($data[DBC_USERS_DOMAIN]);
  49. $this->setPasswordHash($data[DBC_USERS_PASSWORD]);
  50. $this->setMailboxLimit(defined('DBC_USERS_MAILBOXLIMIT') ? intval($data[DBC_USERS_MAILBOXLIMIT]) : 0);
  51. $this->setAttribute('role', static::getRoleByEmail($this->getEmail()));
  52. }
  53. /**
  54. * @return string
  55. */
  56. public function getUsername()
  57. {
  58. return $this->getAttribute('username');
  59. }
  60. /**
  61. * @param string $value
  62. */
  63. public function setUsername($value)
  64. {
  65. $this->setAttribute('username', strtolower($value));
  66. }
  67. /**
  68. * @return string
  69. */
  70. public function getDomain()
  71. {
  72. return $this->getAttribute('domain');
  73. }
  74. /**
  75. * @param string $value
  76. */
  77. public function setDomain($value)
  78. {
  79. $this->setAttribute('domain', strtolower($value));
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getEmail()
  85. {
  86. return $this->getUsername().'@'.$this->getDomain();
  87. }
  88. /**
  89. * @return string
  90. */
  91. public function getPasswordHash()
  92. {
  93. return $this->getAttribute('password_hash');
  94. }
  95. /**
  96. * @param string $value
  97. */
  98. public function setPasswordHash($value)
  99. {
  100. $this->setAttribute('password_hash', $value);
  101. }
  102. /**
  103. * @return int
  104. */
  105. public function getMailboxLimit()
  106. {
  107. return $this->getAttribute('mailboxLimit');
  108. }
  109. /**
  110. * @param int $value
  111. */
  112. public function setMailboxLimit($value)
  113. {
  114. $this->setAttribute('mailboxLimit', $value);
  115. }
  116. /**
  117. * Get mailbox limit default via database default value
  118. *
  119. * @return int
  120. */
  121. public static function getMailboxLimitDefault()
  122. {
  123. global $db;
  124. if(defined('DBC_USERS_MAILBOXLIMIT')){
  125. $sql = "SELECT DEFAULT(".DBC_USERS_MAILBOXLIMIT.") FROM `".static::$table."` LIMIT 1";
  126. if(!$result = $db->query($sql)){
  127. dbError($db->error, $sql);
  128. }
  129. if($result->num_rows === 1){
  130. $row = $result->fetch_array();
  131. return intval($row[0]);
  132. }
  133. }
  134. return 0;
  135. }
  136. /**
  137. * @return string
  138. */
  139. public function getRole()
  140. {
  141. return $this->getAttribute('role');
  142. }
  143. /**
  144. * @param string $email
  145. *
  146. * @return string
  147. */
  148. private static function getRoleByEmail($email)
  149. {
  150. global $admins;
  151. if(in_array($email, $admins)){
  152. return static::ROLE_ADMIN;
  153. }
  154. return static::ROLE_USER;
  155. }
  156. /**
  157. * Is user limited by domain limits?
  158. *
  159. * @return bool
  160. */
  161. public function isDomainLimited()
  162. {
  163. global $adminDomainLimits;
  164. return defined('ADMIN_DOMAIN_LIMITS_ENABLED')
  165. && isset($adminDomainLimits) && isset($adminDomainLimits[$this->getEmail()]);
  166. }
  167. /**
  168. * Get domain limits, returns an empty array if user has no limits or ADMIN_DOMAIN_LIMITS_ENABLED is disabled
  169. *
  170. * @return array
  171. */
  172. public function getDomainLimits()
  173. {
  174. global $adminDomainLimits;
  175. if($this->isDomainLimited()){
  176. if (!is_array($adminDomainLimits[$this->getEmail()])) {
  177. throw new InvalidArgumentException('Config value of admin domain limits for email "'.$this->getEmail().'" needs to be of type array.');
  178. }
  179. return $adminDomainLimits[$this->getEmail()];
  180. }
  181. return array();
  182. }
  183. /**
  184. * @return AbstractRedirect
  185. */
  186. public function getConflictingRedirect()
  187. {
  188. if(is_null($this->conflictingRedirect)){
  189. $this->conflictingRedirect = AbstractRedirect::findWhereFirst(
  190. array(DBC_ALIASES_SOURCE, $this->getEmail())
  191. );
  192. }
  193. return $this->conflictingRedirect;
  194. }
  195. /**
  196. * @return ModelCollection|AbstractRedirect[]
  197. */
  198. public function getRedirects()
  199. {
  200. if(is_null($this->redirects)){
  201. $this->redirects = AbstractRedirect::findMultiWhere(
  202. array(DBC_ALIASES_DESTINATION, 'LIKE', '%'.$this->getEmail().'%')
  203. );
  204. }
  205. return $this->redirects;
  206. }
  207. /**
  208. * @return ModelCollection|AbstractRedirect[]
  209. */
  210. public function getAnonymizedRedirects()
  211. {
  212. $redirects = $this->getRedirects();
  213. foreach($redirects as $redirect){
  214. $emails = $redirect->getDestination();
  215. if(is_array($emails) && count($emails) > 1){
  216. $redirect->setDestination(array($this->getEmail(), '&hellip;'));
  217. }
  218. }
  219. return $redirects;
  220. }
  221. /**
  222. * Change this users password, throws Exception if password is invalid.
  223. *
  224. * @param string $password
  225. * @param string $passwordRepeated
  226. *
  227. * @throws Exception
  228. */
  229. public function changePassword($password, $passwordRepeated)
  230. {
  231. Auth::validateNewPassword($password, $passwordRepeated);
  232. $passwordHash = Auth::generatePasswordHash($password);
  233. $this->setPasswordHash($passwordHash);
  234. $this->save();
  235. }
  236. /**
  237. * @inheritdoc
  238. */
  239. public static function findAll($orderBy = array(DBC_USERS_DOMAIN, DBC_USERS_USERNAME))
  240. {
  241. return parent::findAll($orderBy);
  242. }
  243. /**
  244. * @param string $email
  245. *
  246. * @return static|null
  247. */
  248. public static function findByEmail($email)
  249. {
  250. $emailInParts = explode("@", $email);
  251. if(count($emailInParts) !== 2){
  252. return null;
  253. }
  254. $username = $emailInParts[0];
  255. $domain = $emailInParts[1];
  256. return static::findWhereFirst(
  257. array(
  258. array('username', $username),
  259. array('domain', $domain)
  260. )
  261. );
  262. }
  263. }