Skip to content

Commit

Permalink
Объединен с модулем Reflinks
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Mar 25, 2015
1 parent 955514f commit c051f21
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 0 deletions.
52 changes: 52 additions & 0 deletions classes/Controller/Reflink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' );

/**
* @package KodiCMS/Reflink
* @category Controller
* @author butschster <[email protected]>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Controller_Reflink extends Controller_System_Controller {

public function action_index()
{
$code = $this->request->param('code');
if ($code === NULL)
{
Model_Page_Front::not_found();
}

$reflink_model = ORM::factory('user_reflink', $code);

if (!$reflink_model->loaded())
{
Messages::errors(__('Reflink not found'));
$this->go_home();
}

$next_url = Arr::get($reflink_model->data, 'next_url');

try
{
Database::instance()->begin();
Reflink::factory($reflink_model)->confirm();
$reflink_model->delete();
Database::instance()->commit();
}
catch (Kohana_Exception $e)
{
Database::instance()->rollback();
Messages::errors($e->getMessage());
}

if (Valid::url($next_url))
{
$this->go($next_url);
}

$this->go_home();
}

}
103 changes: 103 additions & 0 deletions classes/Model/User/Reflink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php defined('SYSPATH') or die('No direct script access.');

/**
* @package KodiCMS/Reflink
* @category Model
* @author butschster <[email protected]>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Model_User_Reflink extends ORM {

protected $_primary_key = 'code';

protected $_created_column = array(
'column' => 'created',
'format' => 'Y-m-d H:i:s'
);

protected $_belongs_to = array(
'user' => array(),
);

protected $_serialize_columns = array('data');

/**
* Generate new reflink code
*
* @param Model_User $user
* @param integer $type reflink type
* @param string $data string stored to reflink in database
* @return string
*/
public function generate(Model_User $user, $type, $data = NULL)
{
if (!$user->loaded())
{
throw new Reflink_Exception(' User not loaded ');
}

$type = URL::title($type, '_');

$reflink = $this
->reset(FALSE)
->where('user_id', '=', $user->id)
->where('type', '=', $type)
->where('created', '>', DB::expr('CURDATE() - INTERVAL 1 HOUR'))
->find();

if (!$reflink->loaded())
{
$values = array(
'user_id' => (int) $user->id,
'code' => uniqid(TRUE) . sha1(microtime()),
'type' => $type,
'data' => $data
);

$reflink = ORM::factory('user_reflink')
->values($values, array_keys($values))
->create();
}
else
{
$reflink
->set('data', $data)
->update();
}

return $reflink->code;
}

/**
* Delete reflinks from database
* Model must be loaded
*
* @return integer
*/
public function delete()
{
if (!$this->loaded())
{
throw new Reflink_Exception('Model not loaded or not found.');
}

return DB::delete($this->table_name())
->where('user_id', '=', $this->user_id)
->where('type', '=', $this->type)
->execute($this->_db);
}

/**
* Delete old reflinks from database
*
* @return integer
*/
public function clear_old()
{
return DB::delete($this->table_name())
->where('created', '<', DB::expr('CURDATE() - INTERVAL 1 DAY'))
->execute($this->_db);
}
}
37 changes: 37 additions & 0 deletions classes/Reflink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' );

/**
* @package KodiCMS/Reflink
* @author butschster <[email protected]>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
abstract class Reflink {

public static function factory(Model_User_Reflink $reflink)
{
$class_name = 'Reflink_' . ucfirst($reflink->type);

if (!class_exists($class_name))
{
throw new Reflink_Exception('Class :class not exists', array(
':class' => $class_name));
}

return new $class_name($reflink);
}

/**
*
* @var Model_User_Reflink
*/
protected $_model = NULL;

public function __construct(Model_User_Reflink $reflink)
{
$this->_model = $reflink;
}

abstract public function confirm();
}
11 changes: 11 additions & 0 deletions classes/Reflink/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php defined('SYSPATH') OR die('No direct access allowed.');

/**
* @package KodiCMS/Reflink
* @category Exception
* @author butschster <[email protected]>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Reflink_Exception extends Kohana_Exception {}
38 changes: 38 additions & 0 deletions classes/Reflink/Forgot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' );

/**
* @package KodiCMS/Reflink
* @category Drivers
* @author butschster <[email protected]>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Reflink_Forgot extends Reflink {

public function confirm()
{
$new_password = Text::random();
$this->_model->user->change_email( $new_password );

try
{
Email_Type::get('user_new_password')->send(array(
'username' => $this->_model->user->username,
'email' => $this->_model->user->email,
'password' => $new_password
));

Messages::success(__('An email has been send with your new password!'));

$this->_model->delete();

return TRUE;
}
catch (Kohana_Exception $e)
{
throw new Reflink_Exception('Email :email not send', array(
':email' => $this->_model->user->email));
}
}
}
36 changes: 36 additions & 0 deletions classes/Reflink/Register.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php defined( 'SYSPATH' ) or die( 'No direct access allowed.' );

/**
* @package KodiCMS/Reflink
* @category Drivers
* @author butschster <[email protected]>
* @link http://kodicms.ru
* @copyright (c) 2012-2014 butschster
* @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt
*/
class Reflink_Register extends Reflink {

public function confirm()
{
try
{
$role = ORM::factory('role', array('name' => 'login'));
$this->_model->user->add('roles', $role);

Email_Type::get('user_registered')->send(array(
'username' => $this->_model->user->username,
'email' => $this->_model->user->email
));

Messages::success(__('Thank you for registration!'));

$this->_model->delete();

return TRUE;
}
catch (Kohana_Exception $e)
{
throw new Reflink_Exception('Something went wrong');
}
}
}
12 changes: 12 additions & 0 deletions i18n/ru.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,16 @@
'Section permissions' => 'Доступ к разделам',
'View user permissions' => 'Видеть права пользователя',
'You don\'t have permissions to :permission' => 'У вас нет прав на :permission',

//============Reflinks============//
'Reflink generate error' => 'Ошибка во время генерации временной ссылки',
'Email with reflink send to address set in your profile'
=> 'Письмо с временной ссылкой отправлено на email адрес указанный в вашем профиле',

'Model not loaded or not found.' => 'Модель не загружена',

'Hello, :username!' => 'Здравствуйте, :username!',
'New password: :password' => 'Новый пароль: :password',
'To proceed with the recovery password follow this :link'
=> 'Для продолжения процедуры восстановления пароля проследуйте по ссылке :link',
);
11 changes: 11 additions & 0 deletions init.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
<?php defined('SYSPATH') or die('No direct access allowed.');

// Для проверки одноразовых ссылок
Route::set('reflink', 'reflink/<code>(<suffix>)', array(
'code' => '[A-Za-z0-9]+',
'suffix' => URL_SUFFIX
))
->defaults(array(
'controller' => 'reflink',
'action' => 'index'
));


/*
* Set current lang
*/
Expand Down
10 changes: 10 additions & 0 deletions install/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,14 @@ CREATE TABLE IF NOT EXISTS `__TABLE_PREFIX__roles_permissions` (
`action` varchar(255) NOT NULL,
UNIQUE KEY `role_id` (`role_id`,`action`),
CONSTRAINT `__TABLE_PREFIX__roles_permissions_ibfk_1` FOREIGN KEY (`role_id`) REFERENCES `__TABLE_PREFIX__roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `__TABLE_PREFIX__user_reflinks` (
`user_id` int(10) unsigned NOT NULL,
`type` varchar(50) NOT NULL,
`code` varchar(255) NOT NULL,
`data` text,
`created` datetime NOT NULL,
UNIQUE KEY `unique_reflink` (`user_id`,`code`),
CONSTRAINT `__TABLE_PREFIX__user_reflinks_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `__TABLE_PREFIX__users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

0 comments on commit c051f21

Please sign in to comment.