
Skrypt służy do logowania, rejestracj, zmiany hasła itp dla małej wyszukiwarki firm, praca robiona na praktyki szkolne.
Kontroler:
<?php class Controller_User extends Controller_Layout { public function action_index() { } public function action_login() { // If user already signed-in if (Auth::instance()->logged_in() != 0) { //redirect to the user account HTTP::redirect('index/index'); } $this->template->content = View::factory('user/login'); if ($this->request->post()) { 'username', 'password', 'remember' )); $user = ORM::factory('User'); $validation = $user->validate_login($this->request->post()); if ($validation->check()) { $user = Auth::instance()->login( $this->request->post('username'), $this->request->post('password'), $this->request->post('remember') ); if ($user) { HTTP::redirect('index/index'); } else { } } else { $this->template->errors = $validation->errors('register'); } } } public function action_logout() { // Log user out Auth::instance()->logout(); // Redirect to login page HTTP::redirect('user/login'); } public function action_register() { // If user already signed-in if (Auth::instance()->logged_in() != 0) { //redirect to the user account HTTP::redirect('user/profil'); } if ($this->request->post()) { 'username', 'email', 'username', )); $user = ORM::factory('User'); $validation = $user->validate_register($this->request->post()); if ($validation->check()) { $user->username = $this->request->post('username'); $user->password = $this->request->post('password'); $user->email = $this->request->post('email'); $user->save(); $user->add('roles', $login_role); Helper::notice('Rejestracja ukończona. Teraz możesz się zalogować'); } else { $this->template->errors = $validation->errors('register'); } } $this->template->content = View::factory('user/register'); } public function action_profil() { Helper::chceck_login(); $this->template->content = View::factory('user/profil'); } public function action_changepass() { $user = new Model_User; $validation = $user->validate_change_pass($this->request->post()); if ($validation->check()) { //zmieniamy hasło $user = ORM::factory('User', Auth::instance()->get_user()->id); $user->password = $this->request->post('password'); $user->save(); $user->saved() ? Helper::notice('Hasło zostało zmienione') : Helper::notice('Błąd danych. Hasło nie zostało zmienione') ; } else { //jeśli validacja się nie powiodła , uruchamiamy kotroler profilu. //Wysyłamy do niego także informacje o błędach $this->template->errors = $validation->errors('change_pass'); $this->action_profil(); } } public function action_changeemail() { $user = new Model_User; $validation = $user->validate_change_email($this->request->post()); if ($validation->check()) { //jeśli wszystko przebiegło ok, zmieniamy email $user = ORM::factory('User', Auth::instance()->get_user()->id); $user->password = $this->request->post('email'); $user->save(); $user->saved() ? Helper::notice('Email został zmieniony') : Helper::notice('Błąd danych. Email nie został zmieniony') ; } else { $this->template->errors = $validation->errors('change_email'); $this->action_profil(); } } }
Model: (wydaje mi się że nie napisałem go poprawnie , i w normalnej aplikacju było by to nie do przyjecia, ale nie mam pomysłu jak to "unormalnić")
<?php class Model_User extends Model_Auth_User { ), ), ), ), ), ); ), ), ) ); public function validate_register($postvalues) { $array = Validation::factory($postvalues) ->rules('username', $this->rules_register['username']) ->rules('email', $this->rules_register['email']) ->rules('email_confirm', $this->rules_register['email_confirm']) ->rules('password', $this->rules_register['password']) ->rules('password_confirm', $this->rules_register['password_confirm']) ; return $array; } public function validate_login($postvalues) { $array = Validation::factory($postvalues) ->rules('username', $this->rules_login['username']) ->rules('password', $this->rules_login['password']) ; return $array; } /** * validacja zmiany hasła. * @param array $postvalues */ public function validate_change_pass($postvalues) { $array = Validation::factory($postvalues) ->rules('password_old', $this->rules_register['password']) ->rule('password_old','Model_User::check_pass') ->rules('password', $this->rules_register['password']) ->rules('password_confirm', $this->rules_register['password_confirm']) ; return $array; } /** * validacja zmiany emaila * @param type $postvalues */ public function validate_change_email($postvalues) { $array = Validation::factory($postvalues) ->rules('password_email', $this->rules_register['password']) ->rule('password_email','Model_User::check_pass') ->rules('email',$this->rules_login['email']) ; return $array; } { // Check if the username already exists in the database ->from('users') ->where($column, '=', $value) ->execute() ->get('total'); } /** * Sprawdza czy podane hasło jest zgodne z hasłem * aktualnie zalogowanego usera * @param string $pass Nie zhashowane hasło */ { $pass_hash = Auth::instance()->hash_password($pass); return ( Auth::instance()->get_user()->password == $pass_hash) ? true : false ; } }
Funkcja Helper::filter_post
/** * Filtruje liste zmiennych z post * @param object $controller * @param array $list */ { foreach ($list as $value) { $controller->request->post($value, Helper::filter($controller->request->post($value))); } }