2016-08-05 6 views
0

)ユーザーを追加してデータベースに表示しますが、ユーザー名とパスワードをログインフォームに入力すると「無効なユーザー名パスワードを入力して、もう一度お試しください。私はそれを設定して、ページに投稿を編集したり追加したりするときやログインのリンクをクリックするときにログインの詳細を尋ねるようにしました。CakePHP - ユーザーは追加できますが、ユーザーはログインできません(

ユーザモデルはUser.phpある:

class User extends AppModel { 
public $validate = array(
    'username' => array(
     'required' => array(
      'rule' => 'notBlank', 
      'message' => 'A username is required' 
     ) 
    ), 
    'password' => array(
     'required' => array(
      'rule' => 'notBlank', 
      'message' => 'A password is required' 
     ) 
    ), 
    'role' => array(
     'valid' => array(
      'rule' => array('inList', array('admin', 'author')), 
      'message' => 'Please enter a valid role', 
      'allowEmpty' => false 
     ) 
    ) 
); 

public function beforeSave($options = array()) { 
    if (isset($this -> data[$this -> alias]['password'])) { 
     $passwordHasher = new BlowfishPasswordHasher(); 
     $this -> data[$this -> alias]['password'] = $passwordHasher -> hash(
      $this -> data[$this -> alias]['password'] 
     ); 
    } 
    return true; 
} 
} 

UsersController.php:

App::uses('AppController', 'Controller'); 

class UsersController extends AppController { 

public function beforeFilter() { 
    parent::beforeFilter(); 
    $this -> Auth -> allow('add', 'logout'); 
} 

public function login() { 
    if ($this -> request -> is('post')) { 
     if ($this -> Auth -> login()) { 
      return $this -> redirect($this -> Auth -> redirectUrl()); 
     } 
     $this -> Flash -> error(__('Invalid username or password, try again')); 
    } 
} 

public function logout() { 
    return $this -> redirect($this -> Auth -> logout()); 
} 

public function index() { 
    $this -> User -> recursive = 0; 
    $this -> set('users', $this -> paginate()); 
} 

public function view($id = null) { 
    $this -> User -> id = $id; 
    if (!$this -> User -> exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    $this -> set('user', $this -> User -> findById($id)); 
} 

public function add() { 
    if ($this -> request -> is('post')) { 
     $this -> User -> create(); 
     if ($this -> User -> save($this -> request -> data)) { 
      $this -> Flash -> success(__('The user has been saved')); 
      return $this -> redirect(array('action' => 'index')); 
     } 
     $this -> Flash -> error(
      __('The user could not be saved. Please, try again.') 
     ); 
    } 
} 

public function edit($id = null) { 
    $this -> User -> id = $id; 
    if (!$this -> User -> exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    if ($this -> request -> is('post') || $this -> request -> is('put')) { 
     if ($this -> User -> save($this -> request -> data)) { 
      $this -> Flash -> success(__('The user has been saved')); 
      return $this -> redirect(array('action' => 'index')); 
     } 
     $this -> Flash -> error(
      __('This user could not be saved. Please, try again.') 
     ); 
    } else { 
     $this -> request -> data = $this -> User - findById($id); 
     unset($this -> request -> data['User']['password']); 
    } 
} 

public function delete($id = null) { 

    $this -> request -> allowMethod('post'); 

    $this -> User -> id = $id; 
    if (!$this -> User -> exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    if ($this -> User -> delete()) { 
     $this -> Flash -> success(__('User deleted')); 
     return $this -> redirect(array('action' => 'index')); 
    } 
    $this -> Flash -> error(__('User was not deleted.')); 
    return $this -> redirect(array('action' => 'index')); 
} 

AppController.php:

class AppController extends Controller { 

public $components = array(
    'DebugKit.Toolbar', 
    'Flash', 
    'Auth' => array(
     'loginRedirect' => array(
      'controller' => 'posts', 
      'action' => 'index' 
     ), 
     'logoutRedirect' => array(
      'controller' => 'pages', 
      'action' => 'display', 
      'home' 
     ), 
     'authenticate' => array(
      'Form' => array(
       'passwordHasher' => 'Blowfish' 
      ) 
     ), 
     'authorize' => array('Controller') 
    ) 
); 

public function isAuthorized($user) { 
    if (isset($user['role']) && $user['role'] === 'admin') { 
     return true; 
    } 

    return false; 
} 

public function beforeFilter() { 
    $this -> Auth -> allow('index', 'view'); 
} 
} 

login.ctp:

<div class="users form"> 
    <?php echo $this -> Flash -> render('auth'); ?> 
    <?php echo $this -> Form -> create('user'); ?> 
     <fieldset> 
      <legend> 
       <?php echo __('Please enter you username and password'); ?> 
      </legend> 
      <?php 
       echo $this -> Form -> input('username'); 
       echo $this -> Form -> input ('password'); 
      ?> 
     </fieldset> 
    <?php echo $this -> Form -> end(__('Login')); ?> 
</div> 

パスワードはハッシュのように見えます。私はいくつかの同様の質問で見てきましたが、誰も私の問題を解決することはできません。バージョンはCakePHP 2.8.5です。 私のコーディングでエラーが発生することはありません。なぜそれはいつも私に "無効なユーザー名とパスワードを与えて、もう一度やり直してください"という理由がありますか?あなたは

'authorize' => array('Controller')'authorize' => array('User')
if (isset($user['role']) && $user['role'] === 'admin')でこれを置き換えるため

+2

と交換作業をしようとすると、十分な長ハッシュさを取るために、データベースのパスワードフィールドですパスワード?そうでない場合は、パスワードを切り捨てて正しく検証しません。 –

+1

Cakeがユーザーを取得するために使用するクエリを確認してみてください。 – drmonkeyninja

+0

@ mcgowan.bデータベースのパスワードにVARCHAR(255)が指定されているため、問題ではありません。 –

答えて

-1

変更このコードとはif (isset($user['role']) && $user['role'] === 'admin' || $user['role'] === 'author')

おかげ

+0

それは残念ながら働いていないようです。 –

+0

編集済みの回答を試してください...置き換えてください||条件付きの場合は&&で –

+0

いいえ、どちらも効果がありません。 –

関連する問題