AdminService.class.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. namespace Admin\Service;
  3. /**
  4. * AdminService
  5. */
  6. class AdminService extends CommonService {
  7. /**
  8. * 添加管理员
  9. * @param array $admin 管理员信息
  10. * @return array
  11. */
  12. public function add($admin) {
  13. $Admin = $this->getD();
  14. $Admin->startTrans();
  15. if (false === ($admin = $Admin->create($admin))) {
  16. return $this->errorResultReturn($Admin->getError());
  17. }
  18. $as = $Admin->add($admin);
  19. $roleAdmin = array(
  20. 'role_id' => $admin['role_id'],
  21. 'user_id' => $Admin->getLastInsId()
  22. );
  23. $ras = M('RoleAdmin')->add($roleAdmin);
  24. if (false === $as || false === $ras) {
  25. $Admin->rollback();
  26. return $this->errorResultReturn('系统出错了!');
  27. }
  28. $Admin->commit();
  29. return $this->resultReturn(true);
  30. }
  31. /**
  32. * 更新管理员信息
  33. * @return
  34. */
  35. public function update($admin) {
  36. $Admin = $this->getD();
  37. if (false === ($admin = $Admin->create($admin))) {
  38. return $this->errorResultReturn($Admin->getError());
  39. }
  40. if (empty($admin['password'])) {
  41. unset($admin['password']);
  42. }
  43. if (false === $Admin->save($admin)) {
  44. return $this->errorResultReturn('系统错误!');
  45. }
  46. M('RoleAdmin')->where("user_id={$admin['id']}")
  47. ->save(array('role_id' => $admin['role_id']));
  48. return $this->resultReturn(true);
  49. }
  50. /**
  51. * 管理员登录认证
  52. * @param array $admin 管理员信息
  53. * @return array
  54. */
  55. public function login($admin) {
  56. $Admin = $this->getM();
  57. // 邮箱是否存在
  58. if (!$this->existAccount($admin['email'])) {
  59. return $this->errorResultReturn('邮箱不存在!');
  60. }
  61. $account = $Admin->getByEmail($admin['email']);
  62. // 密码验证
  63. if ($account['password'] != $this->encrypt($admin['password'])) {
  64. return $this->errorResultReturn('密码不正确!');
  65. }
  66. // 是否启用
  67. if (!$this->isActive($admin['email'])) {
  68. return $this->errorResultReturn('账户已被禁用!');
  69. }
  70. $loginMarked = C('LOGIN_MARKED');
  71. $shell = $this->genShell($account['id'], $account['password']);
  72. // 生成登录session
  73. $_SESSION[$loginMarked] = $shell;
  74. // 生成登录cookie
  75. $shell .= '_' . time();
  76. setcookie($loginMarked, $shell, 0, '/');
  77. $_SESSION['current_account'] = $account;
  78. // 权限认证
  79. if (C('USER_AUTH_ON')) {
  80. $_SESSION[C('USER_AUTH_KEY')] = $account['id'];
  81. if ($account['is_super']) {
  82. // 超级管理员无需认证
  83. $_SESSION[C('ADMIN_AUTH_KEY')] = true;
  84. }
  85. // 缓存访问权限
  86. \Org\Util\Rbac::saveAccessList();
  87. }
  88. // 更新最后登录时间
  89. $Admin->where("id={$account['id']}")
  90. ->save(array('last_login_at' => time()));
  91. return $this->resultReturn(true);
  92. }
  93. /**
  94. * 管理员登出
  95. * @return
  96. */
  97. public function logout() {
  98. $this->unsetLoginMarked();
  99. if (C('USER_AUTH_ON')) {
  100. unset($_SESSION[C('USER_AUTH_KEY')]);
  101. unset($_SESSION[C('ADMIN_AUTH_KEY')]);
  102. }
  103. session_destroy();
  104. }
  105. /**
  106. * 检查登录状态
  107. * @return array
  108. */
  109. public function checkLogin() {
  110. $loginMarked = C('LOGIN_MARKED');
  111. // 是否已登录
  112. if (!isset($_COOKIE[$loginMarked])) {
  113. return $this->errorResultReturn('尚未登录,请先进行登录!');
  114. }
  115. // 是否登录超时
  116. $cookie = explode('_', $_COOKIE[$loginMarked]);
  117. $timeout = C('LOGIN_TIMEOUT');
  118. if (time() > (end($cookie) + $timeout)) {
  119. $this->unsetLoginMarked();
  120. return $this->errorResultReturn('登录超时,请重新登录!');
  121. }
  122. // 是否帐号异常
  123. if ($cookie[0] != $_SESSION[$loginMarked]) {
  124. $this->unsetLoginMarked();
  125. return $this->errorResultReturn('账户异常,请重新登录!');
  126. }
  127. // 重新设置过期时间
  128. setcookie($loginMarked, $cookie[0] . '_' . time(), 0, '/');
  129. return $this->resultReturn(true);
  130. }
  131. /**
  132. * 加密数据
  133. * @param string $data 需要加密的数据
  134. * @return string
  135. */
  136. public function encrypt($data) {
  137. return md5(C('AUTH_MASK') . md5($data));
  138. }
  139. /**
  140. * 生成登录shell
  141. * @param int $id shell的id
  142. * @param string $password shell的密码
  143. * @return string
  144. */
  145. private function genShell($id, $password) {
  146. return md5($password . C('AUTH_TOKEN')) . $id;
  147. }
  148. /**
  149. * 销毁登录标记
  150. * @return
  151. */
  152. private function unsetLoginMarked() {
  153. $loginMarked = C('LOGIN_MARKED');
  154. setcookie("{$loginMarked}", null, -3600, '/');
  155. unset($_SESSION[$loginMarked], $_COOKIE[$loginMarked]);
  156. return ;
  157. }
  158. /**
  159. * 是否存在帐号
  160. * @param string $email email
  161. * @return boolean
  162. */
  163. public function existAccount($email) {
  164. if ($this->getM()->where("email='{$email}'")->count() > 0) {
  165. return true;
  166. }
  167. return false;
  168. }
  169. /**
  170. * 是否存在管理员
  171. * @param int $id 管理员id
  172. * @return boolean
  173. */
  174. public function existAdmin($id) {
  175. return !is_null($this->getM()->getById($id));
  176. }
  177. /**
  178. * 是否存在初始化的管理员
  179. * @return boolean
  180. */
  181. public function existInitAdmin() {
  182. $where = array(
  183. 'id' => 1,
  184. 'is_super' => 1
  185. );
  186. return !is_null($this->getM()->where($where)->find());
  187. }
  188. /**
  189. * 是否已经发送重置密码邮件
  190. * @param int $id 管理员id
  191. * @param string $hash 邮件hash值
  192. * @return boolean
  193. */
  194. public function hasSendMail($id, $hash) {
  195. $where = array(
  196. 'id' => $id,
  197. 'mail_hash' => $hash
  198. );
  199. return !is_null($this->getM()->where($where)->find());
  200. }
  201. /**
  202. * 账户是否启用
  203. * @param string $email email
  204. * @return boolean
  205. */
  206. public function isActive($email) {
  207. $where = array(
  208. 'email' => $email,
  209. 'is_active' => 1
  210. );
  211. if ($this->getM()->where($where)->count() > 0) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. protected function getModelName() {
  217. return 'Admin';
  218. }
  219. }