Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

117 rader
4.0 KiB

3 år sedan
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. class UserModel extends BaseModel {
  5. /**
  6. * 用户名是否已经存在
  7. *
  8. */
  9. public function isExist($username){
  10. return $this->where("username = '%s'",array($username))->find();
  11. }
  12. /**
  13. * 注册新用户
  14. *
  15. */
  16. public function register($username,$password){
  17. $password = md5(base64_encode(md5($password)).'576hbgh6');
  18. return $this->add(array('username'=>$username ,'password'=>$password , 'reg_time'=>time()));
  19. }
  20. //修改用户密码
  21. public function updatePwd($uid, $password){
  22. $password = md5(base64_encode(md5($password)).'576hbgh6');
  23. return $this->where("uid ='%d' ",array($uid))->save(array('password'=>$password));
  24. }
  25. /**
  26. * 返回用户信息
  27. * @return
  28. */
  29. public function userInfo($uid){
  30. return $this->where("uid = '%d'",array($uid))->find();
  31. }
  32. /**
  33. *@param username:登录名
  34. *@param password 登录密码
  35. */
  36. public function checkLogin($username,$password){
  37. $password = md5(base64_encode(md5($password)).'576hbgh6');
  38. $where=array($username,$password,$username,$password);
  39. return $this->where("( username='%s' and password='%s' ) ",$where)->find();
  40. }
  41. //设置最后登录时间
  42. public function setLastTime($uid){
  43. return $this->where("uid='%s'",array($uid))->save(array("last_login_time"=>time()));
  44. }
  45. //删除用户
  46. public function delete_user($uid){
  47. D("TeamMember")->where("member_uid = '$uid' ")->delete();
  48. D("TeamItemMember")->where("member_uid = '$uid' ")->delete();
  49. D("ItemMember")->where("uid = '$uid' ")->delete();
  50. D("UserToken")->where("uid = '$uid' ")->delete();
  51. D("Template")->where("uid = '$uid' ")->delete();
  52. D("ItemTop")->where("uid = '$uid' ")->delete();
  53. $return = D("User")->where("uid = '$uid' ")->delete();
  54. return $return ;
  55. }
  56. //检测ldap登录
  57. public function checkLdapLogin($username ,$password ){
  58. $ldap_open = D("Options")->get("ldap_open" ) ;
  59. $ldap_form = D("Options")->get("ldap_form" ) ;
  60. $ldap_form = json_decode($ldap_form,1);
  61. if (!$ldap_open) {
  62. return false;
  63. }
  64. if (!$ldap_form['user_field']) {
  65. $ldap_form['user_field'] = 'cn';
  66. }
  67. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  68. if (!$ldap_conn) {
  69. return false;
  70. }
  71. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  72. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  73. if (!$rs) {
  74. return false ;
  75. }
  76. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  77. $data = ldap_get_entries($ldap_conn, $result);
  78. for ($i=0; $i<$data["count"]; $i++) {
  79. $ldap_user = $data[$i][$ldap_form['user_field']][0] ;
  80. $dn = $data[$i]["dn"] ;
  81. if ($ldap_user == $username) {
  82. //如果该用户不在数据库里,则帮助其注册
  83. $userInfo = D("User")->isExist($username) ;
  84. if(!$userInfo){
  85. D("User")->register($ldap_user,$ldap_user.time());
  86. }
  87. $rs2=ldap_bind($ldap_conn, $dn , $password);
  88. if ($rs2) {
  89. D("User")->updatePwd($userInfo['uid'], $password);
  90. return $this->checkLogin($username,$password);
  91. }
  92. }
  93. }
  94. return false ;
  95. }
  96. public function checkDbOk(){
  97. $ret = $this->find() ;
  98. if ($ret) {
  99. return true;
  100. }else{
  101. return false;
  102. }
  103. }
  104. }