diff --git a/UnitTests/Makefile.unittests b/UnitTests/Makefile.unittests index c4476a9b93..951a6f278c 100755 --- a/UnitTests/Makefile.unittests +++ b/UnitTests/Makefile.unittests @@ -434,7 +434,7 @@ unittests-import: ################################################################################ -### @brief DATAFILE DEBUGGER TESTS +### @brief DATAFILE DEBUGGER TESTS ### ### this test just checks whether the dfdb starts. ### we will send an EOT signal to the dfdb process to stop it right away. diff --git a/js/common/bootstrap/errors.js b/js/common/bootstrap/errors.js index 50f41a28a5..82aa5492c3 100644 --- a/js/common/bootstrap/errors.js +++ b/js/common/bootstrap/errors.js @@ -111,6 +111,10 @@ "ERROR_TRANSACTION_NESTED" : { "code" : 1652, "message" : "nested transactions detected" }, "ERROR_TRANSACTION_INTERNAL" : { "code" : 1653, "message" : "internal transaction error" }, "ERROR_TRANSACTION_UNREGISTERED_COLLECTION" : { "code" : 1654, "message" : "unregistered collection used in transaction" }, + "ERROR_USER_INVALID_NAME" : { "code" : 1700, "message" : "invalid user name" }, + "ERROR_USER_INVALID_PASSWORD" : { "code" : 1701, "message" : "invalid password" }, + "ERROR_USER_DUPLICATE" : { "code" : 1702, "message" : "duplicate user" }, + "ERROR_USER_NOT_FOUND" : { "code" : 1703, "message" : "user not found" }, "ERROR_KEYVALUE_INVALID_KEY" : { "code" : 1800, "message" : "invalid key declaration" }, "ERROR_KEYVALUE_KEY_EXISTS" : { "code" : 1801, "message" : "key already exists" }, "ERROR_KEYVALUE_KEY_NOT_FOUND" : { "code" : 1802, "message" : "key not found" }, diff --git a/js/common/modules/org/arangodb/users.js b/js/common/modules/org/arangodb/users.js index 391d138a53..5fc346f70a 100644 --- a/js/common/modules/org/arangodb/users.js +++ b/js/common/modules/org/arangodb/users.js @@ -32,10 +32,9 @@ var internal = require("internal"); // OK: encodePassword, reloadAuth var encodePassword = internal.encodePassword; var reloadAuth = internal.reloadAuth; - var arangodb = require("org/arangodb"); - var db = arangodb.db; +var ArangoError = require("org/arangodb/arango-error").ArangoError; // ----------------------------------------------------------------------------- // --SECTION-- module "org/arangodb/users" @@ -55,8 +54,12 @@ var db = arangodb.db; //////////////////////////////////////////////////////////////////////////////// var validateName = function (username) { - if (typeof username !== 'string' || ! username.match(/^[a-zA-Z0-9\-_]+$/)) { - throw "username must be a string"; + if (typeof username !== 'string' || username === '') { + var err = new ArangoError(); + err.errorNum = arangodb.errors.ERROR_USER_INVALID_NAME.code; + err.errorMessage = arangodb.errors.ERROR_USER_INVALID_NAME.message; + + throw err; } }; @@ -66,7 +69,11 @@ var validateName = function (username) { var validatePassword = function (passwd) { if (typeof passwd !== 'string') { - throw "password must be a string"; + var err = new ArangoError(); + err.errorNum = arangodb.errors.ERROR_USER_INVALID_PASSWORD.code; + err.errorMessage = arangodb.errors.ERROR_USER_INVALID_PASSWORD.message; + + throw err; } }; @@ -78,7 +85,11 @@ var getStorage = function () { var users = db._collection("_users"); if (users === null) { - throw "collection _users does not exist."; + var err = new ArangoError(); + err.errorNum = arangodb.errors.ERROR_ARANGO_COLLECTION_NOT_FOUND.code; + err.errorMessage = "collection _users not found"; + + throw err; } return users; @@ -124,6 +135,10 @@ var getStorage = function () { //////////////////////////////////////////////////////////////////////////////// exports.save = function (username, passwd) { + if (passwd === null || passwd === undefined) { + passwd = ""; + } + // validate input validateName(username); validatePassword(passwd); @@ -135,8 +150,12 @@ exports.save = function (username, passwd) { var hash = encodePassword(passwd); return users.save({ user: username, password: hash, active: true }); } + + var err = new ArangoError(); + err.errorNum = arangodb.errors.ERROR_USER_DUPLICATE.code; + err.errorMessage = arangodb.errors.ERROR_USER_DUPLICATE.message; - throw "cannot create user: user already exists."; + throw err; }; //////////////////////////////////////////////////////////////////////////////// @@ -169,6 +188,10 @@ exports.save = function (username, passwd) { exports.replace = exports.update = function (username, passwd) { + if (passwd === null || passwd === undefined) { + passwd = ""; + } + // validate input validateName(username); validatePassword(passwd); @@ -177,7 +200,11 @@ exports.update = function (username, passwd) { var user = users.firstExample({ user: username }); if (user === null) { - throw "cannot update user: user does not exist."; + var err = new ArangoError(); + err.errorNum = arangodb.errors.ERROR_USER_NOT_FOUND.code; + err.errorMessage = arangodb.errors.ERROR_USER_NOT_FOUND.message; + + throw err; } var hash = encodePassword(passwd); @@ -217,7 +244,11 @@ exports.remove = function (username) { var user = users.firstExample({ user: username }); if (user === null) { - throw "cannot delete: user does not exist."; + var err = new ArangoError(); + err.errorNum = arangodb.errors.ERROR_USER_NOT_FOUND.code; + err.errorMessage = arangodb.errors.ERROR_USER_NOT_FOUND.message; + + throw err; } return users.remove(user._id); diff --git a/js/common/tests/shell-users.js b/js/common/tests/shell-users.js index f0fa2c99f0..0fa49566c8 100644 --- a/js/common/tests/shell-users.js +++ b/js/common/tests/shell-users.js @@ -53,9 +53,15 @@ function UsersSuite () { try { users.remove(username); } - catch (err) { + catch (e1) { } } + + try { + users.remove("hackers@arangodb.org"); + } + catch (e2) { + } }; return { @@ -81,7 +87,7 @@ function UsersSuite () { //////////////////////////////////////////////////////////////////////////////// testInvalidNames : function () { - var usernames = [ " ", "", "******", "#", "'", "\"", "d d", "d " ]; + var usernames = [ null, 1, 2, 3, [ ], { }, false, true, "" ]; for (var i = 0; i < usernames.length; ++i) { var username = usernames[i]; @@ -92,6 +98,7 @@ function UsersSuite () { fail(); } catch (err) { + assertEqual(ERRORS.ERROR_USER_INVALID_NAME.code, err.errorNum); } } }, @@ -126,9 +133,45 @@ function UsersSuite () { fail(); } catch (err) { + assertEqual(ERRORS.ERROR_USER_DUPLICATE.code, err.errorNum); } }, +//////////////////////////////////////////////////////////////////////////////// +/// @brief test save no passwd +//////////////////////////////////////////////////////////////////////////////// + + testSavePasswdEmpty : function () { + var username = "users-1"; + var passwd = ""; + + users.save(username, passwd); + assertEqual(username, c.firstExample({ user: username }).user); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test save no passwd +//////////////////////////////////////////////////////////////////////////////// + + testSavePasswdMissing : function () { + var username = "users-1"; + + users.save(username); + assertEqual(username, c.firstExample({ user: username }).user); + }, + +//////////////////////////////////////////////////////////////////////////////// +/// @brief test save w/ email address pattern +//////////////////////////////////////////////////////////////////////////////// + + testSaveWithEmailAddressName : function () { + var username = "hackers@arangodb.org"; + var passwd = "arangodb-loves-you"; + + users.save(username, passwd); + assertEqual(username, c.firstExample({ user: username }).user); + }, + //////////////////////////////////////////////////////////////////////////////// /// @brief test update method //////////////////////////////////////////////////////////////////////////////// @@ -156,6 +199,7 @@ function UsersSuite () { fail(); } catch (err) { + assertEqual(ERRORS.ERROR_USER_NOT_FOUND.code, err.errorNum); } }, @@ -184,6 +228,7 @@ function UsersSuite () { fail(); } catch (err) { + assertEqual(ERRORS.ERROR_USER_NOT_FOUND.code, err.errorNum); } }, diff --git a/lib/BasicsC/errors.dat b/lib/BasicsC/errors.dat index 1ca1908b34..a27491d2a7 100755 --- a/lib/BasicsC/errors.dat +++ b/lib/BasicsC/errors.dat @@ -152,6 +152,15 @@ ERROR_TRANSACTION_NESTED,1652,"nested transactions detected","Will be raised whe ERROR_TRANSACTION_INTERNAL,1653,"internal transaction error","Will be raised when a wrong usage of transactions is detected. this is an internal error and indicates a bug in ArangoDB." ERROR_TRANSACTION_UNREGISTERED_COLLECTION,1654,"unregistered collection used in transaction","Will be raised when a collection is used in the middle of a transaction but was not registered at transaction start." +################################################################################ +## User management +################################################################################ + +ERROR_USER_INVALID_NAME,1700,"invalid user name","Will be raised when an invalid user name is used" +ERROR_USER_INVALID_PASSWORD,1701,"invalid password","Will be raised when an invalid password is used" +ERROR_USER_DUPLICATE,1702,"duplicate user","Will be raised when a user name already exists" +ERROR_USER_NOT_FOUND,1703,"user not found","Will be raised when a user name is updated that does not exist" + ################################################################################ ## Key value access ################################################################################ diff --git a/lib/BasicsC/vector.c b/lib/BasicsC/vector.c index 32e93324e5..a6b43615da 100755 --- a/lib/BasicsC/vector.c +++ b/lib/BasicsC/vector.c @@ -62,7 +62,7 @@ //////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////// -/// @brief initialises a vector +/// @brief initialises a vector //////////////////////////////////////////////////////////////////////////////// void TRI_InitVector (TRI_vector_t* vector, TRI_memory_zone_t* zone, size_t elementSize) { diff --git a/lib/BasicsC/voc-errors.c b/lib/BasicsC/voc-errors.c index 19c6d4abc7..4fdda30535 100644 --- a/lib/BasicsC/voc-errors.c +++ b/lib/BasicsC/voc-errors.c @@ -107,6 +107,10 @@ void TRI_InitialiseErrorMessages (void) { REG_ERROR(ERROR_TRANSACTION_NESTED, "nested transactions detected"); REG_ERROR(ERROR_TRANSACTION_INTERNAL, "internal transaction error"); REG_ERROR(ERROR_TRANSACTION_UNREGISTERED_COLLECTION, "unregistered collection used in transaction"); + REG_ERROR(ERROR_USER_INVALID_NAME, "invalid user name"); + REG_ERROR(ERROR_USER_INVALID_PASSWORD, "invalid password"); + REG_ERROR(ERROR_USER_DUPLICATE, "duplicate user"); + REG_ERROR(ERROR_USER_NOT_FOUND, "user not found"); REG_ERROR(ERROR_KEYVALUE_INVALID_KEY, "invalid key declaration"); REG_ERROR(ERROR_KEYVALUE_KEY_EXISTS, "key already exists"); REG_ERROR(ERROR_KEYVALUE_KEY_NOT_FOUND, "key not found"); diff --git a/lib/BasicsC/voc-errors.h b/lib/BasicsC/voc-errors.h index 38635e5ca8..81a50a33cd 100644 --- a/lib/BasicsC/voc-errors.h +++ b/lib/BasicsC/voc-errors.h @@ -237,6 +237,14 @@ extern "C" { /// - 1654: @LIT{unregistered collection used in transaction} /// Will be raised when a collection is used in the middle of a transaction /// but was not registered at transaction start. +/// - 1700: @LIT{invalid user name} +/// Will be raised when an invalid user name is used +/// - 1701: @LIT{invalid password} +/// Will be raised when an invalid password is used +/// - 1702: @LIT{duplicate user} +/// Will be raised when a user name already exists +/// - 1703: @LIT{user not found} +/// Will be raised when a user name is updated that does not exist /// - 1800: @LIT{invalid key declaration} /// Will be raised when an invalid key specification is passed to the server /// - 1801: @LIT{key already exists} @@ -1342,6 +1350,46 @@ void TRI_InitialiseErrorMessages (void); #define TRI_ERROR_TRANSACTION_UNREGISTERED_COLLECTION (1654) +//////////////////////////////////////////////////////////////////////////////// +/// @brief 1700: ERROR_USER_INVALID_NAME +/// +/// invalid user name +/// +/// Will be raised when an invalid user name is used +//////////////////////////////////////////////////////////////////////////////// + +#define TRI_ERROR_USER_INVALID_NAME (1700) + +//////////////////////////////////////////////////////////////////////////////// +/// @brief 1701: ERROR_USER_INVALID_PASSWORD +/// +/// invalid password +/// +/// Will be raised when an invalid password is used +//////////////////////////////////////////////////////////////////////////////// + +#define TRI_ERROR_USER_INVALID_PASSWORD (1701) + +//////////////////////////////////////////////////////////////////////////////// +/// @brief 1702: ERROR_USER_DUPLICATE +/// +/// duplicate user +/// +/// Will be raised when a user name already exists +//////////////////////////////////////////////////////////////////////////////// + +#define TRI_ERROR_USER_DUPLICATE (1702) + +//////////////////////////////////////////////////////////////////////////////// +/// @brief 1703: ERROR_USER_NOT_FOUND +/// +/// user not found +/// +/// Will be raised when a user name is updated that does not exist +//////////////////////////////////////////////////////////////////////////////// + +#define TRI_ERROR_USER_NOT_FOUND (1703) + //////////////////////////////////////////////////////////////////////////////// /// @brief 1800: ERROR_KEYVALUE_INVALID_KEY ///