From e0db22cc0741abaebe44e245f2da6ca1a34f7cac Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Sun, 6 May 2012 18:04:31 -0400 Subject: [PATCH] Provide feedback when user creation fails --- lib/user.php | 6 +++--- settings/ajax/createuser.php | 8 ++++---- settings/js/users.js | 4 +++- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/user.php b/lib/user.php index 8b887559df3..816caff8dd8 100644 --- a/lib/user.php +++ b/lib/user.php @@ -117,15 +117,15 @@ class OC_User { // Check the name for bad characters // Allowed are: "a-z", "A-Z", "0-9" and "_.@-" if( preg_match( '/[^a-zA-Z0-9 _\.@\-]/', $uid )){ - return false; + throw new Exception('Only the following characters are allowed in a username: "a-z", "A-Z", "0-9", and "_.@-"'); } // No empty username if(trim($uid) == ''){ - return false; + throw new Exception('A valid username must be provided'); } // Check if user already exists if( self::userExists($uid) ){ - return false; + throw new Exception('The username is already being used'); } diff --git a/settings/ajax/createuser.php b/settings/ajax/createuser.php index 1ed53efcf06..6714711bc87 100644 --- a/settings/ajax/createuser.php +++ b/settings/ajax/createuser.php @@ -23,7 +23,8 @@ if( in_array( $username, OC_User::getUsers())){ } // Return Success story -if( OC_User::createUser( $username, $password )){ +try { + OC_User::createUser($username, $password); foreach( $groups as $i ){ if(!OC_Group::groupExists($i)){ OC_Group::createGroup($i); @@ -31,9 +32,8 @@ if( OC_User::createUser( $username, $password )){ OC_Group::addToGroup( $username, $i ); } OC_JSON::success(array("data" => array( "username" => $username, "groups" => implode( ", ", OC_Group::getUserGroups( $username ))))); -} -else{ - OC_JSON::error(array("data" => array( "message" => "Unable to add user" ))); +} catch (Exception $exception) { + OC_JSON::error(array("data" => array( "message" => $exception->getMessage()))); } ?> diff --git a/settings/js/users.js b/settings/js/users.js index eed93d3b303..e6eee4ce4fa 100644 --- a/settings/js/users.js +++ b/settings/js/users.js @@ -158,10 +158,11 @@ $(document).ready(function(){ event.preventDefault(); var username=$('#newusername').val(); if($('#content table tbody tr').filterAttr('data-uid',username).length>0){ + OC.dialogs.alert('The username is already being used', 'Error creating user'); return; } if($.trim(username) == '') { - alert('Please provide a username!'); + OC.dialogs.alert('A valid username must be provided', 'Error creating user'); return false; } var password=$('#newuserpassword').val(); @@ -177,6 +178,7 @@ $(document).ready(function(){ function(result){ if(result.status!='success'){ tr.remove(); + OC.dialogs.alert(result.data.message, 'Error creating user'); } } ); -- 2.39.5