]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Add nickname suggestion capability for use during autoregistration.
authorCraig Andrews <candrews@integralblue.com>
Fri, 15 Jan 2010 00:43:03 +0000 (19:43 -0500)
committerCraig Andrews <candrews@integralblue.com>
Fri, 15 Jan 2010 00:52:11 +0000 (19:52 -0500)
lib/authenticationplugin.php
plugins/LdapAuthentication/LdapAuthenticationPlugin.php

index de479a5768081b42648b60c219b6c1d1be51c3dc..17237086c4f025cf0cd6e876e5d577d8d731652a 100644 (file)
@@ -92,6 +92,19 @@ abstract class AuthenticationPlugin extends Plugin
         return false;
     }
 
+    /**
+    * Given a username, suggest what the nickname should be
+    * Used during autoregistration
+    * Useful if your usernames are ugly, and you want to suggest
+    * nice looking nicknames when users initially sign on
+    * @param username
+    * @return string nickname
+    */
+    function suggestNicknameForUsername($username)
+    {
+        return $username;
+    }
+
     //------------Below are the methods that connect StatusNet to the implementing Auth plugin------------\\
     function onInitializePlugin(){
         if(!isset($this->provider_name)){
@@ -108,10 +121,22 @@ abstract class AuthenticationPlugin extends Plugin
     function onAutoRegister($nickname, $provider_name, &$user)
     {
         if($provider_name == $this->provider_name && $this->autoregistration){
-            $user = $this->autoregister($nickname);
-            if($user){
-                User_username::register($user,$nickname,$this->provider_name);
-                return false;
+            $suggested_nickname = $this->suggestNicknameForUsername($nickname);
+            $test_user = User::staticGet('nickname', $suggested_nickname);
+            if($test_user) {
+                //someone already exists with the suggested nickname, so used the passed nickname
+                $suggested_nickname = $nickname;
+            }
+            $test_user = User::staticGet('nickname', $suggested_nickname);
+            if($test_user) {
+                //someone already exists with the suggested nickname
+                //not much else we can do
+            }else{
+                $user = $this->autoregister($suggested_nickname);
+                if($user){
+                    User_username::register($user,$nickname,$this->provider_name);
+                    return false;
+                }
             }
         }
     }
@@ -122,23 +147,30 @@ abstract class AuthenticationPlugin extends Plugin
         $user_username->username=$nickname;
         $user_username->provider_name=$this->provider_name;
         if($user_username->find() && $user_username->fetch()){
-            $username = $user_username->username;
-            $authenticated = $this->checkPassword($username, $password);
+            $authenticated = $this->checkPassword($user_username->username, $password);
             if($authenticated){
                 $authenticatedUser = User::staticGet('id', $user_username->user_id);
                 return false;
             }
         }else{
-            $user = User::staticGet('nickname', $nickname);
+            //$nickname is the username used to login
+            //$suggested_nickname is the nickname the auth provider suggests for that username
+            $suggested_nickname = $this->suggestNicknameForUsername($nickname);
+            $user = User::staticGet('nickname', $suggested_nickname);
             if($user){
-                //make sure a different provider isn't handling this nickname
+                //make sure this user isn't claimed
                 $user_username = new User_username();
-                $user_username->username=$nickname;
-                if(!$user_username->find()){
-                    //no other provider claims this username, so it's safe for us to handle it
+                $user_username->user_id=$user->id;
+                $we_can_handle = false;
+                if($user_username->find()){
+                    //either this provider, or another one, has already claimed this user
+                    //so we cannot. Let another plugin try.
+                    return;
+                }else{
+                    //no other provider claims this user, so it's safe for us to handle it
                     $authenticated = $this->checkPassword($nickname, $password);
                     if($authenticated){
-                        $authenticatedUser = User::staticGet('nickname', $nickname);
+                        $authenticatedUser = $user;
                         User_username::register($authenticatedUser,$nickname,$this->provider_name);
                         return false;
                     }
index eb3a05117a4c72e2440190fce1dd8f90c7ffa87d..1755033f178a4bed7e38d728d5b092904c86fc9f 100644 (file)
@@ -153,6 +153,22 @@ class LdapAuthenticationPlugin extends AuthenticationPlugin
 
         return false;
     }
+
+    function suggestNicknameForUsername($username)
+    {
+        $entry = $this->ldap_get_user($username, $this->attributes);
+        if(!$entry){
+            //this really shouldn't happen
+            return $username;
+        }else{
+            $nickname = $entry->getValue($this->attributes['nickname'],'single');
+            if($nickname){
+                return $nickname;
+            }else{
+                return $username;
+            }
+        }
+    }
     
     //---utility functions---//
     function ldap_get_config(){