user_service.inc in Services 7
Same filename and directory in other branches
@author Services Dev Team
Link general user functionalities to services module.
File
services/user_service/user_service.incView source
<?php
/**
* @author Services Dev Team
* @file
* Link general user functionalities to services module.
*/
/**
* Delete an user.
*
* @param $uid
* Number. The user ID.
*/
function user_service_delete($uid) {
$account = user_load($uid);
if (empty($account)) {
return services_error(t('There is no user with such ID.'), 404);
}
user_delete($account, $uid);
// Everything went right.
return TRUE;
}
/**
* Check if the user is allowed to delete the user.
*
* @param $uid
* Number. The user ID.
*/
function user_service_delete_access($uid) {
global $user;
return $user->uid == $uid && user_access('delete own user') || $user->uid != $uid && user_access('delete any user');
}
/**
* Get user details.
*
* @param $uid
* Number. The user ID.
*/
function user_service_get($uid) {
$account = user_load($uid);
if (empty($account)) {
return services_error(t('There is no user with such ID.'), 404);
}
// Everything went right.
return $account;
}
/**
* Check if the user is allowed to get the user data.
*
* @param $uid
* Number. The user ID.
*/
function user_service_get_access($uid) {
global $user;
return $user->uid == $uid && user_access('get own user data') || $user->uid != $uid && user_access('get any user data');
}
/**
* Login a user
*
* @param $username
* String. The username.
* @param $password
* String. The user password.
*/
function user_service_login($username, $password) {
global $user;
if ($user->uid) {
// user is already logged in
return services_error(t('Already logged in as !user.', array(
'!user' => $user->name,
)), 406);
}
$user = user_authenticate(array(
'name' => $username,
'pass' => $password,
));
if ($user->uid) {
// Regenerate the session ID to prevent against session fixation attacks.
sess_regenerate();
module_invoke_all('user', 'login', NULL, $user);
$return = new stdClass();
$return->sessid = session_id();
$return->user = $user;
return $return;
}
session_destroy();
return services_error(t('Wrong username or password.'), 401);
}
/**
* Logout user
*/
function user_service_logout() {
global $user;
if (!$user->uid) {
// User is not logged in
return services_error(t('User is not logged in.'), 406);
}
watchdog('user', 'Session closed for %name.', array(
'%name' => theme('placeholder', $user->name),
));
// Destroy the current session:
session_destroy();
module_invoke_all('user', 'logout', NULL, $user);
// Load the anonymous user
$user = drupal_anonymous_user();
return TRUE;
}
/**
* Save user details.
*
* @param $user_data
* Object. The user object with all user data.
*/
function user_service_save($account) {
// if uid is present then update, otherwise insert
$update = user_load($account['uid']);
$account = isset($update->uid) ? user_save($update, $account) : user_save('', $account);
if (!$account) {
return services_error(t('Error on saving the user.'), 500);
}
// Everything went right.
// Return the user ID
return $account->uid;
}
/**
* Check if the user is allowed to get the user data.
*
* @param $uid
* Number. The user ID.
*/
function user_service_save_access($account) {
global $user;
return empty($account['uid']) && user_access('create new users') || $user->uid == $account['uid'] && user_access('update own user data') || $user->uid != $account['uid'] && user_access('update any user data');
}
Functions
Name | Description |
---|---|
user_service_delete | Delete an user. |
user_service_delete_access | Check if the user is allowed to delete the user. |
user_service_get | Get user details. |
user_service_get_access | Check if the user is allowed to get the user data. |
user_service_login | Login a user |
user_service_logout | Logout user |
user_service_save | Save user details. |
user_service_save_access | Check if the user is allowed to get the user data. |