public function testDelete()
 {
     $user = User::create();
     $user->login = '******';
     $user->is_active = true;
     $user->save();
     $this->models[] = $user;
     $response = new \Bazalt\Rest\Response(403, 'Permission denied');
     $this->assertResponse('DELETE /auth/users/' . $user->id, ['contentType' => 'application/json'], $response);
     $user = User::getById($user->id);
     $this->assertEquals(0, $user->is_deleted);
     $this->addPermission('auth.can_delete_user', $user);
     // login
     \Bazalt\Auth::setUser($user);
     $response = new \Bazalt\Rest\Response(400, ['id' => 'Can\'t delete yourself']);
     $this->assertResponse('DELETE /auth/users/' . $user->id, ['contentType' => 'application/json'], $response);
     $user = User::getById($user->id);
     $this->assertEquals(0, $user->is_deleted);
     $user2 = User::create();
     $user2->login = '******';
     $user2->is_active = true;
     $user2->save();
     $this->models[] = $user2;
     $this->addPermission('auth.can_delete_user', $user2);
     // login
     \Bazalt\Auth::setUser($user2);
     $response = new \Bazalt\Rest\Response(200, true);
     $this->assertResponse('DELETE /auth/users/' . $user->id, ['contentType' => 'application/json'], $response);
     $user = User::getById($user->id);
     $this->assertEquals(1, $user->is_deleted);
 }
 public function testPost()
 {
     $response = new \Bazalt\Rest\Response(400, ['password' => ['required' => 'Field cannot be empty'], 'email' => ['required' => 'Field cannot be empty', 'exist_user' => 'User with this login/email does not exists']]);
     $this->assertResponse('POST /auth/session', ['data' => json_encode(array('hello' => 'computer'))], $response);
     $user = \Bazalt\Auth\Model\User::getById($this->user->id);
     $response = new \Bazalt\Rest\Response(200, $user->toArray());
     $this->assertResponse('POST /auth/session', ['data' => json_encode(array('email' => $this->user->login, 'password' => '1'))], $response);
     // get logined user
     $response = new \Bazalt\Rest\Response(200, $user->toArray());
     $this->assertResponse('GET /auth/session', ['contentType' => 'application/json'], $response);
     // logout
     $response = new \Bazalt\Rest\Response(200, '/is_guest/');
     $this->assertRegExpResponse('DELETE /auth/session', [], $response);
     // guest logout
     $response = new \Bazalt\Rest\Response(200, '/is_guest/');
     $this->assertRegExpResponse('DELETE /auth/session', [], $response);
 }
 /**
  * @method PUT
  * @method POST
  * @json
  */
 public function saveUser()
 {
     $data = Validator::create((array) $this->request->data);
     $emailField = $data->field('email')->required()->email();
     $user = User::getById($data['id']);
     if (!$user) {
         return new Response(400, ['id' => 'User not found']);
     }
     $userRoles = [];
     $data->field('roles')->validator('validRoles', function ($roles) use(&$userRoles) {
         if ($roles) {
             foreach ($roles as $role) {
                 $userRoles[$role] = Role::getById($role);
                 if (!$userRoles[$role]) {
                     return false;
                 }
             }
         }
         return true;
     }, 'Invalid roles');
     $data->field('login')->required();
     $data->field('gender')->required();
     if (!$data->validate()) {
         return new Response(400, $data->errors());
     }
     $user->login = $data['login'];
     $user->email = $data['email'];
     $user->firstname = $data['firstname'];
     $user->secondname = $data['secondname'];
     $user->patronymic = $data['patronymic'];
     $user->birth_date = date('Y-m-d', strToTime($data['birth_date']));
     //$user->password = User::cryptPassword($data['password']);
     $user->gender = $data['gender'];
     $user->is_active = $data['is_active'];
     $user->is_deleted = $data['is_deleted'];
     $user->save();
     $user->Roles->clearRelations(array_keys($userRoles));
     foreach ($userRoles as $role) {
         $user->Roles->add($role, ['site_id' => 6]);
     }
     return new Response(200, $user->toArray());
 }
 /**
  * @method POST
  * @json
  */
 public function saveUser()
 {
     $data = new Validator((array) $this->request->data);
     $emailField = $data->field('email')->required()->email();
     $isNew = false;
     if ($data->getData('id')) {
         $user = User::getById($data->getData('id'));
         if (!$user) {
             return new Response(400, ['id' => 'User not found']);
         }
     } else {
         $user = User::create();
         // check email
         $emailField->validator('uniqueEmail', function ($email) {
             return User::getUserByEmail($email, false) == null;
         }, 'User with this email already exists');
         $isNew = true;
     }
     $data->field('login')->required();
     $data->field('gender')->required();
     if (!$data->validate()) {
         return new Response(400, $data->errors());
     }
     $user->login = $data->getData('email');
     $user->email = $data->getData('email');
     $user->firstname = $data->getData('first');
     $user->lastname = $data->getData('last');
     $user->password = User::cryptPassword($data->getData('password'));
     $user->gender = $data->getData('gender');
     $user->is_active = $data->getData('is_active');
     $user->save();
     if ($isNew) {
         // Create the message
         $message = \Swift_Message::newInstance()->setSubject('Your subject')->setFrom(array('*****@*****.**' => 'John Doe'))->setTo([$user->email])->setBody('Here is the message itself')->addPart('<q>Here is the message itself</q>', 'text/html');
         $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('*****@*****.**')->setPassword('gjhndtqy777');
         $mailer = \Swift_Mailer::newInstance($transport);
         $result = $mailer->send($message);
         print_r($result);
     }
     return new Response(200, $user->toArray());
 }
 protected function getJWTUser()
 {
     $userId = $this->getIssParam();
     $user = $userId ? \Bazalt\Auth\Model\User::getById((int) $userId) : \Bazalt\Auth\Model\Guest::create(null);
     return $user;
 }