Добавил возможность авторизоваться. При создании точки сохраняется время и автор. Точку может добавить только авторизованный пользователь.
This commit is contained in:
parent
97fab94994
commit
5a72857687
|
|
@ -30,6 +30,7 @@ class App
|
||||||
private static $config = array();
|
private static $config = array();
|
||||||
private static $urlParams = array();
|
private static $urlParams = array();
|
||||||
public static $DB = null;
|
public static $DB = null;
|
||||||
|
public static $user = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Разбивает урл на контроллер, метод и выбирает параметры.
|
* Разбивает урл на контроллер, метод и выбирает параметры.
|
||||||
|
|
@ -60,6 +61,7 @@ class App
|
||||||
public static function run($config = '')
|
public static function run($config = '')
|
||||||
{
|
{
|
||||||
ob_start();
|
ob_start();
|
||||||
|
session_start();
|
||||||
self::$config = $config ? include_once $config : array();
|
self::$config = $config ? include_once $config : array();
|
||||||
self::parseUrl();
|
self::parseUrl();
|
||||||
|
|
||||||
|
|
@ -69,6 +71,9 @@ class App
|
||||||
{
|
{
|
||||||
$confDB = self::getConfig('DB');
|
$confDB = self::getConfig('DB');
|
||||||
self::$DB = new PDO('mysql:host=' . $confDB['host'] . ';dbname=' . $confDB['dbname'].';charset=utf8;', $confDB['user'], $confDB['password']);
|
self::$DB = new PDO('mysql:host=' . $confDB['host'] . ';dbname=' . $confDB['dbname'].';charset=utf8;', $confDB['user'], $confDB['password']);
|
||||||
|
|
||||||
|
// Работать с пользователями моно только при наличии БД
|
||||||
|
self::userInit();
|
||||||
}
|
}
|
||||||
catch (PDOException $e)
|
catch (PDOException $e)
|
||||||
{
|
{
|
||||||
|
|
@ -94,6 +99,35 @@ class App
|
||||||
die($message ? $message : '404 - Not Found');
|
die($message ? $message : '404 - Not Found');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Пытаемся восстановить пользователя из сессии
|
||||||
|
public static function userInit()
|
||||||
|
{
|
||||||
|
if (isset($_SESSION['user_id']) && isset($_SESSION['user_key']) && $_SESSION['user_id'] && $_SESSION['user_key'] )
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
$user = $user->getByPK((int)$_SESSION['user_id']);
|
||||||
|
|
||||||
|
if ( $user && md5($user->uid) == $_SESSION['user_key'] )
|
||||||
|
{
|
||||||
|
self::$user = $user;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function userLogin($user)
|
||||||
|
{
|
||||||
|
$_SESSION['user_id'] = $user->id;
|
||||||
|
$_SESSION['user_key'] = md5($user->uid);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function userLogout()
|
||||||
|
{
|
||||||
|
$_SESSION['user_id'] = null;
|
||||||
|
$_SESSION['user_key'] = null;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Перенаправление на другой URL
|
* Перенаправление на другой URL
|
||||||
* @param type $url
|
* @param type $url
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ class IndexController extends Controller
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Действие по умолчанию - вывоим каталог товаров.
|
* Действие по умолчанию
|
||||||
*/
|
*/
|
||||||
static function actionIndex()
|
static function actionIndex()
|
||||||
{
|
{
|
||||||
|
|
@ -16,6 +16,9 @@ class IndexController extends Controller
|
||||||
self::addStyle('/css/bootstrap.min.css');
|
self::addStyle('/css/bootstrap.min.css');
|
||||||
self::addStyle('/css/bootstrap-theme.min.css');
|
self::addStyle('/css/bootstrap-theme.min.css');
|
||||||
|
|
||||||
|
if (!App::$user )
|
||||||
|
self::addScript('//ulogin.ru/js/ulogin.js');
|
||||||
|
|
||||||
$category = new Category();
|
$category = new Category();
|
||||||
|
|
||||||
$res = App::$DB->query("SELECT COUNT(`id`) pointsCount FROM `points`")->fetch(PDO::FETCH_ASSOC);
|
$res = App::$DB->query("SELECT COUNT(`id`) pointsCount FROM `points`")->fetch(PDO::FETCH_ASSOC);
|
||||||
|
|
@ -23,7 +26,54 @@ class IndexController extends Controller
|
||||||
|
|
||||||
self::render('indexTemplate.php', array(
|
self::render('indexTemplate.php', array(
|
||||||
'categories' => $category->getAll('', 'ord'),
|
'categories' => $category->getAll('', 'ord'),
|
||||||
|
'user' => App::$user,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function actionUser()
|
||||||
|
{
|
||||||
|
print $_SERVER['HTTP_HOST'];
|
||||||
|
var_dump(App::$user->id, App::$user->nick, App::$user->name, App::$user->network);
|
||||||
|
}
|
||||||
|
|
||||||
|
static function actionLogout()
|
||||||
|
{
|
||||||
|
App::userLogout();
|
||||||
|
App::redirect('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
static function actionLogin()
|
||||||
|
{
|
||||||
|
$source = file_get_contents('http://ulogin.ru/token.php?token=' . $_POST['token'] . '&host=' . $_SERVER['HTTP_HOST']);
|
||||||
|
$uLoginUser = json_decode($source, true);
|
||||||
|
$uLoginUser['identity'] = md5($uLoginUser['uid'].$uLoginUser['network'].'=P');
|
||||||
|
|
||||||
|
$user = new User();
|
||||||
|
$user->getByUID($uLoginUser['identity']);
|
||||||
|
|
||||||
|
if( !$user->id )
|
||||||
|
{
|
||||||
|
$res = $user->add(array(
|
||||||
|
'uid' => $uLoginUser['identity'],
|
||||||
|
'nick' => $uLoginUser['nickname'],
|
||||||
|
'name' => $uLoginUser['first_name'],
|
||||||
|
'profile' => $uLoginUser['profile'],
|
||||||
|
'network' => $uLoginUser['network']
|
||||||
|
));
|
||||||
|
|
||||||
|
if ($res)
|
||||||
|
{
|
||||||
|
$user = new User();
|
||||||
|
$user = $user->getByPK($res);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
App::userLogin($user);
|
||||||
|
App::redirect('/');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -81,10 +81,14 @@ class JsonController extends Controller
|
||||||
if(!( isset($_POST['source']) )) $errors[5]='Не передано поле источкика информации. Допускается пустое значение.';
|
if(!( isset($_POST['source']) )) $errors[5]='Не передано поле источкика информации. Допускается пустое значение.';
|
||||||
if(!( isset($_POST['lat']) && $_POST['lat'] )) $errors[6]='Ошибочное значение широты';
|
if(!( isset($_POST['lat']) && $_POST['lat'] )) $errors[6]='Ошибочное значение широты';
|
||||||
if(!( isset($_POST['lng']) && $_POST['lng'] )) $errors[7]='Ошибочное значение долготы';
|
if(!( isset($_POST['lng']) && $_POST['lng'] )) $errors[7]='Ошибочное значение долготы';
|
||||||
|
if(!App::$user ) $errors[8]='Вы не авторизованы! Пожалуйста, войдите.';
|
||||||
|
|
||||||
|
|
||||||
if (empty($errors))
|
if (empty($errors))
|
||||||
{
|
{
|
||||||
$point = new Point();
|
$point = new Point();
|
||||||
|
$_POST['author'] = App::$user->id;
|
||||||
|
$_POST['date'] = time();
|
||||||
$point->add($_POST);
|
$point->add($_POST);
|
||||||
|
|
||||||
if ( $point->id )
|
if ( $point->id )
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class User extends Model
|
||||||
|
{
|
||||||
|
function __construct()
|
||||||
|
{
|
||||||
|
|
||||||
|
$this->_tableName_ = 'users';
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
function getByUID($uid)
|
||||||
|
{
|
||||||
|
$res = App::$DB->query('SELECT `id` FROM ' . $this->_tableName_ . ' WHERE `uid` = "' . $uid . '"');
|
||||||
|
if ($res)
|
||||||
|
{
|
||||||
|
$res = $res->fetch(PDO::FETCH_ASSOC);
|
||||||
|
return parent::getByPK((int)$res['id']);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -25,11 +25,32 @@
|
||||||
<button class="btn btn-default" type="button" onclick="showMeOnTheMap()"><span class="glyphicon glyphicon-screenshot"></span> Я на карте</button>
|
<button class="btn btn-default" type="button" onclick="showMeOnTheMap()"><span class="glyphicon glyphicon-screenshot"></span> Я на карте</button>
|
||||||
</td>
|
</td>
|
||||||
<td class="text-right">
|
<td class="text-right">
|
||||||
|
<?php if ($user): ?>
|
||||||
<button class="btn btn-default" type="button" id="addPointButton" onclick="activateAddPoint()"><span class="glyphicon glyphicon-plus-sign"></span> Добавить точку</button>
|
<button class="btn btn-default" type="button" id="addPointButton" onclick="activateAddPoint()"><span class="glyphicon glyphicon-plus-sign"></span> Добавить точку</button>
|
||||||
|
<a class="btn btn-default" type="button" href="/index/logout"><span class="glyphicon glyphicon-ban-circle"></span> Выйти</a>
|
||||||
|
<?php else:?>
|
||||||
|
<button class="btn btn-default" type="button" id="loginButton" onclick="$('#loginModal').modal('show');"><span class="glyphicon glyphicon-user"></span> Войти</button>
|
||||||
|
<?php endif;?>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="Вход на сайт" aria-hidden="true">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||||
|
<h4 class="modal-title" id="myModalLabel">Вход на сайт</h4>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<div style="display: inline-block;" id="uLogin" data-ulogin="display=panel;fields=first_name,nickname;providers=vkontakte,twitter,facebook,foursquare,google,yandex,mailru;hidden=other;redirect_uri=http%3A%2F%2F<?= $_SERVER['HTTP_HOST'] ?>%2Findex%2Flogin"></div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-default" data-dismiss="modal">Отмена</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="modal fade" id="addPointModal" tabindex="-1" role="dialog" aria-labelledby="Добавление новой точки" aria-hidden="true">
|
<div class="modal fade" id="addPointModal" tabindex="-1" role="dialog" aria-labelledby="Добавление новой точки" aria-hidden="true">
|
||||||
<div class="modal-dialog">
|
<div class="modal-dialog">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue