UserModel.class.php 4.0 KB

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