diff --git a/plugins/restful/user/1.0/RestfulUserRegistrationResource.class.php b/plugins/restful/user/1.0/RestfulUserRegistrationResource.class.php deleted file mode 100644 index 3c27fb0..0000000 --- a/plugins/restful/user/1.0/RestfulUserRegistrationResource.class.php +++ /dev/null @@ -1,149 +0,0 @@ - [ - \RestfulInterface::POST => 'addAccount', - ], - '^.*$' => [ - \RestfulInterface::PUT => 'updateAccount', - ] - ]; - } - - /** - * {@inheritdoc} - */ - public function publicFieldsInfo() { - return []; - } - - /** - * Add user account into Drupal. - */ - public function addAccount() { - $account = $this->getAccount(); - - // Check if the account has access to register a user. - if (!user_access('allowed to register user account', $account)) { - throw new \RestfulBadRequestException( - "Account doesn't have access to register user." - ); - } - $account = new stdClass(); - $account->is_new = TRUE; - - $user = $this->saveUserAccount($account); - - // Check to make sure we didn't fail when saving the user. - if (FALSE === $user) { - throw new \RestfulBadRequestException('Adding user failed.'); - } - - return [$user]; - } - - /** - * Update user account in Drupal. - */ - public function updateAccount($uid) { - if (!isset($uid)) { - throw new \RestfulBadRequestException('User identifier is missing.'); - } - $account = $this->getAccount(); - - // Check if the account has access to update a user. - if (!user_access('allowed to update user account', $account)) { - throw new \RestfulBadRequestException( - "Account doesn't have access to update user." - ); - } - $account = user_load($uid); - - if (FALSE === $account) { - throw new \RestfulBadRequestException("Account doesn't exist."); - } - - $user = $this->saveUserAccount($account); - - // Check to make sure we didn't fail when saving the user. - if (FALSE === $user) { - throw new \RestfulBadRequestException('Updating user failed.'); - } - - return [$user]; - } - - /** - * Save the user account based on request. - * - * @return array - * An array of the saved user object; otherwise FALSE if failed. - */ - protected function saveUserAccount($account) { - if (!is_object($account)) { - return FALSE; - } - $request = $this->getRequest(); - static::cleanRequest($request); - - // Check if the request has the valid parameters defined. - if (!$this->isValidateRequest($request)) { - throw new \RestfulBadRequestException('Missing required parameters.'); - } - $name = $request['name']; - $pass = $request['pass']; - $mail = $request['mail']; - - // Load the user object by account name. - $object = user_load_by_name($name); - - if ((isset($account->is_new) && $account->is_new) || - ($object->uid !== $account->uid)) { - - if (FALSE !== $object) { - throw new \RestfulBadRequestException('Account name already exists.'); - } - } - - $edit = [ - 'name' => $name, - 'pass' => $pass, - 'mail' => $mail, - 'init' => NULL, - 'status' => TRUE, - ]; - $roles = user_roles(TRUE); - - // Attach the valid roles to the user account based on the id. - if (isset($request['roles']) && !empty($request['roles'])) { - foreach ($request['roles'] as $id) { - if (!isset($roles[$id])) { - continue; - } - $edit['roles'][$id] = $roles[$id]; - } - } - - // Save the account in Drupal. - return user_save($account, $edit); - } - - /** - * Determine if the request has the valid parameters defined. - */ - protected function isValidateRequest($request) { - return $request['name'] && $request['pass'] ?: FALSE; - } - -} diff --git a/plugins/restful/user/1.0/user_registration__1_0.inc b/plugins/restful/user/1.0/user_registration__1_0.inc deleted file mode 100644 index 2d30f77..0000000 --- a/plugins/restful/user/1.0/user_registration__1_0.inc +++ /dev/null @@ -1,16 +0,0 @@ - t('User Registration'), - 'resource' => 'user_registration', - 'name' => 'user_registration__1_0', - 'description' => t('User registration actions.'), - 'class' => 'RestfulUserRegistrationResource', - 'authentication_types' => TRUE, - 'authentication_optional' => FALSE, -]; diff --git a/restful_user_registration.info b/restful_user_registration.info index 4a34561..054666e 100644 --- a/restful_user_registration.info +++ b/restful_user_registration.info @@ -1,6 +1,9 @@ -name = RESTful user registration -description = Define user registration RESTful resources. +name = RESTful v2 user registration +description = Define user registration RESTful v2 resources. php = 5.3 core = 7.x package = Web Service -dependencies[] = restful +dependencies[] = restful (2.x) + +registry_autoload[] = PSR-0 +registry_autoload[] = PSR-4 diff --git a/restful_user_registration.module b/restful_user_registration.module index 5bc21f1..490d61c 100644 --- a/restful_user_registration.module +++ b/restful_user_registration.module @@ -5,15 +5,6 @@ * The core implementation for the RESTful user registration module. */ -/** - * Implements hook_ctools_plugin_directory(). - */ -function restful_user_registration_ctools_plugin_directory($owner, $plugin_type) { - if ($owner == 'restful' && $plugin_type == 'restful') { - return 'plugins/' . $plugin_type; - } -} - /** * Implements hook_permission(). */ diff --git a/src/Plugin/user_registration/v1/UserRegistration__1_0.php b/src/Plugin/user_registration/v1/UserRegistration__1_0.php new file mode 100644 index 0000000..398df6a --- /dev/null +++ b/src/Plugin/user_registration/v1/UserRegistration__1_0.php @@ -0,0 +1,178 @@ + array( + RequestInterface::METHOD_POST => 'addAccount', + ), + '^.*$' => array( + RequestInterface::PUT => 'updateAccount', + ) + ); + } + + /** + * {@inheritdoc} + */ + protected function publicFields() { + return array(); + } + + /** + * Add user account into Drupal. + */ + public function addAccount() { + $account = $this->getAccount(); + + // Check if the account has access to register a user. + if (!user_access('allowed to register user account', $account)) { + throw new BadRequestException( + "Account doesn't have access to register user." + ); + } + $account = new \stdClass(); + $account->is_new = TRUE; + + $user = $this->saveUserAccount($account); + + // Check to make sure we didn't fail when saving the user. + if (FALSE === $user) { + throw new BadRequestException('Adding user failed.'); + } + + return array($user); + } + + /** + * Update user account in Drupal. + */ + public function updateAccount($uid) { + if (!isset($uid)) { + throw new BadRequestException('User identifier is missing.'); + } + $account = $this->getAccount(); + + // Check if the account has access to update a user. + if (!user_access('allowed to update user account', $account)) { + throw new BadRequestException( + "Account doesn't have access to update user." + ); + } + $account = user_load($uid); + + if (FALSE === $account) { + throw new BadRequestException("Account doesn't exist."); + } + + $user = $this->saveUserAccount($account); + + // Check to make sure we didn't fail when saving the user. + if (FALSE === $user) { + throw new BadRequestException('Updating user failed.'); + } + + return [$user]; + } + + /** + * Save the user account based on request. + * + * @return array + * An array of the saved user object; otherwise FALSE if failed. + */ + protected function saveUserAccount($account) { + if (!is_object($account)) { + return FALSE; + } + $request_body = $this->getRequest()->getParsedBody(); + + // Check if the request has the valid parameters defined. + if (!$this->isValidateRequest($request_body)) { + throw new BadRequestException('Missing required parameters.'); + } + $name = $request_body['name']; + $pass = $request_body['pass']; + $mail = $request_body['mail']; + + // Load the user object by account name. + $object = user_load_by_name($name); + + if ((isset($account->is_new) && $account->is_new) || + ($object->uid !== $account->uid)) { + + if (FALSE !== $object) { + throw new BadRequestException('Account name already exists.'); + } + } + + $edit = array( + 'name' => $name, + 'pass' => $pass, + 'mail' => $mail, + 'init' => $mail, + 'status' => TRUE, + ); + $roles = user_roles(TRUE); + + // Attach the valid roles to the user account based on the id. + if (isset($request_body['roles']) && !empty($request_body['roles'])) { + foreach ($request_body['roles'] as $id) { + if (!isset($roles[$id])) { + continue; + } + $edit['roles'][$id] = $roles[$id]; + } + } + + // Save the account in Drupal. + return user_save($account, $edit); + } + + /** + * Determine if the request has the valid parameters defined. + */ + protected function isValidateRequest($request_body) { + return $request_body['name'] && $request_body['pass'] && $request_body['mail'] ?: FALSE; + } + +}