GraphQL: clean code for create user #2644

Remove code from webservices
pull/2818/head
Angel Fernando Quiroz Campos 6 years ago
parent a9ee66700a
commit da1df04a50
  1. 122
      main/inc/lib/webservices/Rest.php
  2. 4
      main/webservices/api/v2.php
  3. 83
      main/webservices/soap_user.php
  4. 197
      main/webservices/webservice_user.php
  5. 8
      src/GraphQlBundle/Map/MutationMap.php
  6. 6
      src/GraphQlBundle/Resources/config/schema.types.graphql

@ -1149,128 +1149,6 @@ class Rest extends WebService
return $results;
}
/**
* @param $user_param
*
* @return array
*/
public function addUser($user_param)
{
$results = [];
$orig_user_id_value = [];
$userManager = UserManager::getManager();
$firstName = $user_param['firstname'];
$lastName = $user_param['lastname'];
$status = $user_param['status'];
$email = $user_param['email'];
$loginName = $user_param['loginname'];
$password = $user_param['password'];
$official_code = '';
$language = '';
$phone = '';
$picture_uri = '';
$auth_source = PLATFORM_AUTH_SOURCE;
$expiration_date = '';
$active = 1;
$hr_dept_id = 0;
$extra = null;
$original_user_id_name = $user_param['original_user_id_name'];
$original_user_id_value = $user_param['original_user_id_value'];
$orig_user_id_value[] = $user_param['original_user_id_value'];
$extra_list = $user_param['extra'];
if (!empty($user_param['language'])) {
$language = $user_param['language'];
}
if (!empty($user_param['phone'])) {
$phone = $user_param['phone'];
}
if (!empty($user_param['expiration_date'])) {
$expiration_date = $user_param['expiration_date'];
}
// Default language.
if (empty($language)) {
$language = api_get_setting('platformLanguage');
}
// First check wether the login already exists.
if (!UserManager::is_username_available($loginName)) {
$results[] = 0;
}
$userId = UserManager::create_user(
$firstName,
$lastName,
$status,
$email,
$loginName,
$password,
$official_code,
$language,
$phone,
$picture_uri,
$auth_source,
$expiration_date,
$active,
$hr_dept_id
);
if ($userId) {
if (api_is_multiple_url_enabled()) {
if (api_get_current_access_url_id() != -1) {
UrlManager::add_user_to_url(
$userId,
api_get_current_access_url_id()
);
} else {
UrlManager::add_user_to_url($userId, 1);
}
} else {
// We add by default the access_url_user table with access_url_id = 1
UrlManager::add_user_to_url($userId, 1);
}
// Save new field label into user_field table.
UserManager::create_extra_field(
$original_user_id_name,
1,
$original_user_id_name,
''
);
// Save the external system's id into user_field_value table.
UserManager::update_extra_field_value(
$userId,
$original_user_id_name,
$original_user_id_value
);
if (is_array($extra_list) && count($extra_list) > 0) {
foreach ($extra_list as $extra) {
$extra_field_name = $extra['field_name'];
$extra_field_value = $extra['field_value'];
// Save new field label into user_field table.
UserManager::create_extra_field(
$extra_field_name,
1,
$extra_field_name,
''
);
// Save the external system's id into user_field_value table.
UserManager::update_extra_field_value(
$userId,
$extra_field_name,
$extra_field_value
);
}
}
$results[] = $userId;
} else {
$results[] = 0;
}
return $results;
}
/**
* Subscribe User to Course.
*

@ -123,10 +123,6 @@ try {
$data = $restApi->addCourse($_POST);
$restResponse->setData($data);
break;
case Rest::SAVE_USER:
$data = $restApi->addUser($_POST);
$restResponse->setData($data);
break;
case Rest::SUBSCRIBE_USER_TO_COURSE:
$data = $restApi->subscribeUserToCourse($_POST);
$restResponse->setData($data);

@ -79,64 +79,6 @@ $s->register(
['return' => 'tns:user_result_array']
);
$s->register(
'WSUser.CreateUser',
[
'secret_key' => 'xsd:string',
'firstname' => 'xsd:string',
'lastname' => 'xsd:string',
'status' => 'xsd:int',
'loginname' => 'xsd:string',
'password' => 'xsd:string',
'encrypt_method' => 'xsd:string',
'user_id_field_name' => 'xsd:string',
'user_id_value' => 'xsd:string',
'visibility' => 'xsd:int',
'email' => 'xsd:string',
'language' => 'xsd:string',
'phone' => 'xsd:string',
'expiration_date' => 'xsd:string',
'extras' => 'tns:extra_field',
],
['return' => 'xsd:int']
);
$s->wsdl->addComplexType(
'user_create',
'complexType',
'struct',
'all',
'',
[
'firstname' => ['name' => 'firstname', 'type' => 'xsd:string'],
'lastname' => ['name' => 'lastname', 'type' => 'xsd:string'],
'status' => ['name' => 'status', 'type' => 'xsd:int'],
'loginname' => ['name' => 'loginname', 'type' => 'xsd:string'],
'password' => ['name' => 'password', 'type' => 'xsd:string'],
'encrypt_method' => [
'name' => 'encrypt_method',
'type' => 'xsd:string',
],
'user_id_field_name' => [
'name' => 'user_id_field_name',
'type' => 'xsd:string',
],
'user_id_value' => [
'name' => 'user_id_value',
'type' => 'xsd:string',
],
'visibility' => ['name' => 'visibility', 'type' => 'xsd:int'],
'email' => ['name' => 'email', 'type' => 'xsd:string'],
'language' => ['name' => 'language', 'type' => 'xsd:string'],
'phone' => ['name' => 'phone', 'type' => 'xsd:string'],
'expiration_date' => [
'name' => 'expiration_date',
'type' => 'xsd:string',
],
'extras' => ['name' => 'extras', 'type' => 'tns:extra_field'],
]
);
$s->wsdl->addComplexType(
'user_create_result',
'complexType',
@ -156,31 +98,6 @@ $s->wsdl->addComplexType(
]
);
$s->wsdl->addComplexType(
'user_create_result_array',
'complexType',
'array',
'',
'SOAP-ENC:Array',
[],
[
[
'ref' => 'SOAP-ENC:arrayType',
'wsdl:arrayType' => 'tns:user_create_result[]',
],
],
'tns:user_create_result'
);
$s->register(
'WSUser.CreateUsers',
[
'secret_key' => 'xsd:string',
'users' => 'tns:user_create[]',
],
['return' => 'tns:user_create_result_array']
);
$s->register(
'WSUser.EditUser',
[

@ -73,129 +73,6 @@ class WSUser extends WS
}
}
/**
* Creates a user.
*
* @param string API secret key
* @param string User first name
* @param string User last name
* @param int User status
* @param string Login name
* @param string Password (encrypted or not)
* @param string Encrypt method. Leave blank if you are passing the password in clear text,
* set to the encrypt method used to encrypt the password otherwise. Remember
* to include the salt in the extra fields if you are encrypting the password
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
* @param string User id value. Leave blank if you are using the internal user_id
* @param int Visibility. Set by default to 1
* @param string User email. Set by default to an empty string
* @param string Language. Set by default to english
* @param string Phone. Set by default to an empty string
* @param string Expiration date. Set to null by default
* @param array Extra fields. An array with elements of the form
* array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field'). Set to an empty array by default
*
* @return int New user id generated by the system
*/
public function CreateUser(
$secret_key,
$firstname,
$lastname,
$status,
$login,
$password,
$encrypt_method,
$user_id_field_name,
$user_id_value,
$visibility = 1,
$email = '',
$language = 'english',
$phone = '',
$expiration_date = '0000-00-00 00:00:00',
$extras = []
) {
// First, verify the secret key
$verifKey = $this->verifyKey($secret_key);
if ($verifKey instanceof WSError) {
$this->handleError($verifKey);
} else {
$result = $this->createUserHelper(
$firstname,
$lastname,
$status,
$login,
$password,
$encrypt_method,
$user_id_field_name,
$user_id_value,
$visibility,
$email,
$language,
$phone,
$expiration_date,
$extras
);
if ($result instanceof WSError) {
$this->handleError($result);
} else {
return $result;
}
}
}
/**
* Creates multiple users.
*
* @param string API secret key
* @param array Users array. Each member of this array must follow the structure imposed by the CreateUser method
*
* @return array Array with elements of the form
* array('user_id_value' => 'original value sent', 'user_id_generated' => 'value_generated', 'result' => array('code' => 0, 'message' => 'Operation was successful'))
*/
public function CreateUsers($secret_key, $users)
{
$verifKey = $this->verifyKey($secret_key);
if ($verifKey instanceof WSError) {
$this->handleError($verifKey);
} else {
$results = [];
foreach ($users as $user) {
$result_tmp = [];
// re-initialize variables just in case
$firstname = $lastname = $status = $login = $password = $encrypt_method = $user_id_field_name = $user_id_value = $visibility = $email = $language = $phone = $expiration_date = $extras = null;
extract($user);
$result = $this->createUserHelper(
$firstname,
$lastname,
$status,
$login,
$password,
$encrypt_method,
$user_id_field_name,
$user_id_value,
$visibility,
$email,
$language,
$phone,
$expiration_date,
$extras
);
if ($result instanceof WSError) {
$result_tmp['result'] = $result->toArray();
$result_tmp['user_id_value'] = $user_id_value;
$result_tmp['user_id_generated'] = 0;
} else {
$result_tmp['result'] = $this->getSuccessfulResult();
$result_tmp['user_id_value'] = $user_id_value;
$result_tmp['user_id_generated'] = $result;
}
$results[] = $result_tmp;
}
return $results;
}
}
/**
* Edits user info.
*
@ -341,80 +218,6 @@ class WSUser extends WS
}
}
/**
* Creates a user (helper method).
*
* @param string User first name
* @param string User last name
* @param int User status
* @param string Login name
* @param string Password (encrypted or not)
* @param string Encrypt method. Leave blank if you are passing the password in clear text,
* set to the encrypt method used to encrypt the password otherwise. Remember
* to include the salt in the extra fields if you are encrypting the password
* @param string User id field name. Use "chamilo_user_id" as the field name if you want to use the internal user_id
* @param string User id value. Leave blank if you are using the internal user_id
* @param int visibility
* @param string user email
* @param string language
* @param string phone
* @param string Expiration date
* @param array Extra fields. An array with elements of the form
* array('field_name' => 'name_of_the_field', 'field_value' => 'value_of_the_field').
*
* @return mixed New user id generated by the system, WSError otherwise
*/
protected function createUserHelper(
$firstname,
$lastname,
$status,
$login,
$password,
$encrypt_method,
$user_id_field_name,
$user_id_value,
$visibility,
$email,
$language,
$phone,
$expiration_date,
$extras = []
) {
// Add the original user id field name and value to the extra fields if needed
$extras_associative = [];
if ($user_id_field_name != "chamilo_user_id") {
$extras_associative[$user_id_field_name] = $user_id_value;
}
if (!empty($extras)) {
foreach ($extras as $extra) {
$extras_associative[$extra['field_name']] = $extra['field_value'];
}
}
$result = UserManager::create_user(
$firstname,
$lastname,
$status,
$email,
$login,
$password,
'',
$language,
$phone,
'',
PLATFORM_AUTH_SOURCE,
$expiration_date,
$visibility,
0,
$extras_associative,
$encrypt_method
);
if (!$result) {
return new WSError(104, 'There was an error creating the user');
} else {
return $result;
}
}
/**
* Edits user info (helper method).
*

@ -210,10 +210,8 @@ class MutationMap extends ResolverMap implements ContainerAwareInterface
}
$userInput = $args['user'];
$originalUserIdName = $args['originalUserIdName'];
$originalUserIdValue = $args['originalUserIdValue'];
$userId = \UserManager::get_user_id_from_original_id($originalUserIdValue, $originalUserIdName);
$userId = \UserManager::get_user_id_from_original_id($args['userId']['value'], $args['userId']['name']);
if (!empty($userId)) {
throw new UserError($this->translator->trans('User already exists'));
@ -253,8 +251,8 @@ class MutationMap extends ResolverMap implements ContainerAwareInterface
$userId,
$this->currentAccessUrl->getId()
);
\UserManager::create_extra_field($originalUserIdName, \ExtraField::FIELD_TYPE_TEXT, $originalUserIdName, '');
\UserManager::update_extra_field_value($userId, $originalUserIdName, $originalUserIdValue);
\UserManager::create_extra_field($args['userId']['name'], \ExtraField::FIELD_TYPE_TEXT, $args['userId']['name'], '');
\UserManager::update_extra_field_value($userId, $args['userId']['name'], $args['userId']['value']);
return $this->em->find('ChamiloUserBundle:User', $userId);
}

@ -43,11 +43,7 @@ type Mutation {
originalCourseIdName: String!,
originalCourseIdValue: String!
): Course
createUser(
user: CreateUserInput!,
originalUserIdName: String!,
originalUserIdValue: String!
): User
createUser(user: CreateUserInput!, userId: UserIdInput!): User
subscribeUserToCourse(
user: Int!
course: Int!

Loading…
Cancel
Save