mirror of https://gitee.com/bigwinds/arangodb
185 lines
4.3 KiB
Plaintext
185 lines
4.3 KiB
Plaintext
!CHAPTER The Users App
|
|
|
|
The users app provides a username-based user storage JavaScript API that can be used in other Foxx apps.
|
|
|
|
!SECTION JavaScript API: *userStorage*
|
|
|
|
This app exposes a user storage via a JavaScript API named *userStorage*.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
var userStorage = Foxx.requireApp('/_system/users').userStorage;
|
|
```
|
|
|
|
!SUBSECTION Exceptions
|
|
|
|
!SUBSUBSECTION User Not Found
|
|
|
|
Indicates a user could not be found in the database.
|
|
|
|
`new userStorage.errors.UserNotFound(userId)`
|
|
|
|
Thrown by the user storage's *delete* and *get* methods if passed a user ID that does not exist in the database.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
try {
|
|
userStorage.get(invalidUserId);
|
|
} catch(err) {
|
|
assertTrue(err instanceof userStorage.errors.UserNotFound);
|
|
}
|
|
```
|
|
|
|
!SUBSUBSECTION Username Not Available
|
|
|
|
Indicates a username is already in use.
|
|
|
|
`new userStorage.errors.UsernameNotAvailable(username)`
|
|
|
|
Thrown by the user storage's *create* method if passed a *userData* object with a *username* that is already in use.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
try {
|
|
userStorage.create('alreadyTaken', {some: 'data'});
|
|
} catch(err) {
|
|
assertTrue(err instanceof userStorage.errors.UsernameNotAvailable);
|
|
}
|
|
```
|
|
|
|
!SUBSECTION The user object
|
|
|
|
User objects are instances of a Foxx model with the following attributes:
|
|
|
|
* *user*: the user's unique *username*.
|
|
* *userData*: application-specific user data.
|
|
* *authData*: an arbitrary object used by authentication apps to store sensitive data. For password authentication this could be a hash, for third-party authentication services this could be information about the user's identity. This attribute should never be exposed to the user directly.
|
|
|
|
!SUBSECTION Create a user
|
|
|
|
Creates and saves a new instance of the user model.
|
|
|
|
`userStorage.create(username, userData)`
|
|
|
|
Throws *UsernameNotAvailable* if a user with the given username already exists.
|
|
|
|
*Parameter*
|
|
|
|
* *username*: an arbitrary string that will be used as the user's username
|
|
* *userData*: an arbitrary object that will be stored as the user's *userData* attribute when the model is saved to the database.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
var user = userStorage.create('malaclypse', {hair: 'fuzzy'});
|
|
assertEqual(user.get('userData').hair, 'fuzzy');
|
|
```
|
|
|
|
!SUBSECTION Fetch an existing user
|
|
|
|
There are two ways to fetch a user via the user storage API:
|
|
|
|
* resolving a *username* with the user storage's *resolve* method
|
|
* calling the user storage's *get* method with a user ID directly
|
|
|
|
!SUBSUBSECTION Resolve a *username*
|
|
|
|
Fetches a user with a given *username*.
|
|
|
|
`userStorage.resolve(username)`
|
|
|
|
If the username can not be resolved, a *UserNotFound* exception will be thrown instead.
|
|
|
|
*Parameter*
|
|
|
|
* *username*: an arbitrary string matching the username of the user you are trying to fetch.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
var user = userStorage.resolve('malaclypse');
|
|
assertEqual(user.user, 'malaclypse');
|
|
```
|
|
|
|
!SUBSUBSECTION Resolve a user ID directly
|
|
|
|
Fetches a user with a given ID.
|
|
|
|
`userStorage.get(userId)`
|
|
|
|
Attempts to fetch the user with the given ID from the database. If the user does not exist, a *UserNotFound* exception will be thrown instead.
|
|
|
|
*Parameter*
|
|
|
|
* *userId*: a user *_key*.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
var user = userStorage.get(userId);
|
|
assertEqual(user.get('_key'), userId);
|
|
```
|
|
|
|
!SUBSECTION Delete a user
|
|
|
|
There are two ways to delete a user from the database:
|
|
|
|
* calling the user storage's *delete* method with a user ID directly
|
|
* telling a user to delete itself
|
|
|
|
!SUBSUBSECTION Delete a user by its ID
|
|
|
|
Delete a user with a given ID.
|
|
|
|
`userStorage.delete(userId)`
|
|
|
|
Attempts to delete the user with the given user ID from the database. If the user does not exist, a *UserNotFound* exception will be thrown. The method always returns *null*.
|
|
|
|
*Parameter*
|
|
|
|
* *userId*: a user *_key*.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
userStorage.delete(userId);
|
|
```
|
|
|
|
!SUBSUBSECTION Tell a user to delete itself
|
|
|
|
Delete a user from the database.
|
|
|
|
`user.delete()`
|
|
|
|
Attempts to delete the user from the database.
|
|
|
|
Returns *true* if the user was deleted successfully.
|
|
|
|
Returns *false* if the user already didn't exist.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
var user = userStorage.get(userId);
|
|
user.delete();
|
|
```
|
|
|
|
!SUBSECTION Save a user
|
|
|
|
Save a user to the database.
|
|
|
|
`user.save()`
|
|
|
|
In order to commit changes made to the user in your code, you need to call this method.
|
|
|
|
@EXAMPLES
|
|
|
|
```js
|
|
var userData = user.get('userData');
|
|
userData.counter++;
|
|
user.save();
|
|
```
|