You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
3.8 KiB

4 years ago
  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("ItemMember")->where("uid = '$uid' ")->delete();
  48. D("UserToken")->where("uid = '$uid' ")->delete();
  49. D("Template")->where("uid = '$uid' ")->delete();
  50. D("ItemTop")->where("uid = '$uid' ")->delete();
  51. $return = D("User")->where("uid = '$uid' ")->delete();
  52. return $return ;
  53. }
  54. //检测ldap登录
  55. public function checkLdapLogin($username ,$password ){
  56. $ldap_open = D("Options")->get("ldap_open" ) ;
  57. $ldap_form = D("Options")->get("ldap_form" ) ;
  58. $ldap_form = json_decode($ldap_form,1);
  59. if (!$ldap_open) {
  60. return false;
  61. }
  62. if (!$ldap_form['user_field']) {
  63. $ldap_form['user_field'] = 'cn';
  64. }
  65. $ldap_conn = ldap_connect($ldap_form['host'], $ldap_form['port']);//建立与 LDAP 服务器的连接
  66. if (!$ldap_conn) {
  67. return false;
  68. }
  69. ldap_set_option($ldap_conn, LDAP_OPT_PROTOCOL_VERSION, $ldap_form['version']);
  70. $rs=ldap_bind($ldap_conn, $ldap_form['bind_dn'], $ldap_form['bind_password']);//与服务器绑定 用户登录验证 成功返回1
  71. if (!$rs) {
  72. return false ;
  73. }
  74. $result = ldap_search($ldap_conn,$ldap_form['base_dn'],"(cn=*)");
  75. $data = ldap_get_entries($ldap_conn, $result);
  76. for ($i=0; $i<$data["count"]; $i++) {
  77. $ldap_user = $data[$i][$ldap_form['user_field']][0] ;
  78. $dn = $data[$i]["dn"] ;
  79. if ($ldap_user == $username) {
  80. //如果该用户不在数据库里,则帮助其注册
  81. $userInfo = D("User")->isExist($username) ;
  82. if(!$userInfo){
  83. D("User")->register($ldap_user,$ldap_user.time());
  84. }
  85. $rs2=ldap_bind($ldap_conn, $dn , $password);
  86. if ($rs2) {
  87. D("User")->updatePwd($userInfo['uid'], $password);
  88. return $this->checkLogin($username,$password);
  89. }
  90. }
  91. }
  92. return false ;
  93. }
  94. public function checkDbOk(){
  95. $ret = $this->find() ;
  96. if ($ret) {
  97. return true;
  98. }else{
  99. return false;
  100. }
  101. }
  102. }