Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controllers;
- use CodeIgniter\RESTful\ResourceController;
- class Learn extends ResourceController
- {
- public $user;
- public function __construct()
- {
- $this->user = model( 'UserModel' );
- }
- public function create()
- {
- try {
- $data = $this->request->getPost();
- $this->user = $this->user->fill( $data );
- if (isset( $data['showPrefix'] ) && $data['showPrefix'] === 'on') {
- if (strlen( $data['prefix'] ) <> 4) {
- throw new \RuntimeException( "'Prefix must be exact 4 chars length'" );
- }
- $this->user->login = trim( $data['prefix'] . $data['login'] );
- }
- // Have commented out this code, as when we use password hashing function with fill,
- // our hashed password will be longer, than clean password and it will prevent validation rule
- // for password min_length[8]..
- // if (isset($data['password']) && strlen( $data['password'] ) < PASSWORD_MIN_LENGTH) {
- // log_message( 'info', 'pw min length error' );
- // return $this->fail( 'Password minimum length is ' . PASSWORD_MIN_LENGTH );
- // }
- // ^
- // INSTEAD OF THIS | => GO TO LINE(64)
- // Validation @ Models/User
- // protected $validationRules = [
- // 'login' => [
- // 'rules' => [
- // 'required',
- // 'min_length[' . LOGIN_MIN_LENGTH . ']',
- // 'max_length[' . LOGIN_MAX_LENGTH . ']',
- // 'is_unique[accounts.login]',
- // 'regex_match[/^([a-zA-Z0-9_])\w+$/]'
- // ],
- // ],
- // 'password' => [
- // 'required',
- // 'min_length[' . PASSWORD_MIN_LENGTH . ']',
- // 'max_length[' . PASSWORD_MAX_LENGTH . ']'
- // ],
- // 'email' => [
- // 'required',
- // 'valid_email',
- // ]
- // ];
- // 64 LINE, WE USE THIS @ App\Entities\User
- // set when we set password by entity $userEntity->password('asdd') || $userEntity->password = 'asd';
- // we will get error, maybe there is better variants (and there is, but im showing what i know/done yet :( )
- // public function setPassword(string $pass): static
- // {
- // try {
- // helper( 'lineage' );
- //
- // if (strlen( $pass ) < PASSWORD_MIN_LENGTH) {
- // throw new \RuntimeException( lang( 'Validation.min_length', [ 'min_length' => PASSWORD_MIN_LENGTH ] ) );
- // }
- //
- // if (strlen( $pass ) > PASSWORD_MAX_LENGTH) {
- // throw new \RuntimeException( lang( 'Validation.max_length', [ 'max_length' => PASSWORD_MAX_LENGTH ] ) );
- // }
- //
- // $this->attributes['password'] = hashPassword( $pass );
- // } catch (\RuntimeException | \Exception $e) {
- // throw new \RuntimeException( $e->getMessage() );
- // }
- // return $this;
- // }
- // Validate before insert action
- if (!$this->validation->run( $this->user->toArray(), 'user', L2J_LS )) {
- return $this->fail( $this->validation->getErrors() );
- }
- $user_id = $this->model->insert( $this->user );
- if ($this->model->errors()) {
- log_message( 'debug', 'model errors: ' . print_r( $this->model->errors(), true ) );
- return $this->fail( $this->model->errors() );
- } else {
- // $user_id available if need
- }
- return $this->respondCreated( lang( 'User.user_created' ) );
- } catch (\Exception $e) {
- ErrorHandler::handle( $e );
- return $this->fail( $e->getMessage() );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement