microserviciile de register, login finalizate

This commit is contained in:
Cerbu Andrei Mihnea
2023-06-15 22:21:53 +03:00
parent a739a66ae4
commit 4e585ba278
159 changed files with 4775 additions and 1747 deletions
+27 -27
View File
@@ -1,28 +1,28 @@
<?php
namespace app\models;
use app\core\Model;
class ContactForm extends Model{
public string $subject = '';
public string $body = '';
public function rules(): array
{
return [
'subject' => [self::RULE_REQUIRED],
'body' => [self::RULE_REQUIRED]
];
}
public function labels(): array{
return [
'subject' => 'Enter your subject',
'body' => 'Enter the body'
];
}
public function send(){
return true;
}
<?php
namespace app\models;
use app\core\Model;
class ContactForm extends Model{
public string $subject = '';
public string $body = '';
public function rules(): array
{
return [
'subject' => [self::RULE_REQUIRED],
'body' => [self::RULE_REQUIRED]
];
}
public function labels(): array{
return [
'subject' => 'Enter your subject',
'body' => 'Enter the body'
];
}
public function send(){
return true;
}
}
-40
View File
@@ -1,40 +0,0 @@
<?php
namespace app\models;
use app\core\Application;
use app\core\Model;
class LoginForm extends Model{
public string $email = '';
public string $password = '';
public function rules(): array{
return [
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
'password' => [self::RULE_REQUIRED]
];
}
public function labels():array {
return [
'email' => 'Email',
'password' => 'Password'
];
}
public function login(){
$user = new User();
$user = $user->findOne(['email' => $this->email]);
if(!$user){
$this->addError('email', 'User does not exist with this email');
return false;
}
if(!password_verify($this->password, $user->password)){
$this->addError('password', 'Incorrect password');
return false;
}
return Application::$app->login($user);
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
namespace app\models;
use app\core\Model;
class LoginModel extends Model{
public string $email = '';
public string $password = '';
public function rules(): array
{
return [
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
'password' => [self::RULE_REQUIRED]
];
}
public function labels():array
{
return [
'email' => 'Email',
'password' => 'Password'
];
}
public function createBody(): array
{
$body = array();
$body["values"] = [
"email" => $this->email,
"password" => $this->password
];
return $body;
}
public function createHeaders(string $body): array
{
$headers = array();
$headers["Content-type"] = "application/json";
$headers["Content-Length"] = strlen($body);
$headers["Authorization"] = hash('md5', 'api.knowyourfood');
$headers["Auth-Request"] = "login";
return $this->formatHeaderArray($headers);
}
protected function formatHeaderArray(array $headers): array{
$array = array();
foreach ($headers as $key => $value){
$array[] = $key . ": " . $value;
}
return $array;
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
namespace app\models;
use app\core\Model;
class RegisterModel extends Model{
public string $username = '';
public string $email = '';
public string $password = '';
public function rules(): array{
return [
'username' => [self::RULE_REQUIRED],
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL],
'password' => [self::RULE_REQUIRED]
];
}
public function labels():array {
return [
'username' => 'Username',
'email' => 'Email',
'password' => 'Password',
];
}
public function createBody(): array
{
$body = array();
$body["values"] = [
"username" => $this->username,
"email" => $this->email,
"password" => $this->password
];
return $body;
}
public function createHeaders(string $body): array
{
$headers = array();
$headers["Content-type"] = "application/json";
$headers["Content-Length"] = strlen($body);
$headers["Authorization"] = hash('md5', 'api.knowyourfood');
$headers["Auth-Request"] = "register";
return $this->formatHeaderArray($headers);
}
protected function formatHeaderArray(array $headers): array{
$array = array();
foreach ($headers as $key => $value){
$array[] = $key . ": " . $value;
}
return $array;
}
}
-53
View File
@@ -1,53 +0,0 @@
<?php
namespace app\models;
use app\core\DbModel;
class User extends DbModel{
const STATUS_INACTIVE = 0;
const STATUS_ACTIVE = 1;
CONST STATUS_DELETED = 2;
public string $firstname = '';
public string $lastname = '';
public int $status = self::STATUS_INACTIVE;
public string $email = '';
public string $password = '';
public function save(){
$this->status = self::STATUS_INACTIVE;
$this->password = password_hash($this->password, PASSWORD_DEFAULT);
return parent::save();
}
public function tableName(): string{
return 'users';
}
public function rules(): array{
return [
'firstname' => [self::RULE_REQUIRED],
'lastname' => [self::RULE_REQUIRED],
'email' => [self::RULE_REQUIRED, self::RULE_EMAIL, [self::RULE_UNIQUE, 'class' => self::class]],
'password' => [self::RULE_REQUIRED, [self::RULE_MIN, 'min' => 8], [self::RULE_MAX, 'max' => 15]]
];
}
public function primaryKey(): string{
return 'id';
}
public function labels():array {
return [
'firstname' => 'First name',
'lastname' => 'Last name',
'email' => 'Email',
'password' => 'Password'
];
}
public function attributes(): array{
return ['firstname', 'lastname', 'email', 'password', 'status'];
}
}