Exemplo n.º 1
0
 public function testEmail()
 {
     foreach ($this->validEmails as $email) {
         $data = Validator::create(array('email' => $email));
         $data->field('email')->required()->email();
         $this->assertTrue($data->validate(), 'E-mail "' . $email . '" is invalid');
     }
     foreach ($this->invalidEmails as $email) {
         $data = Validator::create(array('email' => $email));
         $data->field('email')->required()->email();
         $this->assertFalse($data->validate(), 'E-mail "' . $email . '" is valid');
     }
 }
Exemplo n.º 2
0
 /**
  * @method POST
  * @json
  */
 public function login()
 {
     /** @var \Bazalt\Auth\Model\User $user */
     $user = null;
     $data = Validator::create($this->request->data);
     $data->field('password')->required();
     $data->field('email')->required()->validator('exist_user', function ($value) use(&$user, $data) {
         $user = User::getUserByLoginPassword($value, $data['password'], true);
         return $user != null;
     }, 'User with this login/email does not exists');
     if (!$data->validate()) {
         return new Response(400, $data->errors());
     }
     $user->login($data['remember_me'] == 'true');
     return new Response(Response::OK, $user->toArray());
 }
Exemplo n.º 3
0
 /**
  * @method POST
  * @json
  */
 public function addUser()
 {
     $data = Validator::create((array) $this->request->data);
     $emailField = $data->field('email')->required()->email();
     $user = User::create();
     // check email
     $emailField->validator('uniqueEmail', function ($email) {
         return User::getUserByEmail($email, false) == null;
     }, 'User with this email already exists');
     $userRoles = [];
     /*$data->field('roles')->validator('validRoles', function($roles) use (&$userRoles) {
           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->password = User::cryptPassword($data['password']);
     $user->gender = $data['gender'];
     $user->save();
     $user->Roles->clearRelations(array_keys($userRoles));
     foreach ($userRoles as $role) {
         $user->Roles->add($role, ['site_id' => 6]);
     }
     // Create the message
     $message = \Swift_Message::newInstance()->setSubject('Благодарим за регистрацию на MixFree')->setFrom(array('*****@*****.**' => 'MixFree'))->setTo([$user->email])->setBody(sprintf('Ваш ключ активации: http://localhost/user/activation/%d/%s', $user->id, $user->getActivationKey()));
     $transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('*****@*****.**')->setPassword('gjhndtqy777');
     $mailer = \Swift_Mailer::newInstance($transport);
     $result = $mailer->send($message);
     return new Response(200, $user->toArray());
 }
Exemplo n.º 4
0
 /**
  * @param Validator $validator
  * @return ValidationSet
  */
 public function nestedArray(Validator $validator)
 {
     return $this->isArray()->validator('nestedArray', function (&$value) use($validator) {
         $valid = true;
         foreach ($value as $item) {
             $itemValid = $validator->data($item)->validate();
             if (!$itemValid) {
                 $valid = false;
             }
         }
         return $valid;
     }, 'Invalid nested array validation');
 }
Exemplo n.º 5
0
 /**
  * @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());
 }
Exemplo n.º 6
0
 /**
  * @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());
 }
Exemplo n.º 7
0
 public function testArrayKeys()
 {
     $obj = new \stdClass();
     $nested = new \stdClass();
     $nested->title = '123';
     $nested2 = new \stdClass();
     $nested2->title = 'qwe';
     $obj->data = array('uk' => $nested, 'ru' => $nested2);
     $data = Validator::create($obj)->field('data')->nestedArray(Validator::create()->field('title')->required()->end())->keys(array('ru', 'uk'))->end();
     $valid = $data->validate();
     $this->assertEquals(array(), $data->errors());
     $this->assertTrue($valid);
     //invalid
     $data = Validator::create($obj)->field('data')->nestedArray(Validator::create()->field('title')->required()->end())->keys(array('ru', 'en'))->end();
     $valid = $data->validate();
     $this->assertEquals(array('data' => array('keys' => 'Invalid keys "uk"')), $data->errors());
     $this->assertFalse($valid);
 }