diff --git a/classes/Controller/Account.php b/classes/Controller/Login.php similarity index 51% rename from classes/Controller/Account.php rename to classes/Controller/Login.php index 0ce5671..ca81c56 100644 --- a/classes/Controller/Account.php +++ b/classes/Controller/Login.php @@ -1,4 +1,4 @@ diff --git a/classes/Controller/User/Account.php b/classes/Controller/User/Account.php deleted file mode 100644 index cedc14f..0000000 --- a/classes/Controller/User/Account.php +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/classes/Model/Auth/UserDefault.php b/classes/Model/Auth/UserDefault.php deleted file mode 100644 index e59d05f..0000000 --- a/classes/Model/Auth/UserDefault.php +++ /dev/null @@ -1,4 +0,0 @@ - diff --git a/classes/Model/Module/Method/Token.php b/classes/Model/Module/Method/Token.php index 21eb00a..5f3eb98 100644 --- a/classes/Model/Module/Method/Token.php +++ b/classes/Model/Module/Method/Token.php @@ -1,4 +1,4 @@ diff --git a/classes/lnAuth/Auth/ORM.php b/classes/lnAuth/Auth/ORM.php index 50abdbe..84aafd9 100644 --- a/classes/lnAuth/Auth/ORM.php +++ b/classes/lnAuth/Auth/ORM.php @@ -9,20 +9,7 @@ * @copyright (c) 2014 Deon George * @license http://dev.leenooks.net/license.html */ -class lnAuth_Auth_ORM extends Kohana_Auth_ORM { - /** - * We need to override Kohana's __construct(), for tasks, which attempt to open a session - * and probably dont have access to PHP sessions path. - * Tasks dont need sessions anyway? - */ - public function __construct($config = array()) { - // Save the config in the object - $this->_config = $config; - - if (PHP_SAPI !== 'cli') - parent::__construct($config); - } - +abstract class lnAuth_Auth_ORM extends lnApp_Auth_ORM { /** * Get the user that a token applies to * @@ -31,7 +18,7 @@ class lnAuth_Auth_ORM extends Kohana_Auth_ORM { * @param $token The token * @return Model_Account|NULL The user that the token is valid for. */ - private function _get_token_user($token) { + protected function _get_token_user($token) { // This has been implemented, as we sometimes we seem to come here twice static $uo = NULL; @@ -89,104 +76,7 @@ class lnAuth_Auth_ORM extends Kohana_Auth_ORM { } /** - * Logs a user in. - * - * @param string username - * @param string password - * @param boolean enable autologin - * @return boolean - */ - protected function _login($user,$password,$remember) { - if (! is_object($user)) { - $username = $user; - - // Load the user - $user = ORM::factory($this->_model); - $user->where('email','=',$username)->find(); - - // If no user loaded, return - if (! $user->loaded()) - return FALSE; - } - - // Create a hashed password - if (is_string($password)) - $password = $this->hash($password); - - // If we have the right password, we'll check the status of the account - if ($user->password === $password AND $user->active) { - // Record our session ID, we may need to update our DB when we get a new ID - $oldsess = session_id(); - - // Finish the login - $this->complete_login($user); - - // Do we need to update databases with our new sesion ID - $sct = Kohana::$config->load('config')->session_change_trigger; - if (session_id() != $oldsess AND count($sct)) - foreach ($sct as $t => $c) - if (Config::module_exist($t)) - foreach (ORM::factory(ucwords($t))->where($c,'=',$oldsess)->find_all() as $o) - $o->set('session_id',session_id()) - ->update(); - - if (! $user->has_any('group',ORM::factory('Group',array('name'=>'Registered Users'))->list_childgrps(TRUE))) - HTTP::redirect(URL::link('user','account/activate')); - - return TRUE; - } - - // Login failed - return FALSE; - } - - /** - * Determine if a user is authorised to view an account - * - * @param Model_Account Account Ojbect to validate if the current user has access - * @return boolean TRUE if authorised, FALSE if not. - */ - public function authorised(Model_Account $ao) { - return (($uo = $this->get_user()) AND $uo->loaded() AND ($uo == $ao OR in_array($ao->id,$uo->RTM->customers($uo->RTM)))); - } - - /** - * Gets the currently logged in user from the session. - * Returns NULL if no user is currently logged in. - * - * @param boolean Check token users too - * @return mixed - */ - public function get_user($default=NULL,$tokenuser=TRUE) { - // If we are a CLI, we are not logged in - if (PHP_SAPI === 'cli') - throw new Kohana_Exception('Calling :method from the CLI is not allowed!',array(':method'=>__METHOD__)); - - // Get the current user - $uo = parent::get_user($default); - - // If we are not logged in, see if there is token for the user - if (is_null($uo) AND $tokenuser AND ($token=Session::instance()->get('token')) OR (! empty($_REQUEST['token']) AND $token=$_REQUEST['token'])) - $uo = $this->_get_token_user($token); - - return $uo; - } - - public function get_groups() { - return is_null($x=$this->get_user()) ? ORM::factory('Group')->where('id','=',0)->find_all() : $x->groups(); - } - - // Override Kohana Auth requirement to have a hash_key - public function hash($str) { - switch ($this->_config['hash_method']) { - case '' : return $str; - case 'md5': return md5($str); - default: return hash_hmac($this->_config['hash_method'], $str, $this->_config['hash_key']); - } - } - - /** - * OSB authentication is controlled via database queries. + * Authentication is controlled via database queries. * * This method can be used to test two situations: * 1) Is the user logged in? ($role == FALSE) diff --git a/classes/lnAuth/Controller/Account.php b/classes/lnAuth/Controller/Account.php deleted file mode 100644 index d2cb71f..0000000 --- a/classes/lnAuth/Controller/Account.php +++ /dev/null @@ -1,14 +0,0 @@ - diff --git a/classes/lnAuth/Controller/Admin/Module.php b/classes/lnAuth/Controller/Admin/Module.php index 57c7cba..8644a0c 100644 --- a/classes/lnAuth/Controller/Admin/Module.php +++ b/classes/lnAuth/Controller/Admin/Module.php @@ -74,7 +74,7 @@ abstract class lnAuth_Controller_Admin_Module extends Controller_Module { $auth_required[$cn] = $rdp['auth_required']; foreach ($r->getMethods() as $method) - if ($method->class == $cn AND preg_match('/^action_/',$method->name)) + if (preg_match('/^action_/',$method->name)) array_push($methods,str_replace('action_',strtolower($x.($sc ? '_'.$sc : '').':'),$method->name)); } } diff --git a/classes/lnAuth/Controller/Admin/Module/Method.php b/classes/lnAuth/Controller/Admin/Module/Method.php index f98640e..ad19238 100644 --- a/classes/lnAuth/Controller/Admin/Module/Method.php +++ b/classes/lnAuth/Controller/Admin/Module/Method.php @@ -100,10 +100,8 @@ abstract class lnAuth_Controller_Admin_Module_Method extends Controller_Admin_Mo Block::factory() ->title(sprintf(_('Configure access to method (%s::%s)'),$mmo->controller(),$mmo->method())) ->title_icon('icon-plus-sign') - ->type('form') - ->body(View::factory('module/method/admin/edit') - ->set('o',$mmo) - ); + ->type('form-horizontal') + ->body(View::factory('module/method/admin/edit')->set('o',$mmo)); } } ?> diff --git a/classes/lnAuth/Controller/Login.php b/classes/lnAuth/Controller/Login.php new file mode 100644 index 0000000..e359b97 --- /dev/null +++ b/classes/lnAuth/Controller/Login.php @@ -0,0 +1,109 @@ +request->post()) { + $ao = ORM::factory('Account',array('id'=>$this->request->param('id'),'email'=>$this->request->post('email'))); + + if ($ao->loaded()) { + if ($ao->activated()) + HTTP::redirect('login'); + + elseif (! $ao->active) { + SystemMessage::factory() + ->title(_('Account NOT Activated')) + ->type('warning') + ->body(_('Your account cannot been activated, please contact us.')); + + } elseif ($ao->activate_code() == $this->request->post('code')) { + $go = ORM::factory('Group',array('name'=>'Registered Users')); + + $ago = ORM::factory('Account_Group',array('account_id'=>$ao,'group_id'=>$go)); + + if (! $ago->loaded()) { + $ago->account_id=$ao; + $ago->group_id=$go; + } + + $ago->active = TRUE; + $ago->save(); + + SystemMessage::factory() + ->title(_('Account Activated')) + ->type('info') + ->body(_('Your account has been activated.')); + } + + HTTP::redirect('welcome'); + } + + } elseif (! $this->request->param('id')) + HTTP::redirect('login/activate_resend'); + + Block::factory() + ->title('Activate account') + ->title_icon('fa-wrench') + ->type('form-horizontal') + ->body(View::factory('login/activate')->set('o',Session::instance()->get_once('activate'))->set('email',$this->request->query('email'))); + } + + /** + * Register for an account on the site + */ + public function action_register() { + $ao = ORM::factory('Account',$this->request->param('id')); + + if ($this->request->post()) { + $ao->values($this->request->post()); + $ao->active = "1"; + + if ($ao->changed() AND (! $this->save($ao))) + $ao->reload()->values($this->request->post()); + } + + if ($ao->loaded()) { + $co = Company::instance(); + + // Send our email with the token + $email = Email::factory('login_activate') + ->set('SITE',URL::base(TRUE,TRUE)) + ->set('SITE_ADMIN',$co->admin()->name()) + ->set('CODE',$ao->activate_code()) + ->set('EMAIL',$ao->email) + ->set('ID',$ao->id) + ->set('USER_NAME',$ao->name()); + + $email->to = array('email'=>array($ao->email=>$ao->name())); + $email->from = array('email'=>array($co->admin()->email=>$co->admin()->name())); + $email->subject = 'Please activate your account for '.$co->name(); + $email->deliver(); + + SystemMessage::factory() + ->title(_('Account Registered')) + ->type('info') + ->body(_('Please check your email for more instructions!')); + + Session::instance()->set('activate',$ao); + HTTP::redirect('login/activate/'.$ao->id); + } + + Block::factory() + ->type('form-horizontal') + ->title('Register Account') + ->title_icon('fa-edit') + ->body(View::factory('account/user/edit')->set('o',$ao)); + } +} +?> diff --git a/classes/lnAuth/Controller/TemplateDefault.php b/classes/lnAuth/Controller/TemplateDefault.php index ce9cb36..f002df0 100644 --- a/classes/lnAuth/Controller/TemplateDefault.php +++ b/classes/lnAuth/Controller/TemplateDefault.php @@ -31,9 +31,7 @@ abstract class lnAuth_Controller_TemplateDefault extends lnApp_Controller_Templa die(); } - return (($this->auth_required !== FALSE && Auth::instance()->logged_in(NULL,get_class($this).'|'.__METHOD__) === FALSE) || - (is_array($this->secure_actions) && array_key_exists($this->request->action(),$this->secure_actions) && - ! Auth::instance()->logged_in($this->secure_actions[$this->request->action()],get_class($this).'|'.__METHOD__))); + return parent::_auth_required(); } public function before() { diff --git a/classes/lnAuth/Controller/User/Account.php b/classes/lnAuth/Controller/User/Account.php deleted file mode 100644 index fd13fa8..0000000 --- a/classes/lnAuth/Controller/User/Account.php +++ /dev/null @@ -1,105 +0,0 @@ -TRUE, - 'resetpassword'=>TRUE, - ); - - /** - * Enable User to Edit their Account Details - */ - public function action_edit() { - if ($_POST AND $this->ao->values($_POST)->changed() AND (! $this->save($this->ao))) - $this->ao->reload(); - - Block::factory() - ->title(sprintf('Account: %s',$this->ao->accnum())) - ->title_icon('fa-wrench') - ->type('form-horizontal') - ->body(View::factory('account/user/edit')->set('o',$this->ao)); - } - - public function action_resetpassword() { - if ($this->request->post()) { - $validation = Validation::factory($this->request->post()) - ->rule('password','not_empty') - ->rule('password','min_length',array(':value',6)) - ->rule('password_confirm','matches',array(':validation',':field','password')); - - // Store our new values - $this->ao->values($this->request->post()); - - if (! $validation->check()) - SystemMessage::factory() - ->title(_('Record NOT updated')) - ->type('error') - ->body(_('Your password didnt pass validation.')); - - // Run validation and save - elseif ($this->ao->changed()) - if ($this->ao->save()) { - SystemMessage::factory() - ->title('Record updated') - ->type('success') - ->body(_('Your account record has been updated.')); - - // Log the password reset - $this->ao->log('Password reset'); - - HTTP::redirect('login'); - } - } - -// Make this a default JS load - if (Kohana::$environment >= Kohana::TESTING OR Request::current()->secure()) - Script::factory() - ->type('src') - ->data('media/js/jquery/jquery.validate-1.11.1.min.js'); - else - Script::factory() - ->type('src') - ->data('http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.min.js'); - - Script::factory() - ->type('stdin') - ->data(' -$("#reset").validate({ - wrapper: "div", - errorElement: "span", - - rules: { - password_confirm: { - equalTo: "input[name=password]", - }, - }, - highlight: function(element) { - $(element).parents(".control-group").removeClass("success").addClass("error"); - }, - success: function(element) { - $(element).parents(".control-group").removeClass("error").addClass("success"); - }, - errorPlacement: function(error, element) { - error.appendTo(element.parents(".controls")); - } -}); - '); - - Block::factory() - ->title(sprintf('Password Reset: %s',$this->ao->accnum())) - ->title_icon('fa-cog') - ->id('reset') - ->type('form-horizontal') - ->body(View::factory('account/user/resetpassword')->set('o',$this->ao)); - } -} -?> diff --git a/classes/lnAuth/Menu.php b/classes/lnAuth/Menu.php index fcac33a..a2b3f3a 100644 --- a/classes/lnAuth/Menu.php +++ b/classes/lnAuth/Menu.php @@ -36,7 +36,7 @@ abstract class lnAuth_Menu extends lnApp_Menu { return $result; } - public static function items($type) { + public static function items($type,array $list=array()) { $result = array(); if (empty(URL::$method_directory[$type])) @@ -51,9 +51,6 @@ abstract class lnAuth_Menu extends lnApp_Menu { if (empty($result[$mmo->id])) $result[$mmo->id] = $mmo; - if ($type == 'user') - $result[0] = 'logout'; - return self::collapse($result); } diff --git a/classes/lnAuth/Model/Account.php b/classes/lnAuth/Model/Account.php index 96626b1..9840d8b 100644 --- a/classes/lnAuth/Model/Account.php +++ b/classes/lnAuth/Model/Account.php @@ -9,7 +9,7 @@ * @copyright (c) 2014 Deon George * @license http://dev.leenooks.net/license.html */ -abstract class lnAuth_Model_Account extends Model_Auth_UserDefault { +abstract class lnAuth_Model_Account extends lnApp_Model_Account { // Relationships protected $_has_many = array( 'user_tokens'=>array('model'=>'user_token'), @@ -17,28 +17,6 @@ abstract class lnAuth_Model_Account extends Model_Auth_UserDefault { 'group'=>array('through'=>'account_group'), ); - protected $_has_one = array( - 'country'=>array('foreign_key'=>'id'), - 'currency'=>array('foreign_key'=>'id'), - 'language'=>array('foreign_key'=>'id'), - ); - - protected $_display_filters = array( - 'date_orig'=>array( - array('Site::Date',array(':value')), - ), - 'date_last'=>array( - array('Site::Date',array(':value')), - ), - 'active'=>array( - array('StaticList_YesNo::get',array(':value',TRUE)), - ), - ); - - protected $_form = array('id'=>'id','value'=>'name(TRUE)'); - - protected $_save_message = TRUE; - public function rules() { return Arr::merge(parent::rules(),array( 'id'=>array( @@ -50,50 +28,10 @@ abstract class lnAuth_Model_Account extends Model_Auth_UserDefault { )); } - /** - * Our account number format - */ - public function accnum() { - return sprintf('%s-%04s',Company::instance()->site(TRUE),$this->id); - } - - public function activate_code() { - return md5(sprintf('%s-%s-%s-%s',$this->accnum(),$this->date_orig,$this->date_last,$this->email)); - } - public function activated() { return $this->has_any('group',ORM::factory('Group',array('name'=>'Registered Users'))->list_childgrps(TRUE)); } - /** - * Get the groups that an account belongs to - */ - public function groups() { - $result = array(); - - foreach ($this->group->where_active()->find_all() as $go) - foreach ($go->list_parentgrps(TRUE) as $cgo) - if (empty($result[$cgo->id])) - $result[$cgo->id] = $cgo; - - return $result; - } - - public function log($message) { - // Log a message for this account - $alo = ORM::factory('Account_Log'); - $alo->account_id = $this->id; - $alo->ip = Request::$client_ip; - $alo->details = $message; - $alo->save(); - - return $alo->saved(); - } - - public function isAdmin() { - return FALSE; - } - /** * This function will extract the available methods for this account * This is used both for menu options and method security @@ -116,49 +54,15 @@ abstract class lnAuth_Model_Account extends Model_Auth_UserDefault { } /** - * Return an account name + * Return a token valid for this user */ - public function name() { - return trim(sprintf('%s %s',$this->first_name,$this->last_name)); - } - - /** - * Search for accounts matching a term - */ - public function list_autocomplete($term,$index,$value,array $label,array $limit=array(),array $options=NULL) { - $ao = Auth::instance()->get_user(); - - $this->clear(); - $this->where_active(); - - // Build our where clause - // First Name, Last name - if (preg_match('/\ /',$term)) { - list($fn,$ln) = explode(' ',$term,2); - - $this->where_open() - ->where('first_name','like','%'.$fn.'%') - ->and_where('last_name','like','%'.$ln.'%') - ->where_close(); - - } elseif (is_numeric($term)) { - $this->where('id','like','%'.$term.'%'); - - } elseif (preg_match('/\@/',$term)) { - $this->where('email','like','%'.$term.'%'); - - } else { - $this->where_open() - ->or_where('first_name','like','%'.$term.'%') - ->or_where('last_name','like','%'.$term.'%') - ->or_where('email','like','%'.$term.'%') - ->where_close(); - } - - // Restrict results to authorised accounts - // @todo - - return parent::list_autocomplete($term,$index,$value,$label,$limit,$options); + public function token($token_expire,$module,$method,$uses) { + return ORM::factory('Module_Method_Token') + ->method(array($module,$method)) + ->account($this) + ->uses($uses) + ->expire(time()+$token_expire) + ->generate(); } } ?> diff --git a/classes/lnAuth/Model/Auth/UserDefault.php b/classes/lnAuth/Model/Auth/UserDefault.php deleted file mode 100644 index b3ab399..0000000 --- a/classes/lnAuth/Model/Auth/UserDefault.php +++ /dev/null @@ -1,40 +0,0 @@ - array( - array('not_empty'), - array('min_length', array(':value', 4)), - array('max_length', array(':value', 127)), - array('email'), - ), - ); - } - - /** - * Complete our login - * - * For some database logins, we may not want to record the user last login - * details in the repository, so we just override that parent function - * here. - * - * We can also do some other post-login actions here. - */ - public function complete_login() { - return $this->log('Logged In'); - } - - abstract public function isAdmin(); -} -?> diff --git a/classes/lnAuth/Model/Group/Method.php b/classes/lnAuth/Model/Group/Method.php index 44dc1b1..2224323 100644 --- a/classes/lnAuth/Model/Group/Method.php +++ b/classes/lnAuth/Model/Group/Method.php @@ -3,7 +3,7 @@ /** * Application Module Method Model * - * @package lnAauth + * @package lnAuth * @category Models * @author Deon George * @copyright (c) 2014 Deon George diff --git a/config/auth.php b/config/auth.php deleted file mode 100644 index 5327678..0000000 --- a/config/auth.php +++ /dev/null @@ -1,17 +0,0 @@ - 'ORM', - 'hash_method' => 'md5', -); -?> diff --git a/config/config.php b/config/config.php index 0ca0cfd..d2e6169 100644 --- a/config/config.php +++ b/config/config.php @@ -3,6 +3,4 @@ return array ( 'method_security' => TRUE, // Enable User Based method security - 'session_change_trigger'=>array( // Updates to tables to make when our session ID is changed - ), ); diff --git a/views/account/user/edit.php b/views/account/user/edit.php deleted file mode 100644 index 6111f63..0000000 --- a/views/account/user/edit.php +++ /dev/null @@ -1,61 +0,0 @@ -
- -