Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [kohana] Ocena Modelu Autoryzacji Użytkowników
k3nsei
post
Post #1





Grupa: Zarejestrowani
Postów: 38
Pomógł: 0
Dołączył: 25.06.2008

Ostrzeżenie: (0%)
-----


Chciał bym abyście ocenili i skomentowali mój model autoryzacji. Jestem właśnie na etapie jego pisania więc chciał bym się dowiedzieć co zmodyfikować, dodać i usunąć.
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2.  
  3. class User_Model extends Model
  4. {
  5. protected $prefix;
  6. protected $session;
  7. protected $input;
  8.  
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. $this->session = new Session;
  13. $this->input = new Input;
  14. $this->prefix = config::item('database.default.table_prefix');
  15. }
  16.  
  17. public function makeAutoLogIn()
  18. {
  19. $cookie_data = cookie::get('Authentication');
  20. $login = NULL;
  21. $cookie_key = NULL;
  22.  
  23. if($cookie_data !== NULL and $this->session->get('isLogin', FALSE) === FALSE)
  24. {
  25. $data = explode('-', $cookie_data);
  26. $cookie_key = $this->session->id();
  27. $cookie_name = 'Authentication';
  28. $cookie_value = $data[0].'-'.$cookie_key;
  29. $cookie_expire = 60*60*24*30;
  30.  
  31. cookie::delete($cookie_name);
  32. cookie::set($cookie_name, $cookie_value, $cookie_expire);
  33.  
  34. $this->db->select('users.user_id, users.user_email, users.user_name, users.user_logins_count, users
    .user_last_login, users_roles.role_id'
    );
  35. $this->db->from('users');
  36. $this->db->join('users_roles', 'users_roles.user_id = users.user_id');
  37. $this->db->where(array('users.user_id' => $data[0], 'users.user_cookie_key' => $data[1]));
  38. $query = $this->db->get()->result_array();
  39.  
  40. foreach($query as $row)
  41. {
  42. $this->session->set('isLogin', TRUE);
  43. $this->session->set('id', $row->user_id);
  44. $this->session->set('login', $row->user_name);
  45. $this->session->set('email', $row->user_email);
  46. $this->session->set('lastvisit', $row->user_last_login);
  47. $this->session->set('role', $row->role_id);
  48. $this->db->from('users');
  49. $this->db->set(array('user_logins_count' => $row->user_logins_count+1, 'user_cookie_key' => $cookie_key, 'user_last_ip' => $this->input->ip_address(), 'user_last_login' => mktime()));
  50. $this->db->where(array('user_id' => $data[0], 'user_cookie_key' => $data[1]));
  51. $this->db->update();
  52. }
  53.  
  54. return (bool) TRUE;
  55. }
  56. else
  57. {
  58. return (bool) FALSE;
  59. }
  60. }
  61.  
  62. public function makeLogIn($login = NULL, $password = NULL, $auto = FALSE)
  63. {
  64. $cookie_key = $this->session->id();
  65. cookie::delete('Authentication');
  66. $check = $this->db->select('user_id, user_salt_begin, user_salt_end')->from('users')->where('users.user_name', $login)->get()->current();
  67.  
  68. if($auto === TRUE)
  69. {
  70. $cookie_name = 'Authentication';
  71. $cookie_value = $check->user_id.'-'.$cookie_key;
  72. $cookie_expire = 60*60*24*30;
  73. cookie::set($cookie_name, $cookie_value, $cookie_expire);
  74. }
  75.  
  76. if($login !== NULL and $password !== NULL and $this->session->get('isLogin', FALSE) === FALSE)
  77. {
  78. $this->db->select('users.user_id, users.user_email, users.user_name, users.user_logins_count, users
    .user_last_login, users_roles.role_id'
    );
  79. $this->db->from('users');
  80. $this->db->join('users_roles', 'users_roles.user_id = users.user_id');
  81. $this->db->where(array('users.user_name' => $login, 'users.user_password' => sha1($check->user_salt_begin.md5($password).$check->user_salt_end)));
  82. $query = $this->db->get()->result_array();
  83.  
  84. foreach($query as $row)
  85. {
  86. $this->session->set('isLogin', TRUE);
  87. $this->session->set('id', $row->user_id);
  88. $this->session->set('login', $row->user_name);
  89. $this->session->set('email', $row->user_email);
  90. $this->session->set('lastvisit', $row->user_last_login);
  91. $this->session->set('role', $row->role_id);
  92. $this->db->from('users');
  93. $this->db->set(array('user_logins_count' => $row->user_logins_count+1, 'user_cookie_key' => $cookie_key, 'user_last_ip' => $this->input->ip_address(), 'user_last_login' => mktime()));
  94. $this->db->where(array('users.user_name' => $login, 'users.user_password' => sha1($check->user_salt_begin.md5($password).$check->user_salt_end)));
  95. $this->db->update();
  96. }
  97.  
  98. return (bool) TRUE;
  99. }
  100. else
  101. {
  102. return (bool) FALSE;
  103. }
  104. }
  105.  
  106. public function makeLogOut()
  107. {
  108. $this->session->destroy();
  109. cookie::delete('Authentication');
  110. }
  111.  
  112. public function isAnonymous()
  113. {
  114. if($this->session->get('isLogin', FALSE) === FALSE)
  115. {
  116. return (bool) TRUE;
  117. }
  118. else
  119. {
  120. return (bool) FALSE;
  121. }
  122. }
  123.  
  124. public function getData()
  125. {
  126. $data = array(
  127. 'isLogin' => (bool) $this->session->get('isLogin', FALSE),
  128. 'id' => (int) $this->session->get('id', 0),
  129. 'login' => (string) $this->session->get('login', NULL),
  130. 'email' => (string) $this->session->get('email', NULL),
  131. 'lastvisit' => (string) date('d.m.Y, H:i:s', $this->session->get('lastvisit', mktime())),
  132. 'role' => (int) $this->session->get('role', 1),
  133. 'IP' => (string) $this->input->ip_address(),
  134. 'Browser' => (string) Kohana::user_agent()
  135. );
  136. return (array) $data;
  137. }
  138.  
  139. public function getId()
  140. {
  141. return (int) $this->session->get('id', 0);
  142. }
  143.  
  144. public function getEmail()
  145. {
  146. return (string) $this->session->get('email', NULL);
  147. }
  148.  
  149. public function getLogin()
  150. {
  151. return (string) $this->session->get('login', NULL);
  152. }
  153.  
  154. public function getLastvisit()
  155. {
  156. return (int) $this->session->get('lastvisit', mktime());
  157. }
  158.  
  159. public function getRole()
  160. {
  161. return (int) $this->session->get('role', 1);
  162. }
  163.  
  164. public function getIp()
  165. {
  166. return (string) $this->input->ip_address();
  167. }
  168.  
  169. public function getBrowser()
  170. {
  171. return (string) Kohana::user_agent();
  172. }
  173. } // End User Model
  174. ?>


Ten post edytował k3nsei 26.06.2008, 11:53:56
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Vengeance
post
Post #2





Grupa: Zarejestrowani
Postów: 657
Pomógł: 2
Dołączył: 15.08.2003
Skąd: Łódź

Ostrzeżenie: (0%)
-----


Pierwsze co rzuca mi się w oczy do bezsensowne komentarze w stylu
  1. <?php
  2. return (bool) TRUE; // Return result
  3. }
  4. }
  5. else
  6. {
  7. return (bool) FALSE; // Return result
  8. }
  9. }
  10. else
  11. {
  12. return (bool) FALSE; // Return result
  13. }
  14. }
  15. else
  16. {
  17. return (bool) FALSE; // Return result
  18. ?>


Poza tym, metoda AddUser czy makeActive bardziej pasuje mi ogólnie do modelu bądź kontrolera użytkownika niż autoryzacji
Kolejna rzecz, czy te metody getEmail, getUserName itd nie łatwiej byłoby zastąpić po prostu np. poprzez pobranie instancji obiektu aktualnie zalogowanego użytkownika, a potem wszystko już z tego obiektu. Czyli zamiast
$auth->getEmail()
to
$auth->getUser()->getEmail()

tu wydaje się bardziej rozbudowane... ale tak nie jest, bo w przypadku dodawania kolejnych rzeczy musisz do klasy Auth dopisywać kolejne metody.

Jak dla mnie Auth powinno odpowiadać za: logowanie, wylogowanie, zalogowanie automatyczne, pobranie obiektu zalogowanego użytkownika, sprawdzanie uprawnień. finito.

Pozdrawiam
Go to the top of the page
+Quote Post

Posty w temacie
- k3nsei   [kohana] Ocena Modelu Autoryzacji Użytkowników   25.06.2008, 12:43:35
- - Cysiaczek   Cytatvar $db; Teraz sobie poczytaj, dlaczego...   25.06.2008, 15:03:58
|- - k3nsei   Cytat(Cysiaczek @ 25.06.2008, 16:03:5...   25.06.2008, 15:07:52
- - radex_p   mógłbyś zamienić [ code ] na [ php ] ? Nieczytelni...   25.06.2008, 15:05:41
- - kwiateusz   reczne zapytania nie po to wymyslili tam active re...   25.06.2008, 15:10:21
- - k3nsei   A masz w active record inner join?   25.06.2008, 15:16:33
- - kwiateusz   ale update jest jak najmniej czystych zapyta...   25.06.2008, 15:23:53
- - Cysiaczek   @radex_p - nie baw się w moderatora - jest fu...   25.06.2008, 15:26:34
- - k3nsei   Właściwie to od logowania i wylogowania mam contro...   25.06.2008, 15:30:46
- - bełdzio   tylko ja w 50 lini widze SQLi ? Cytat(k3nsei ...   25.06.2008, 16:19:05
- - k3nsei   Wtedy się sypie cały kod. Chyba że ...   25.06.2008, 16:23:51
- - bełdzio   sypie? ja u siebie korzystam z join( ) i wsio dzia...   25.06.2008, 16:29:07
- - k3nsei   Zresztą quering też jest poprawny i o wiele szybci...   26.06.2008, 11:54:36
- - k3nsei   Oto mój nowy model autoryzacji użytkowników. ...   29.06.2008, 19:46:14
- - Vengeance   Pierwsze co rzuca mi się w oczy do bezsensowne kom...   29.06.2008, 22:42:38
- - Cysiaczek   @autor - nie zakładamy nowych topików dla każdej k...   30.06.2008, 01:24:20
- - k3nsei   Vengeance przecież jest getData(). Ale nie zawsze ...   30.06.2008, 08:07:50
- - Vengeance   Chodzi mi oto, że takie rzeczy nie koniecznie musz...   1.07.2008, 20:20:53
- - jarek_bolo   Witam, odświerzam trochę stary temat, bo chciałem ...   8.09.2008, 17:34:55


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 21:23