Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
56dfe12
review code in master
andrewalf Sep 11, 2017
e6cd0b7
Merge remote-tracking branch 'refs/remotes/origin/review_branch'
andrewalf Sep 11, 2017
d4415a6
finish review
andrewalf Sep 11, 2017
e6d5529
Edited BaseView
oleglacto Nov 23, 2017
fb438de
transfered layout
oleglacto Nov 23, 2017
f00512f
edited path to
oleglacto Nov 23, 2017
aee517f
delited bad mettods
oleglacto Nov 23, 2017
1bde919
delited hash password
oleglacto Nov 23, 2017
8f44a49
edited path to template
oleglacto Nov 23, 2017
a907f39
small changes
oleglacto Nov 23, 2017
c2af62d
edited Route.php
oleglacto Nov 23, 2017
8142302
added phpdoc to BaseRepository.php
oleglacto Nov 23, 2017
3e22e09
added error.log
oleglacto Nov 23, 2017
b1f55c4
класс для логирования ошибок
oleglacto Nov 23, 2017
13a2962
added logs to ignore
oleglacto Nov 23, 2017
acc09cc
added errorHandler and set errorHandler as default
oleglacto Nov 29, 2017
764eb3e
added .htaccess
oleglacto Nov 29, 2017
b2a7647
added output error
oleglacto Dec 4, 2017
aed8eed
for test
oleglacto Dec 4, 2017
c3a260a
test2
oleglacto Dec 4, 2017
bfcf924
test3
oleglacto Dec 4, 2017
7e18996
test4
oleglacto Dec 4, 2017
79871ab
small changes
oleglacto Feb 10, 2018
fc8164e
new css rules for errors
oleglacto Feb 10, 2018
b5bf049
AuthorizationGuard class
oleglacto Feb 10, 2018
e40c42e
Security class
oleglacto Feb 10, 2018
e4acf02
Validator
oleglacto Feb 10, 2018
d059394
registration and authorisation controller
oleglacto Feb 10, 2018
bee12c4
edited method render
oleglacto Feb 10, 2018
b748e5a
don't know
oleglacto Feb 10, 2018
d69ba1c
small fix
oleglacto Feb 10, 2018
1f380fe
small changes(edited method 'render')
oleglacto Feb 10, 2018
f1cf7cb
.
oleglacto Feb 10, 2018
10f03c4
DataBase to singleton
oleglacto Feb 10, 2018
5654ae2
my first model
oleglacto Feb 10, 2018
4f56311
added error handler
oleglacto Feb 10, 2018
a6bc77a
small changes
oleglacto Feb 10, 2018
488aa4f
small changes
oleglacto Feb 10, 2018
b238c5e
edited view
oleglacto Feb 10, 2018
557d687
errors
oleglacto Feb 10, 2018
995caaf
for review
oleglacto Mar 25, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.idea/*
application/config/*
logs/*
49 changes: 49 additions & 0 deletions application/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
/**
* Created by PhpStorm.
* User: oleg
* Date: 12.02.18
* Time: 19:50
*/

namespace application;


use application\components\AuthorizationGuard;
use application\components\Request;
use application\core\Route;

class App
{
protected static $instance = null;

public $isAuth;

private $route;

public $guard;

private function __construct()
{
$this->guard = AuthorizationGuard::getInstance();
$this->route = new Route();
$request = new Request();
}

public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}

return self::$instance;
}

public function run()
{
$this->isAuth = $this->guard->isAuthorized();

$this->route->start();
}

}
6 changes: 2 additions & 4 deletions application/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,10 @@

use \application\core\Route;



require_once 'core/BaseModel.php';
require_once 'core/BaseView.php';
require_once 'core/BaseController.php';
require_once 'core/Route.php';


Route::start(); // запускаем маршрутизатор
$route = new Route();
$route->start();
138 changes: 138 additions & 0 deletions application/components/AuthorizationGuard.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php
/**
* Created by PhpStorm.
* User: oleg
* Date: 31.01.18
* Time: 23:16
*/

namespace application\components;


use application\models\User;
use application\repositories\UserRepository;

class AuthorizationGuard
{
protected static $instance = null;

/*
* Текущий пользователь
*/
protected $currentUser;

protected $errors = [];

/*
* Экземпляр класса Security
*/
protected $security;

/*
* Экземпляр класса UserRepository
*/
protected $userRepository;

// это называется инъекция зависимостей.
// оч плозо делать new Class Где-то там внутри
// а так все зависимости видны в сигнатуре конструктора
// есть такая штуа как контейнер внедреня зависимостей,
// он модет сам подставлять зависимости
// у нас такого нет, так что будешь делать new Class внутри констркутора


private function __construct()
{
// так делать нельзя, но допускается
// в рамках данного проекта.
$this->security = new Security();
$this->userRepository = new UserRepository();
}

// вот через этот метод мы будем получать инстанс
// если класс не был инстанцирован, делаем это
// если был, пропускаем этот шаш и сразу возвращаем инстанс
// итого: у нас всегда будет один и тот же экзмепляр этого класса
public static function getInstance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}

return self::$instance;
}

// простой геттер
public function getCurrentUser()
{
return $this->currentUser;
}

// простой сеттер
public function setCurrentUser(User $user)
{
return $this->currentUser = $user;
}

// регистрация
public function registerUser(array $data)
{
$data['password'] = $this->security->encode($data['password']);
unset($data['password_check']);
if ($this->userRepository->add($data)) {
return true;
} else {
$this->errors[] = $this->userRepository->getError();
return false;
}
}

// авторизация
public function authUser(array $data = null)
{
if ($this->isAuthorized()) {
return true;
}
$user = new User();
$passwordHash = $this->userRepository->get(['email' => $data['email']], 'password');
$success = $this->security->checkPassword($data['password'], $passwordHash[0]['password']);
if (!$success) {
$this->errors['message'] = 'Неверный email или пароль';
return false;
}
$user->getByEmail($data['email']);
$cookie = $this->security->encode($data['email'] . $passwordHash[0]['password']);
setcookie('isAuth', $cookie, strtotime('+30 days'), '/');
$this->userRepository->edit($user->getId(), ['cookie' => $cookie]);
$this->setCurrentUser($user);

return true; //smth|bool|User|че там надо;
}

public function isAuthorized()
{

$user = new User();

if (!isset($_COOKIE['isAuth'])) {
return false;
}

$result = $this->userRepository->get(['cookie' => $_COOKIE['isAuth']]);
if (!empty($result)) {
$user->getByEmail($result[0]['email']);
$this->setCurrentUser($user);
return true;
}

setcookie('isAuth', '', strtotime( '-1 days' ), '/');
return false;
}
/**
* @return array
*/
public function getErrors(): array
{
return $this->errors;
}
}
13 changes: 13 additions & 0 deletions application/components/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* Created by PhpStorm.
* User: oleg
* Date: 27.02.18
* Time: 19:53
*/

namespace application\components;

class Request{

}
29 changes: 29 additions & 0 deletions application/components/Security.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: oleg
* Date: 05.01.18
* Time: 17:45
*/

namespace application\components;

use application\contracts\PasswordEncoder;

class Security implements PasswordEncoder
{
/**
* Хэширофание пароля
* @param string $password
* @return string
*/
public function encode(string $password): string
{
return password_hash($password, PASSWORD_DEFAULT);
}

public function checkPassword(string $password, string $hash): bool
{
return password_verify($password, $hash);
}
}
Loading