<?php namespace CoreCommon\Controllers;
use CoreCommon\Entities\UsersEntity;
use CoreCommon\Libraries\CustomFileSystem;
use CoreCommon\Models\UsersModel;
use CodeIgniter\HTTP\ResponseInterface;
class Profile extends ApiController
{
/**
* index function
* @method : GET
*/
public function index()
{
$this->setupAuthClasses();
$userModel = new UsersModel();
$user = $userModel->where('id', $this->userId)->get()->getResultObject();
return $this->respond([
'data' => $user
], ResponseInterface::HTTP_OK, lang('Common.api.receive'));
}
public function create()
{
$userModel = new UsersModel();
$userEntity = new UsersEntity();
$usersMythAuth = new \Myth\Auth\Models\UserModel();
$isPassword = false;
$customConfig = new \CoreCommon\Config\CoreAppConfig();
$imageService = \CodeIgniter\Config\Services::image();
$handyFunction = new CustomFileSystem();
if ($this->request) {
//get request from Vue Js
$rules = [
'first_name' => 'if_exist|required|max_length[255]',
'last_name' => 'if_exist|required|max_length[255]',
'address' => 'if_exist|required|max_length[255]',
'phone' => 'if_exist|required|max_length[11]',
'password' => 'if_exist|required',
'pass_confirm' => 'if_exist|required|matches[password]',
'gender' => 'if_exist|required',
'country' => 'if_exist|required|max_length[255]',
'city' => 'if_exist|required|max_length[255]',
'image' => 'if_exist|uploaded[image]|max_size[image,4096]|ext_in[avatar,png,jpg,jpeg,webp]',
];
if (!$this->validate($rules)) {
return $this->respond([
'error' => $this->validator->getErrors(),
'success' => false
], ResponseInterface::HTTP_NOT_ACCEPTABLE, lang('Common.api.validation'));
}
$user = $usersMythAuth->where('id', $this->userId)
->first();
if (isset($this->request->getJSON()->password)) {
$user->password = $this->request->getJSON()->password;
$user->reset_hash = null;
$user->reset_at = null;
$user->reset_expires = null;
$user->force_pass_reset = false;
$isPassword = true;
} else if (isset($this->request->getJSON()->first_name)) {
$userEntity->first_name = $this->request->getJSON()->first_name;
$userEntity->last_name = $this->request->getJSON()->last_name;
$userEntity->gender = $this->request->getJSON()->gender;
} else if (isset($this->request->getJSON()->address)) {
$userEntity->address = $this->request->getJSON()->address;
$userEntity->country = $this->request->getJSON()->country;
$userEntity->city = $this->request->getJSON()->city;
}
if (isset($_FILES['image'])) {
$path = $userModel->where('id', $this->userId)
->first();
$handyFunction->removeSingleFile(ROOTPATH . $path->image);
$avatar = $this->request->getFile('image');
$avatar->move($customConfig->uploadDirectory . '/profile', time() . '.' . $avatar->getClientExtension());
$userEntity->image = $avatar->getName();
$userEntity->editImage();
$imageService->withFile(ROOTPATH . $userEntity->image)
->withResource()->fit(100, 100, 'center')
->save(ROOTPATH . $userEntity->image, 90);
}
if (is_null($user->phone) && isset($this->request->getJSON()->phone)) {
$userEntity->phone = $this->request->getJSON()->phone;
} else if (is_null($user->email) && isset($this->request->getJSON()->email)) {
$userEntity->email = $this->request->getJSON()->email;
}
if ($isPassword == true) {
if (!$usersMythAuth->save($user)) {
return $this->respond([
'data' => $usersMythAuth->errors(),
'success' => false,
], ResponseInterface::HTTP_BAD_REQUEST, lang('Common.api.reject'));
}
} else {
if (!$userModel->update($this->userId, $userEntity)) {
return $this->respond([
'data' => $userModel->errors(),
'success' => false,
], ResponseInterface::HTTP_BAD_REQUEST, lang('Common.api.reject'));
}
}
return $this->respond([
'success' => true,
], ResponseInterface::HTTP_CREATED, lang('Common.api.save'));
}
}
}
<?php namespace CoreCommon\Entities;
use \CodeIgniter\Entity;
use CodeIgniter\I18n\Time;
class UsersEntity extends Entity
{
protected $id;
protected $first_name;
protected $last_name;
protected $phone;
protected $image;
protected $address;
protected $password_hash;
protected $gender;
protected $country;
protected $city;
protected $email;
protected $attributes = [
'id' => null,
'first_name' => null,
'last_name' => null,
'phone' => null,
'email' => null,
'address' => null,
'image' => null,
'password_hash' => null,
'gender' => null,
'country' => null,
'city' => null,
'reset_hash' => null,
'reset_at' => null,
'reset_expires' => null,
'force_pass_reset' => null,
];
protected $datamap = [
];
protected $dates = [];
protected $casts = [];
protected $permissions = [];
protected $roles = [];
public function setPassword()
{
$config = new \Myth\Auth\Config\Auth();
if (
(defined('PASSWORD_ARGON2I') && $config->hashAlgorithm == PASSWORD_ARGON2I)
||
(defined('PASSWORD_ARGON2ID') && $config->hashAlgorithm == PASSWORD_ARGON2ID)
) {
$hashOptions = [
'memory_cost' => $config->hashMemoryCost,
'time_cost' => $config->hashTimeCost,
'threads' => $config->hashThreads
];
} else {
$hashOptions = [
'cost' => $config->hashCost
];
}
$this->attributes['password_hash'] = password_hash(
base64_encode(
hash('sha384', $this->password_hash, true)
),
$config->hashAlgorithm,
$hashOptions
);
/*
Set these vars to null in case a reset password was asked.
Scenario:
user (a *dumb* one with short memory) requests a
reset-token and then does nothing => asks the
administrator to reset his password.
User would have a new password but still anyone with the
reset-token would be able to change the password.
*/
$this->attributes['reset_hash'] = null;
$this->attributes['reset_at'] = null;
$this->attributes['reset_expires'] = null;
$this->attributes['force_pass_reset'] = false;
}
public function editImage()
{
$this->attributes['image'] = 'public/upload/profile/' . $this->attributes['image'];
return $this;
}
}
<?php namespace CoreCommon\Models;
use CoreCommon\Entities\UsersEntity;
use CodeIgniter\Model;
class UsersModel extends Model
{
/**
* table name
*/
protected $primaryKey = "id";
protected $table = "users";
/**
* allowed Field
*/
protected $allowedFields = [
'first_name',
'last_name',
'image',
'address',
'phone',
'email',
'password_hash',
'gender',
'country',
'city',
'reset_hash',
'reset_at',
'reset_expires',
'force_pass_reset',
];
protected $returnType = UsersEntity::class;
protected $validationRules = [
'first_name' => 'if_exist|required|max_length[255]',
'last_name' => 'if_exist|required|max_length[255]',
'image' => 'if_exist|required|max_length[255]',
'address' => 'if_exist|required|max_length[255]',
'phone' => 'if_exist|required|max_length[11]',
'email' => 'if_exist|required|valid_email|is_unique[users.email]',
'password_hash' => 'if_exist|required',
'gender' => 'if_exist|required',
'country' => 'if_exist|required|max_length[255]',
'city' => 'if_exist|required|max_length[255]',
];
protected $validationMessages = [];
protected $skipValidation = false;
}