2012-01-21 13 views
0

私はCakePHP 1.3で非常に興味深い問題を抱えていました。正しい情報を使ってログインしようとしても動作しませんでした。私は今Cakephp 2.0に同じアプリをアップグレードしましたが、私は全く別の問題を抱えています。基本的には、ログインしているときにどの情報を入力しても、データベースが空であってもログインします。なぜこれが起こっているんアイデア...Strange CakePHP 2.0 auth login issue

は、ここに私のコードません:

ビュー:

<div id="login"> 
<p>Please log in! <a id="register" href="register" alt="Register">Register</a></p> 
<hr class="login"/> 
<?php  
    echo $this->Session->flash('auth');  
    echo $this->Form->create('User');  
    echo $this->Form->input('username'); 
    echo $this->Form->input('password'); 
    echo "<hr class=\"login\"/>"; 
    echo $this->Form->end('Login'); 
    echo $this->Session->flash('flash_registration'); 
    echo "<pre>"; print_r($this->request->data); echo "</pre>"; 
    echo $this->Html->link('Log-Out', 'logout'); 
?> 

</div> 

モデル:

<?php 
App::uses('AuthComponent', 'Controller/Component'); 
class User extends AppModel { 
var $name = 'User'; 
var $validate = array(
    'name' => array(
     'custom_rule' => array(
      'rule' => '/^[A-Za-z\s]*$/i', 
      'message' => 'Please enter an acceptable name' 
      ), 
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      'message' => 'This field is required' 
     ) 
    ), 
    'dob' => array(
     'rule' => array('date', 'ymd'), 
     'message' => 'Enter a valid date', 
    ), 
    'phone' => array(
     'numbers' => array(
      'rule' => 'numeric', 
      'message' => 'Numbers only, no dashes or spaces' 
     ), 
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      'message' => 'This field is required' 
     ) 
    ), 
    'username' => array(
     'alphaNumeric' => array(
      'rule' => 'alphaNumeric', 
      'message' => 'Letters and numbers only' 
     ), 
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      'message' => 'This field is required' 
     ) 
    ), 
    'e-mail' => array(
     'email' => array(
      'rule' => 'email', 
      'message' => 'Please enter a valid e-mail address' 
     ), 
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      'message' => 'This field is required' 
     ) 
    ), 
    'password_enter' => array(
     'length' => array(
      'rule' => array('between', 8, 16), 
      'message' => 'Password must be between 8 and 16 characters' 
     ), 
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      'message' => 'This field is required' 
     ) 
    ), 
    'password_confirm' => array( 
     'identicalFieldValues' => array( 
      'rule' => array('identicalFieldValues', 'password_enter'), 
      'message' => 'Passwords do not match' 
     ), 
     'length' => array(
      'rule' => array('between', 8, 16), 
      'message' => 'Password must be between 8 and 16 characters' 
     ), 
     'notEmpty' => array(
      'rule' => array('notEmpty'), 
      'message' => 'This field is required' 
     ) 
    ) 
); 

function identicalFieldValues($field=array(), $compare_field=null){ 
    foreach($field as $key => $value){ 
     $v1 = $value; 
     $v2 = $this->data[$this->name][ $compare_field ]; 
     if($v1 !== $v2) { 
      return FALSE; 
     } else { 
      return TRUE; 
     } 
    } 
} 
function beforeValidate(){ 

    $this->data['User']['dob'] = $this->data['User']['dob']; 

    return true; 
} 
function beforeSave(){ 

    $this->data['User']['password'] = AuthComponent::password($this->data['User']['password_enter']); 
    $this->data['User']['activated'] = FALSE; 

    return TRUE; 
} 

} 

?> 

コントローラー:

<?php 

class UsersController extends AppController { 

var $name = 'Users'; 
var $uses = array("User"); 
var $components = array(
     'Session', 
     'Auth' => array(
      'loginRedirect' => array('controller' => 'pages', 'action' => 'index'), 
      'logoutRedirect' => array('controller' => 'users', 'action' => 'login') 
     ) 
    ); 

var $helpers = array('Form', 'Session', 'Html'); 

function beforeFilter(){ 
    // Basic setup 
    $this->Auth->authenticate = array('Form'); 
    $this->Auth->allow('register', 'activate'); 

} 

function index() { 



} 

function login() { 
    $this->Auth->login($this->request->data); 
    $this->set('title_for_layout', "Welcome to Sound-On.com!"); 
    $this->layout = 'user_functions'; 
    if ($this->Auth->user()) { 
     echo "Logged in!"; 
    } else { 
     echo "Not logged in!"; 
    } 
} 

function logout() { 

    $this->redirect($this->Auth->logout()); 

} 

function register(){ 

    $this->set('title_for_layout', "Register Here!"); 
    $this->layout = 'user_functions'; 
    $date = date('Y'); 

    if (!empty($this->data)) { 
     $user_check = $this->User->find('first', array('conditions' => array('username' => $this->data['User']['username']))); 
     $email_check = $this->User->find('first', array('conditions' => array('e-mail' => $this->data['User']['e-mail']))); 
     if (empty($user_check)) { 
      if(empty($email_check)){ 
       if ($this->User->save($this->data)) { 
        $uuid_string = $this->data['User']['activation_hash']; 
        $email = <<<EOT 
        <html> 
         <head> 
          <title>Welcome to Sound-On.com!</title> 
         </head> 
         <body> 
          <p> 
           <h1>Welcome to Sound-on.com!</h1> 

           <p>You have successfully registered! To activate your account and start sounding on, please click <a href="http://www.sound-on.com/activate?uid=$uuid_string">Here</a>! <br/>If the link is not clickable, please copy and paste the link below into your browser address bar.</p> 

           http://www.sound-on.com/activate?uid=$uuid_string 

           <p style="">Thank you for registering!</p> 
               <p>Your friendly Sound-On registration robot</p> 

           <p>If you did not register or wish to remove your account, please click <a href="http://www.sound-on.com/not-me?uid=$uuid_string">here</a>.</p> 

           <p style="font-size:8pt;color:#707070">&copy; Copyright $date Sound-on.com. All rights Reserved.</p> 
          </p> 
         </body 
        </html> 
EOT; 
        $to = $this->data['User']['e-mail']; 
        $subject = 'Welcome to Sound-On.com!'; 
        $headers = "MIME-Version: 1.0" . "\r\n"; 
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n"; 
        $headers .= 'From: [email protected]'; 

        if (mail($to, $subject, $email, $headers)) { 
         $this->redirect('/'); 
        } 
       } else { 
        //$this->Session->setFlash('<p class="register_flash">Something went wrong. Please try again.</p>', 'flash_registration'); 
        //$this->flash('', '/'); 
       } 
      } else { 

       //email exists 

      } 
     } else { 

      //username exists 

     } 
    } 
} 

function activate(){ 

    $this->set('title_for_layout', "Register Here!"); 
    $this->layout = 'user_functions'; 
    if (!empty($_GET)) { 
     $activate = $this->User->updateAll(array('activated' => 1), array('activation_hash' => $_GET['uuid'])); 
     if ($activate) { 
      $this->set('message', '<p id="activation_message">Your account has been successfully activated! Please click <a href="/">here</a> to proceed to login!</p>'); 
     } 
    } 

} 
} 





?> 

おかげで前進!

答えて

0

データをAuth->login()関数に送信すると、データとともにログインします。

http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in

あなたはこのようなものを使用する必要があります。

public function login() { 
    if ($this->request->is('post')) { 
     if (!$this->Auth->login()) { 
      $this->Session->setFlash('Your username or password was incorrect.'); 
     } else { 
      $this->Session->setFlash('You are now logged in.'); 
      //redirect 
     } 
    } 
} 
+0

驚くばかり!ありがとう! :) – LukeWaggoner