Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: System CMS - Galeria
Forum PHP.pl > Forum > Gotowe rozwiązania
martinii007
Potrzebuję zmodyfikować kod w taki sposób, aby było można logować się bez potrzeby wpisywania haseł.

System jest obszerny, nie mniej jednak wybrałem chyba właściwy plik. Pytanie: Co zmienić by dostęp był aktywny bez wpisywania loginu i hasła. Próbowałem prawie wszystkiego, ale nic nie działa....

  1.  
  2. <?php
  3.  
  4. require_once 'DefaultController.class.php';
  5.  
  6. class MK_AccountController extends MK_DefaultController{
  7.  
  8. public function _init(){
  9. parent::_init();
  10. $this->getView()->setTemplatePath('small');
  11.  
  12. $user = MK_Authorizer::authorize();
  13. if( $user->isAuthorized() && MK_Request::getParam('section') !== 'log-out' )
  14. {
  15. $this->getView()->redirect(array('controller' => 'index'));
  16. }
  17. }
  18.  
  19. public function sectionIndex()
  20. {
  21. $this->getView()->setRender( false );
  22. $user = MK_Authorizer::authorize();
  23. if( !$user->isAuthorized() )
  24. {
  25. $this->getView()->redirect(array('controller' => 'account', 'section' => 'login'));
  26. }
  27. else
  28. {
  29. $this->getView()->redirect(array('controller' => 'index'));
  30. }
  31.  
  32. }
  33.  
  34. public function sectionLogin()
  35. {
  36. $config = MK_Config::getInstance();
  37.  
  38. $this->getView()->getHead()->prependTitle( 'Login' );
  39.  
  40. $form_structure = array(
  41. 'email' => array(
  42. 'label' => 'Email',
  43. 'validation' => array(
  44. 'instance' => array()
  45. )
  46. ),
  47. 'password' => array(
  48. 'label' => 'Password',
  49. 'validation' => array(
  50. 'instance' => array()
  51. ),
  52. 'attributes' => array(
  53. 'type' => 'password'
  54. )
  55. ),
  56. 'remember-me' => array(
  57. 'type' => 'checkbox',
  58. 'label' => 'Remember me on this machine'
  59. ),
  60. 'login' => array(
  61. 'type' => 'submit',
  62. 'attributes' => array(
  63. 'value' => 'Login'
  64. )
  65. ),
  66. 'forgot-password' => array(
  67. 'type' => 'link',
  68. 'text' => 'Forgot Pass',
  69. 'attributes' => array(
  70. 'href' => $this->getView()->uri(array('controller' => 'account', 'section' => 'forgot-password'))
  71. )
  72. )
  73. );
  74.  
  75. $form_settings = array(
  76. 'attributes' => array(
  77. 'class' => 'standard clear-fix small'
  78. )
  79. );
  80.  
  81. $form = new MK_Form($form_structure, $form_settings);
  82.  
  83. if($form->isSuccessful())
  84. {
  85. $user = MK_Authorizer::authorizeByEmailPass(
  86. $form->getField('email')->getValue(),
  87. $form->getField('password')->getValue()
  88. );
  89.  
  90. if( $user->isAuthorized() )
  91. {
  92. if( $user->objectGroup()->isAdmin() )
  93. {
  94. $cookie = MK_Cookie::getInstance();
  95. $session = MK_Session::getInstance();
  96. $session->login = $user->getId();
  97. if( $form->getField('remember-me')->getValue() )
  98. {
  99. $cookie->set('login', $user->getId(), $config->site->user_timeout);
  100. }
  101. $this->getView()->redirect( array('controller' => 'index') );
  102. }
  103. else
  104. {
  105. $form->getField('password')->getValidator()->addError("You cannot access this section");
  106. }
  107. }
  108. else
  109. {
  110. $form->getField('password')->getValidator()->addError("Incorrect email / password combination");
  111. }
  112. }
  113. $html = $form->render();
  114.  
  115. $this->view->login_form = $html;
  116.  
  117. }
  118.  
  119. public function sectionForgotPassword()
  120. {
  121. $config = MK_Config::getInstance();
  122.  
  123. $this->getView()->getHead()->prependTitle( 'Forgot Password' );
  124.  
  125. $html = '';
  126. $form_structure = array(
  127. 'email' => array(
  128. 'label' => 'Email',
  129. 'validation' => array(
  130. 'instance' => array(),
  131. 'email' => array()
  132. )
  133. ),
  134. 'reset-password' => array(
  135. 'type' => 'submit',
  136. 'attributes' => array(
  137. 'value' => 'Reset Password'
  138. )
  139. )
  140. );
  141.  
  142. $form_settings = array(
  143. 'attributes' => array(
  144. 'class' => 'small clear-fix standard'
  145. )
  146. );
  147.  
  148. $form = new MK_Form($form_structure, $form_settings);
  149.  
  150. if($form->isSuccessful()){
  151. $search_criteria = array(
  152. array('field' => 'email', 'value' => $form->getField('email')->getValue())
  153. );
  154.  
  155. $users_module = MK_RecordModuleManager::getFromSlug('users');
  156. $user_account = $users_module->searchRecords( $search_criteria );
  157. $user_account = array_pop($user_account);
  158.  
  159. if( $user_account ){
  160. $new_password = MK_Utility::getRandomPassword();
  161. $user_account
  162. ->setTemporaryPassword($new_password)
  163. ->save();
  164.  
  165. $message = '<p>Hi, <strong>'.$user_account->getDisplayName().'</strong>!</p><p>Your new login details are below;</p><p><strong>Email:</strong> '.$user_account->getEmail().'<br /><strong>Password:</strong> '.$new_password.'</p>';
  166. $emailer = new MK_BrandedEmail();
  167. $emailer
  168. ->setSubject('Password Recovery')
  169. ->setMessage($message);
  170.  
  171. if( !$emailer->send( $user_account->getEmail(), $user_account->getUsername()) )
  172. {
  173. $form->getField('email')
  174. ->getValidator()
  175. ->addError("There was a problem sending your login credentials. Please consult <a href=\"mailto:".$config->site->email."\">".$config->site->email."</a> stating your problem.");
  176. }
  177. else
  178. {
  179. $html .= '<p>An email containing login credentials has been sent to <strong>'.$user_account->getEmail().'</strong>!</p>';
  180. $html .= '<p>Upon receiving your new password you can <a href="'.$this->getView()->uri(array('controller' => 'account', 'section' => 'login')).'">login</a> and change it to something memorable.</p>';
  181. }
  182. }else{
  183. $form->getField('email')
  184. ->getValidator()
  185. ->addError("Invalid email");
  186. }
  187. }else{
  188. $html .= '<p>Forgotten your password? No worries, enter your email address below and we\'ll send you a new password. If you haven\'t forgotten your password the <a href="'.$this->getView()->uri(array('controller' => 'account', 'section' => 'login')).'">login</a>.</p>';
  189. }
  190. $html .= $form->render();
  191.  
  192. $this->view->password_reset_form = $html;
  193.  
  194. }
  195.  
  196. public function sectionLogOut()
  197. {
  198. $session = MK_Session::getInstance();
  199. $cookie = MK_Cookie::getInstance();
  200. unset($session->login, $cookie->login);
  201. $this->getView()->redirect(array('controller' => 'account', 'section' => 'login'));
  202. }
  203.  
  204. }
  205.  
  206. ?>
  207.  
markuz
Pokaż klasę MK_Authorizer
martinii007
  1.  
  2. <?php
  3.  
  4. abstract class MK_Authorizer
  5. {
  6.  
  7. protected static $user;
  8.  
  9. public static function authorizeByEmailPassword( $email, $password )
  10. {
  11. return self::authorizeByEmailPass( $email, $password );
  12. }
  13.  
  14. public static function authorizeByEmailPass( $email, $password )
  15. {
  16. if( empty($password) || empty($password) )
  17. {
  18. throw new MK_Exception("Either username and password is blank");
  19. }
  20.  
  21. $config = MK_Config::getInstance();
  22.  
  23. $password = MK_Utility::getHash( $password );
  24.  
  25. $user_module = MK_RecordModuleManager::getFromType('user');
  26. $search = array(
  27. array('literal' => "`email` = ".MK_Database::getInstance()->quote($email)." AND ( `password` = ".MK_Database::getInstance()->quote($password)." OR `temporary_password` = ".MK_Database::getInstance()->quote($password)." )"),
  28. array('field' => 'type', 'value' => MK_RecordUser::TYPE_CORE)
  29. );
  30.  
  31. if( !empty($config->extensions->core->email_verification) && $config->extensions->core->email_verification )
  32. {
  33. $search[] = array('field' => 'email_verified', 'value' => '1');
  34. }
  35.  
  36. $results = $user_module->searchRecords( $search );
  37.  
  38. if( count($results) === 1 && ( $user = array_pop( $results ) ) )
  39. {
  40. self::authorizeById( $user->getId() );
  41. }
  42.  
  43. return self::authorize();
  44.  
  45. }
  46.  
  47. public static function authorizeByEmail( $email )
  48. {
  49. $user_module = MK_RecordModuleManager::getFromType('user');
  50.  
  51. $search = array(
  52. array('field' => 'email', 'value' => $email),
  53. );
  54.  
  55. $search_results = $user_module->searchRecords( $search );
  56. $user = array_pop( $search_results );
  57.  
  58. if( !empty($user) )
  59. {
  60. return self::authorizeById( $user->getId() );
  61. }
  62. else
  63. {
  64. throw new MK_Exception("User with Email $email doesn't exist");
  65. }
  66.  
  67. }
  68.  
  69.  
  70. public static function authorizeById( $id )
  71. {
  72. $config = MK_Config::getInstance();
  73.  
  74. $user_module = MK_RecordModuleManager::getFromType('user');
  75.  
  76. try
  77. {
  78. self::$user = MK_RecordManager::getFromId( $user_module->getId(), $id );
  79. self::$user
  80. ->setLastip( MK_Utility::getUserIp() )
  81. ->setLastLogin( date('Y-m-d H:i:s') )
  82. ->setTemporaryPassword('')
  83. ->save(false);
  84. }
  85. catch(Exception $e){}
  86.  
  87. return self::authorize();
  88.  
  89. }
  90.  
  91. public static function authorize()
  92. {
  93.  
  94. if( empty(self::$user) )
  95. {
  96. $user_module = MK_RecordModuleManager::getFromType('user');
  97. self::$user = MK_RecordManager::getNewRecord( $user_module->getId() );
  98. }
  99.  
  100. return self::$user;
  101.  
  102. }
  103.  
  104. }
  105.  
  106. ?>
markuz
Podmień zmienną $search (26 linia) na:

  1. $search = array(
  2. array('literal' => "`email` = ".MK_Database::getInstance()->quote($email)),
  3. array('field' => 'type', 'value' => MK_RecordUser::TYPE_CORE)
  4. );


PS.
To jest rozwiązanie dla "można logować się bez potrzeby wpisywania haseł."

Nie doczytałem, że chcesz:

"Co zmienić by dostęp był aktywny bez wpisywania loginu i hasła."

Jakiś użytkownik musi być przekazany dalej, w jakim celu chcesz to zrobić?
martinii007
Coś nie działa...

Chcę podpiąć tą galerię pod swojego CMSa za pomocą iframe'a i nie potrzebuję osobnego logowania.
Mogę zrobić w ten sposób, że login i hasło będą na sztywno wpisane w kodzie, ale też coś to nie funkcjonuje bo pobiera je zawsze z inputa...
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2024 Invision Power Services, Inc.