]> git.defcon.no Git - hermes/commitdiff
Added and implemented user/change_pw, user/update and user/change_email, removed...
authorjonl <jonl@sippbx.hig.no>
Mon, 16 Jan 2012 20:01:35 +0000 (21:01 +0100)
committerjonl <jonl@sippbx.hig.no>
Mon, 16 Jan 2012 20:01:35 +0000 (21:01 +0100)
api-nodes.txt
lib/user_functions.php
user.php

index b01058a9f5946e489845e0820e465e329bd685ab..9dc240a113608bfbc1e54ca4cdb50b248649b05b 100644 (file)
@@ -60,10 +60,22 @@ user/change_pw?user=foo@bar.bz&password=baz
 
 user/update?username=foo&domain=bar
 user/update?user=foo@bar.bz
-  optionals: &displayname=baz&dialplan=(.*)&linetext=lalala
+  optionals: &displayname=baz&dialplan=(.*)&linetext=lalala&email=foo@bar.baz
        Not implemented! Will only be implemented for Local users.
        Remote users will have to be removed, and re-added.
 
+user/change_email?username=foo&domain=bar&email=user@example.com
+user/change_email?user=foo@bar.bz&email=user@example.com
+       Changes the email address for the given user, returns 'ok' with
+       'user' set to the requested username and 'email' set to the
+       email address.  This only applies to kamailio local users.
+
+       Returns 'failed' with 'cause' = 'nonexistant' if user does not exist.
+       
+       Returns 'failed' with 'cause' = 'dbfail' if the database request
+       failed. Note that this may be because the given password was identical
+       to the old.
+
 
 user/remove?username=foo&domain=bar
 user/remove?user=foo@bar.bz
index 180843717388df5547d7883c6e14c2d8bd660a2d..e618d4c5bdb5bd83f143dc8a4ecac65f8ebc0abd 100644 (file)
@@ -102,6 +102,25 @@ function update_kamailio_pw ( $username, $domain, $password )
        return true;
 }
 
+function update_kamailio_email ( $username, $domain, $email )
+{
+       global $config;
+
+       if ( ! ($username && $domain && $email))
+               return false;
+
+       $query = sprintf("UPDATE %s SET email_address = '%s' WHERE username = '%s' AND domain = '%s'",
+               $config['kamailio_subscriber_table'],
+               sql_clean($email),
+               sql_clean($username),
+               sql_clean($domain)
+       
+       );
+       if ( sql_dbexec_rows( $config['kamailio_db'], $query) != 1 ) return false;
+       return true;
+}
+
+
 function add_provision_user( $username, $password, $domain, $authid, $registrar, $r_port, $proxy, $p_port, $displayname, $dialplan, $linetext )
 {      global $config;
 
@@ -157,6 +176,42 @@ function delete_provision_user( $username, $domain )
        return true;
 }
 
+function update_provision_data ( $param, $username, $domain, $data )
+{
+       global $config;
+
+       if (! (
+               $param == "displayname" ||
+               $param == "dialplan" ||
+               $param == "linetext" ||
+               $param == "registrar" ||
+               $param == "r_port" ||
+               $param == "proxy" ||
+               $param == "p_port"
+               ) ) return -1;
+
+
+       if ( ! ($username && $domain))
+               return -1;
+
+       if ( ! is_provision_user( $username, $domain ) )
+               return -2;
+
+       $query = sprintf("UPDATE %s SET %s = '%s' WHERE username = '%s' AND domain = '%s'",
+               $config['provision_users_table'],
+               sql_clean($param),
+               sql_clean($data),
+               sql_clean($username),
+               sql_clean($domain)
+       
+       );
+       $res = sql_dbexec_rows( $config['provision_db'], $query);
+
+       if ( $res < 0 ) return -2;
+       if ( $res > 1 ) return -2;
+       return  $res;
+}
+
 function update_provision_pw ( $username, $domain, $password )
 {
        global $config;
@@ -241,7 +296,7 @@ function get_userdata( $username, $domain )
        $user['linetext']    = $provision_data['linetext'];
        if ( $type == 'local' )
        {
-               $user['email'] = $kamailio_data['email'];
+               $user['email'] = $kamailio_data['email_address'];
                $user['ha1']   = $kamailio_data['ha1'];
                $user['ha1b']  = $kamailio_data['ha1b'];
                $user['rpid']  = $kamailio_data['rpid'];
index 49e858898a987b8bbd4e8ace050c027237918bb6..2830a3ee1040d0b1869c3307778f718c02792304 100644 (file)
--- a/user.php
+++ b/user.php
@@ -431,7 +431,7 @@ if ( !$config['sql_link'] )
                                if ( is_provision_user( $username, $domain ) )
                                {
                                        // Update provisioning password
-                                       if (! update_provision_pw( $username, $domain, $password ) )
+                                       if ( update_provision_pw( $username, $domain, $password ) < 0 )
                                        {
                                                print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update provisioning password' ) );
                                                break;
@@ -441,7 +441,7 @@ if ( !$config['sql_link'] )
                                if ( is_kamailio_subscriber( $username, $domain ) )
                                {
                                        // Update kamailio password
-                                       if (! update_kamailio_pw( $username, $domain, $password ) )
+                                       if ( update_kamailio_pw( $username, $domain, $password ) < 0 )
                                        {
                                                print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio password' ) );
                                                break;
@@ -454,6 +454,60 @@ if ( !$config['sql_link'] )
                                print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
                        break;
 
+
+               case "/change_email":
+                       /*
+                       Required parameters should be...
+                               ( username & domain ) | user
+                               email
+                       */
+                       if (    array_key_exists('email', $_GET) &&
+                               ( ( array_key_exists( 'username', $_GET) && array_key_exists( 'domain', $_GET ) ) 
+                                       || array_key_exists('user', $_GET) ))
+
+                       {
+                               $username = "";
+                               $domain = "";
+                               if ( array_key_exists('username', $_GET) )
+                               {
+                                       $username = $_GET['username'];
+                                       $domain = $_GET['domain'];
+                               }
+                               else
+                               {
+                                       $user = split_sipaddress($_GET['user']);
+                                       if ( !$user )
+                                       {
+                                               print json_encode ( array( 'response' => 'failed', 'cause' => 'invalid', 'detail' => 'Invalid SIP address') );
+                                               break;
+                                       }
+                                       list ( $username, $domain ) = $user;
+                               }
+                               $email = $_GET['email'];
+
+                               // Check for user in kamailio
+                               if ( is_kamailio_subscriber( $username, $domain ) )
+                               {
+                                       // Update kamailio email
+                                       if ( update_kamailio_email( $username, $domain, $email ) < 0 )
+                                       {
+                                               print json_encode( array( 'response' => 'failed', 'cause' => 'dbfail', 'detail' => 'Failed to update kamailio email' ) );
+                                               break;
+                                       }
+                               }
+                               print json_encode( array ( 'response' => 'ok', 'user' => $username.'@'.$domain, 'email' => $email));
+                               break;
+                       }
+                       else 
+                               print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );
+                       break;
+
+
+
+
+
+
+
                case "/update":
                        /*
                        Required parameters should be...
@@ -488,7 +542,59 @@ if ( !$config['sql_link'] )
                                        }
                                        list ( $username, $domain ) = $user;
                                }
-                               print json_encode ( array( 'response' => 'failed', 'cause' => 'notimplemented', 'detail' => 'Requested feature valid, but not implemented' ) );
+                               if ( ! is_provision_user ( $username,  $domain ) )
+                               {
+                                       print json_encode( array ( 'response' => 'failed', 'cause' => 'nonexistant', 'detail' => '' . $username . '@' . $domain . ' does not exist.'));
+                                       break;
+                               }
+                               $updated = array();
+                               $failed = array();
+                               $error = 1;
+                               $result_text = "";
+                               $params = array('displayname', 'dialplan', 'linetext', 'registrar', 'r_port', 'proxy', 'p_port');
+                               foreach ( $params as $p )
+                               {
+                                       if ( array_key_exists($p, $_GET ) )
+                                       {
+                                               $data = $_GET[$p];
+                                               $t = update_provision_data($p, $username, $domain, $data);
+                                               if ( $t != true )
+                                               {
+                                                       $error = $t;
+                                                       array_push($failed, $p);
+                                               }
+                                               else
+                                               {
+                                                       array_push( $updated, $p);
+                                               }
+                                       }
+                               }
+                               $res = array();
+                               if ( ( $error == 1 ) || ( $error == 0 ) )
+                               {
+                                       $res['response'] = 'ok';
+                                       $res['skipped'] = $failed;
+                               }
+                               else if ( $error == -1 )
+                               {
+                                       $res['response'] = 'failed';
+                                       $res['cause'] = 'param';
+                                       $res['detail'] = 'Invalid parameters';
+                                       $res['failed'] = $failed;
+                               }
+                               else if ( $error == -2 )
+                               {
+                                       $res['response'] = 'failed';
+                                       $res['cause'] = 'dbfail';
+                                       $res['detail'] = 'Database failure';
+                                       $res['failed'] = $failed;
+                               }
+                               else
+                                       $res['response'] = 'error'; // Wait, what?
+       
+                               $res['updated'] = $updated;
+
+                               print json_encode ( $res );
                        }
                        else 
                                print json_encode( array( 'response' => 'invalid', 'cause' => 'parameters' ) );