Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Add the possibility to change the user_type through the API #179

Open
wants to merge 1 commit into
base: test
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,34 @@ function kms_service_resources_resource() {
],
],

'updateUserType' => [
'file' => [
'type' => 'inc',
'module' => 'kms_services_resources',
'name' => 'kms_services_resources.services',
],
'help' => t('Update the user type by using the user ID'),
'access callback' => '_kms_services_resources_user_resource_access',
'access arguments append' => TRUE,
'callback' => '_kms_service_resources_update_user_type',
'args' => [
[
'name' => 'id',
'type' => 'string',
'description' => t('The id for the user'),
'source' => ['data' => 'id'],
'optional' => FALSE,
],
[
'name' => 'type',
'type' => 'string',
'description' => t('The type to set the user as'),
'source' => ['data' => 'type'],
'optional' => FALSE,
],
],
],

],
],
];
Expand Down Expand Up @@ -1864,5 +1892,51 @@ function _kms_service_resources_get_user_by_uuid($uuid) {
return services_error(t('User could not be loaded.'), 422);
}

return $user;
}

/**
* Update the field_user_type for a user with the given $user_type_id.
*
* @param $id
* User ID
* @param $user_type_tid
* User type Term ID.
*
* @return mixed
* @throws \Exception
* @throws \ServicesException
*/
function _kms_service_resources_update_user_type($id, $user_type_tid) {

$user = user_load($id);
if (!$user) {
return services_error(t('User could not be loaded.'), 404);
}

// Do we already have an entry ?
$user_type_entity = db_select('field_data_field_user_type', 'fdfut')
->fields('fdfut', ['entity_type'])
->condition('entity_type', 'user')
->condition('entity_id', $id)
->condition('field_user_type_tid', $user_type_tid)
->execute()
->fetchAssoc();

if (!$user_type_entity) {
db_insert('field_data_field_user_type')
->fields(array(
'entity_type' => 'user',
'bundle' => 'user',
'deleted' => 0,
'entity_id' => $id,
'revision_id' => $id,
'language' => LANGUAGE_NONE,
'delta' => 0,
'field_user_type_tid' => intval($user_type_tid)
))
->execute();
}

return $user;
}