From 266a88b9c769ceb9fbb5909c1d1a033cd3421adb Mon Sep 17 00:00:00 2001 From: jonl Date: Mon, 16 Jan 2012 21:01:35 +0100 Subject: [PATCH] Added and implemented user/change_pw, user/update and user/change_email, removed a bug from user/get --- api-nodes.txt | 14 +++++- lib/user_functions.php | 57 ++++++++++++++++++++- user.php | 112 +++++++++++++++++++++++++++++++++++++++-- 3 files changed, 178 insertions(+), 5 deletions(-) diff --git a/api-nodes.txt b/api-nodes.txt index b01058a..9dc240a 100644 --- a/api-nodes.txt +++ b/api-nodes.txt @@ -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 diff --git a/lib/user_functions.php b/lib/user_functions.php index 1808437..e618d4c 100644 --- a/lib/user_functions.php +++ b/lib/user_functions.php @@ -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']; diff --git a/user.php b/user.php index 49e8588..2830a3e 100644 --- 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' ) ); -- 2.39.2