?

Log in

No account? Create an account
Patch Dokuwiki for new user registration with OpenLDAP - Unix-Solution [entries|archive|friends|userinfo]
Unix-Solution

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

Patch Dokuwiki for new user registration with OpenLDAP [Dec. 8th, 2007|05:04 pm]
Unix-Solution

unix_solution

[maximum_value]
Applies for Dokuwiki from 2006-03-05 up to 2007-06-26 (may be later)


*** start ***

--- inc/auth/ldap.class.php.orig        Tue Jun 26 22:27:15 2007
+++ inc/auth/ldap.class.php     Sun Dec  2 04:58:52 2007
@@ -19,6 +19,10 @@
         global $conf;
         $this->cnf = $conf['auth']['ldap'];

+       $this->cando['addUser'] = true;
+        $this->cando['modPass'] = true;                                                                              
+        $this->cando['modName'] = true;                                                                              
+        $this->cando['modMail'] = true;                                                                      
         // ldap extension is needed
         if(!function_exists('ldap_connect')) {
             if ($this->cnf['debug'])
@@ -184,6 +188,7 @@
         $info['dn']   = $user_result['dn'];
         $info['mail'] = $user_result['mail'][0];
         $info['name'] = $user_result['cn'][0];
+        $info['pass'] = $user_result['userpassword'][0];
         $info['grps'] = array();

         // overwrite if other attribs are specified.
@@ -337,6 +342,61 @@

         return true;
     }
+
+    function createUser($user,$pwd,$name,$mail,$grps=null){
+       global $conf;
+        if(!$this->_openLDAP()) return false;
+        // force superuser bind if wanted and not bound as superuser yet
+        if($this->cnf['binddn'] && $this->cnf['bindpw'] && $this->bound < 2){
+            // use superuser credentials
+            if(!@ldap_bind($this->con,$this->cnf['binddn'],$this->cnf['bindpw'])){
+                if($this->cnf['debug'])
+                    msg('LDAP bind as superuser: '.htmlspecialchars(ldap_error($this->con)),0,__LINE__,__FILE__);
+                return false;
+            }
+            $this->bound = 2;
+        }
+            if (strpos($this->cnf['usertree'], '%{user}')) {
+                $dn=str_replace ('%{user}', $user, $this->cnf['usertree']);
+            }
+           $info["cn"]=$user;
+            $info["sn"]=$name;
+            $info["mail"]=$mail;
+           $info["objectClass"][0]="inetOrgPerson";
+           $info["objectClass"][1]="top";
+           $info["userPassword"]=auth_cryptPassword($pwd);
+            if (@ldap_add($this->con,$dn, $info)) {
+                $group_name="cn=".$conf['defaultgroup'].",".$this->cnf['grouptree'];
+                $group_info['memberUid'] = $user; // User's DN is added to group's 'member' array
+                if (@ldap_mod_add($this->con,$group_name,$group_info)) {
+                    return true;
+                }
+            }
+           return null;
+    }
+     
+    function modifyUser($user, $changes) {                                                                                  
+      global $conf;
+      // sanity checks, user must already exist and there must be something to change                                       
+      if (($this->getUserData($user)) === false)  return false;
+      if (!is_array($changes) || !count($changes)) return true;                                                             
+      // update userinfo with new data, remembering to encrypt any password                                                 
+      foreach ($changes as $field => $value) {                                                                              
+        if ($field == 'pass') $userinfo['userPassword'] = auth_cryptPassword($value);
+        if ($field == 'name') $userinfo['sn'] = $value;
+        if ($field == 'user') $userinfo['cn'] = $value;
+        if ($field == 'mail') $userinfo['mail'] = $value;
+      }
+      if (strpos($this->cnf['usertree'], '%{user}')) {
+                $dn=str_replace ('%{user}', $user, $this->cnf['usertree']);
+            }
+      if(!empty($dn)) {
+            if (@ldap_mod_replace($this->con, $dn, $userinfo)) {
+               return true;
+           }
+      }                                                                                                                     
+      return false;                                                                                                          
+    }             
 }


*** end ***

Remark for FreeBSD: Create file 'patch-dokuwiki-ldap' with text above, put this file to /usr/ports/www/dokuwiki/files/ (cat > /usr/ports/www/dokuwiki/files/patch-dokuwiki-ldap) and install this port one way or another.
linkReply