/**
  * Test put user/password
  */
 public function test_post_user_password_creates_user()
 {
     $this->mySetup(__DIR__ . "/basic_user_table.xml");
     $new_data = ["password" => "Password1234"];
     $controller = new PasswordController([], Response::REQUEST_METHOD_PUT, $new_data, 1);
     $response = $controller->getResponse();
     self::assertIsValidResponse($response, Response::STATUS_CODE_CREATED);
     self::assertIsValidResponse($response, Response::STATUS_CODE_CREATED);
 }
 /**
  * Create a new User instance.
  *
  * @param  Object  $request
  * @return Response
  */
 public static function addUser()
 {
     $app = \Slim\Slim::getInstance();
     $request = (object) $app->request->post();
     //validate input
     $validata = $app->validata;
     $validator = $validata::key('email', $validata::email()->notEmpty())->key('first_name', $validata::stringType()->notEmpty())->key('last_name', $validata::stringType()->notEmpty())->key('password', $validata::stringType()->notEmpty())->key('company', $validata::intVal())->key('city', $validata::stringType()->notEmpty())->key('province', $validata::stringType()->notEmpty())->key('zip_code', $validata::stringType()->notEmpty())->key('address', $validata::stringType()->notEmpty());
     $errors = array();
     try {
         $validator->assert((array) $request);
     } catch (\InvalidArgumentException $e) {
         $errors = $e->findMessages(array('email' => '{{name}} must be a valid email', 'first_name' => '{{name}} is required', 'last_name' => '{{name}} is required', 'password' => 'Password is required', 'company' => 'Company is required', 'city' => 'City is required', 'province' => 'State/Province is required', 'address' => 'Address is required', 'zip_code' => 'Zipcode is required'));
     }
     if ($validator->validate((array) $request)) {
         if (!PasswordController::isValid($request->password, $request->email)) {
             $app->halt('400', json_encode("Password Formart Wrong"));
         }
         if (!Company::find($request->company)) {
             $app->halt('400', json_encode("Company does not exist"));
         }
         if (self::isExist($request->email)) {
             $app->response->setStatus(400);
             return json_encode("Email already taken");
         }
         $user = new User();
         //$user->name = $request->name;
         $user->email = $request->email;
         $user->password = PasswordController::encryptPassword($request->password);
         $user->first_name = $request->first_name;
         $user->last_name = $request->last_name;
         if (isset($request->phone)) {
             $user->phone = $request->phone;
         }
         $user->city = $request->city;
         $user->address = $request->address;
         $user->province = $request->province;
         $user->zip_code = $request->zip_code;
         $user->country = $request->country;
         $user->active = 0;
         $user->save();
         $app->response->setStatus(200);
         //send confirm email
         if ($user->id) {
             $user->companies()->attach($request->company);
             $link = WEBSITELINK . '/' . self::$active_api . openssl_encrypt($user->id, 'AES-256-CBC', self::$pass, 0, self::$iv);
             EmailController::newUserConfirmation($user->id, $request->password, $link);
         }
         return $user->id;
     } else {
         $app->response->setStatus(400);
         $return = [];
         foreach (array_values($errors) as $key => $error) {
             if ($error != "") {
                 array_push($return, array("code" => $key, "data" => $error));
             }
         }
         return json_encode($return);
     }
 }