This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/application/classes/Model/Module/Method/Token.php

113 lines
3.1 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* OSB Application Module Method Token Model
*
* @package OSB
* @category Models
* @author Deon George
2013-03-19 22:35:19 +00:00
* @copyright (c) 2009-2013 Open Source Billing
* @license http://dev.osbill.net/license.html
2010-11-29 22:41:08 +00:00
*/
class Model_Module_Method_Token extends ORM_OSB {
// This module doesnt keep track of column updates automatically
protected $_updated_column = FALSE;
2010-11-29 22:41:08 +00:00
// Relationships
protected $_belongs_to = array(
'account'=>array(),
'module_method'=>array('foreign_key'=>'method_id'),
2010-11-29 22:41:08 +00:00
);
protected $_has_one = array(
'record_id'=>array(),
);
2011-10-12 03:52:04 +00:00
public function method(array $modmeth) {
list($module,$method) = $modmeth;
if (! $method instanceof Model_Module_Method) {
if (is_numeric($module))
2012-11-09 23:13:57 +00:00
$mo = ORM::factory('Module',$module);
2011-10-12 03:52:04 +00:00
elseif (is_string($module))
2012-11-09 23:13:57 +00:00
$mo = ORM::factory('Module',array('name'=>$module));
2011-10-12 03:52:04 +00:00
elseif (! $module instanceof Model_Module)
throw new Kohana_Exception('Unknown module :module',array(':module'=>serialize($module)));
else
$mo = $module;
if (! $mo->loaded())
throw new Kohana_Exception('Unknown module :module - not loaded?',array(':module'=>$mo->id));
if (is_numeric($method))
2012-11-09 23:13:57 +00:00
$mmo = ORM::factory('Module_Method',$method);
2011-10-12 03:52:04 +00:00
elseif (is_string($method))
2012-11-09 23:13:57 +00:00
$mmo = ORM::factory('Module_Method',array('name'=>$method,'module_id'=>$mo->id));
2011-10-12 03:52:04 +00:00
else
throw new Kohana_Exception('Unknown method :method',array(':method'=>serialize($method)));
} else
$mmo = $method;
if (! $mmo->loaded())
throw new Kohana_Exception('Unknown method :method - not loaded?',array(':method'=>$mmo->id));
$this->method_id = $mmo->id;
return $this;
}
public function account($account) {
if (! $account instanceof Model_Account) {
if (is_numeric($account))
2012-11-09 23:13:57 +00:00
$ao = ORM::factory('Account',$account);
2011-10-12 03:52:04 +00:00
else
throw new Kohana_Exception('Unknown account :account',array(':account'=>serialize($account)));
} else
$ao = $account;
$this->account_id = $ao->id;
return $this;
}
public function uses($uses) {
$this->uses = $uses;
return $this;
}
public function expire($expire) {
$this->date_expire = $expire;
return $this;
}
2012-02-22 08:15:46 +00:00
// @todo Login Reset: When called within a timelimit (so existing token already exists), is returning true but password reset emails have blanks where the tokens are
2011-10-12 03:52:04 +00:00
public function generate() {
if (! $this->account_id OR ! $this->method_id OR ! ($this->date_expire OR $this->uses))
return NULL;
// Check we dont already have a valid token
2012-11-09 23:13:57 +00:00
$mmto = ORM::factory('Module_Method_Token')
2011-10-12 03:52:04 +00:00
->where('account_id','=',$this->account_id)
->where('method_id','=',$this->method_id)
->find();
if ($mmto->loaded()) {
2013-01-12 00:20:46 +00:00
// Check that the token is still good
if ((is_null($mmto->date_expire) OR $mmto->date_expire > time()) AND (is_null($mmto->uses) OR $mmto->uses > 0)) {
$this->token = $mmto->token;
return $this->token;
// Token expired
} else
2011-10-12 03:52:04 +00:00
$mmto->delete();
}
$this->token = md5(sprintf('%s:%s:%s',$this->account_id,$this->method_id,time()));
$this->save();
return $this->saved() ? $this->token : NULL;
}
2010-11-29 22:41:08 +00:00
}
?>