/**
  * @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());
 }
 /**
  * @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());
 }