public function run() { $apiKey = $this->request->getContextPar('acl-app-info')['apiKey']; $userData = json_decode($this->request->getBody(), true)['User']; $user = new User(); $user->setUsername($userData['username']); $user->setPassword($userData['password']); try { $res = $this->forward('/users/validate', $user->serialize()); } catch (GraphException $e) { throw new GraphException('username or password invalid', 403); } // Creazione della sessione $session = new Session(); $user = json_decode($res->getBody(), true)['User']; $session->setHostAddress($this->request->getIp()); $session->setHostAgent($this->request->getUserAgent()); $session->setApiKey($apiKey); $session->setEnabled(true); $session->createDatetime(); $session->createAccessToken(); $session->setUser($user['id']); $created = $session->create(); $this->sendModel($created); }
/** * @param array $values * @param User|null $user * @return ValidationObject */ private function create(array $values, User $user = null) { if ($user === null) { $user = new User($values['username'], $values['email'], $values['password']); } $this->em->beginTransaction(); $user->setUsername($values['username']); $user->setEmail($values['email']); $user->setPassword($values['password']); $user->setFirstName($values['first_name']); $user->setLastName($values['last_name']); $validationObject = new ValidationObject(); $role = $this->getRole($values['role'], $validationObject); if (!$validationObject->isValid()) { $this->em->rollback(); return $validationObject; } $user->addRole($role); $newUser = $this->em->safePersist($user); if ($newUser === false) { // username or email already exists if ($this->usernameExists($values['username'])) { $validationObject->addError('users.user.form.messages.usernameExists', FlashMessage::WARNING); } if ($this->emailExists($values['email'])) { $validationObject->addError('users.user.form.messages.emailExists', FlashMessage::WARNING); } } if ($validationObject->isValid()) { $this->onSuccessUserCreation($user); $this->em->commit(); } else { $this->em->rollback(); } return $validationObject; }