public function postCreate()
	{
		$response = array(
			"method" => "create",
			"success" => false,
			"user" => "",
			"error_code" => 0,
			"error_message" => ""
		);

		try {
		    AuthorizerHelper::can(UserValidator::USER_CAN_SAVE);

            $data = json_decode(file_get_contents("php://input"));
            if (empty($data)) {
                $data = (object) $_POST;
            }

            $userModel = new User();

            // Check required fields
            $requiredParams = array('email', 'name', 'role', 'password');
            $params = (array) $data;
            foreach ($requiredParams as $param){
                if (empty($params[$param])) {
                    throw new \Exception(ucfirst($param) .' is required.');
                }
            }

            // Check Dupe email [ER-155]
            if(!empty($data->email)) {
                if (false === $userModel->isEmailUnique($data->email)) {
                    throw new \Exception("The email you entered already exists.");
                }
            }

            $userId = $userModel->save($data);

            if (empty($userId)) {
                throw  new \Exception('Could not create new user.');
            }
            $user = $userModel->getById($userId);
            $output = array('id'       => $user->getId(),
                            'email'    => $user->getEmail(),
                            'role'     => $this->getRoleInfo($user),
                            'name'     => $user->getName(),
                            'last_login' => $user->getLastLogin(),
                            'gateway_customer_id'=> $user->getGatewayCustomerId()
            );

			$response['user'] = $output;
			$response['success'] = true;
			$this->setStatusCode(200);
		} catch (\Exception $e) {
			$response['error_message'] = $e->getMessage();
			$response['error_code'] = $e->getCode();
		}

		$this->setContent($response);
	}
public function postCreate()
	{
		$response = array(
			"method" => "create",
			"success" => false,
			"user" => "",
			"error_code" => 0,
			"error_message" => ""
		);

		try {
		    AuthorizerHelper::can(UserValidator::USER_CAN_SAVE);

            $data = json_decode(file_get_contents("php://input"));
            if (empty($data)) {
                $data = (object) $_POST;
            }

            $userModel = new User();

            // Check required fields
            $requiredParams = array('email', 'name', 'role', 'password');
            $params = (array) $data;
            foreach ($requiredParams as $param){
                if (empty($params[$param])) {
                    throw new \Exception(ucfirst($param) .' is required.');
                }
            }

            // Check Dupe email [ER-155]
            if(!empty($data->email)) {
                if (false === $userModel->isEmailUnique($data->email)) {
                    throw new \Exception("The email you entered already exists.");
                }
            }

            $userId = $userModel->save($data);

            if (empty($userId)) {
                throw  new \Exception('Could not create new user.');
            }
            $user = $userModel->getById($userId);
            $output = array('id'       => $user->getId(),
                            'email'    => $user->getEmail(),
                            'role'     => $this->getRoleInfo($user),
                            'name'     => $user->getName(),
                            'last_login' => $user->getLastLogin(),
                            'gateway_customer_id'=> $user->getGatewayCustomerId()
            );

			$response['user'] = $output;
			$response['success'] = true;
			$this->setStatusCode(200);
		} catch (\Exception $e) {
			$response['error_message'] = $e->getMessage();
			$response['error_code'] = $e->getCode();
		}

		$this->setContent($response);
	}
public function postCreate ( ) { $ response = array ( "method" => "create" , "success" => false , "user" => "" , "error_code" => 0 , "error_message" => "" ) ; try { AuthorizerHelper :: can ( UserValidator :: USER_CAN_SAVE ) ; $ data = json_decode ( file_get_contents ( "php://input" ) ) ; if ( empty ( $ data ) ) { $ data = ( object ) $ _POST ; } $ userModel = new User ( ) ; // Check required fields $ requiredParams = array ( 'email' , 'name' , 'role' , 'password' ) ; $ params = ( array ) $ data ; foreach ( $ requiredParams as $ param ) { if ( empty ( $ params [ $ param ] ) ) { throw new \ Exception ( ucfirst ( $ param ) . ' is required.' ) ; } } // Check Dupe email [ER-155] if ( ! empty ( $ data -> email ) ) { if ( false === $ userModel -> isEmailUnique ( $ data -> email ) ) { throw new \ Exception ( "The email you entered already exists." ) ; } } $ userId = $ userModel -> save ( $ data ) ; if ( empty ( $ userId ) ) { throw new \ Exception ( 'Could not create new user.' ) ; } $ user = $ userModel -> getById ( $ userId ) ; $ output = array ( 'id' => $ user -> getId ( ) , 'email' => $ user -> getEmail ( ) , 'role' => $ this -> getRoleInfo ( $ user ) , 'name' => $ user -> getName ( ) , 'last_login' => $ user -> getLastLogin ( ) , 'gateway_customer_id' => $ user -> getGatewayCustomerId ( ) ) ; $ response [ 'user' ] = $ output ; $ response [ 'success' ] = true ; $ this -> setStatusCode ( 200 ) ; } catch ( \ Exception $ e ) { $ response [ 'error_message' ] = $ e -> getMessage ( ) ; $ response [ 'error_code' ] = $ e -> getCode ( ) ; } $ this -> setContent ( $ response ) ; }
User CRUD actions
User CRUD actions