PDA

View Full Version : سوال: فوری: مشکل با کامپوننت Auth



masudafsar
جمعه 12 اردیبهشت 1393, 19:00 عصر
من یه کلاس usersController دارم ولی نمی دونم چه طوری روال های login و register و logout روو براش بنویسم.

تو سایت cakephp.org هرچی خوندم، چیزی دستگیرم نشد. ورژن کیک هم 2.4 هستش.

کد کنترلر :

<?php

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

/**
* CakePHP usersController
* @author Masud Afsar <Masud@Afsar.Info>
*/
class usersController extends AppController {

public $name = "users";
public $uses = array('User');

public function beforeRender() {
$this->set('controller_title', "کاربران");
}

public function index() {
$this->set('action_title', "لیست&nbsp;کاربران");

$options = array(
'conditions' => array(),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
$users = $this->User->find('all', $options);
$this->set('users', $users);
}

public function add() {
$this->set('action_title', "کاربر&nbsp;جدید");

if ($this->request->is("post")) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("Add new user successfully", 'admin/flash/success');
// $this->Session->setFlash("Add new user successfully");
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User not saved! please try again.", 'admin/flash/fail');
}
}
}

public function edit($id = NULL) {
$this->set('action_title', "ویرایش&nbsp;کاربر");

if ($this->isValidByID($id, $this->User)) {
if ($this->request->is('post') || $this->request->is('put')) {
if (!isset($this->request->data['User']['password']) || $this->request->data['User']['password'] == "") {
$data = $this->User->findById($this->request->data['User']['id']);
$this->request->data['User']['password'] = $data['User']['password'];
}
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("User updated", 'admin/flash/success');
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User updating fail!", 'admin/flash/fail');
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
$this->request->data['User']['password'] = NULL;
}
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}

public function delete($id = NULL) {
$this->set('action_title', "حذف&nbsp;کاربر");

if ($this->isValidByID($id, $this->User)) {
if ($this->User->delete($id)) {
$this->Session->setFlash("User deleted", 'admin/flash/warning');
$this->redirect(array('controller' => 'users', 'action' => 'index'));
} else {
$this->Session->setFlash("User deleting fail! please try again.", 'admin/flash/fail');
}
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}

public function profile($id = NULL) {
$this->set('action_title', "پروفایل&nbsp;کاربر");

if ($this->isValidByID($id, $this->User)) {
$options = array(
'conditions' => array(
'User.' . $this->User->primaryKey => $id
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
$this->request->data = $this->User->find('first', $options);
} else {
throw new NotFoundException(__("Invalid user ID"));
}
}

public function login() {
$this->layout = "login";

if ($this->request->is("post")) {
$options = array(
'conditions' => array(
'User.username' => $this->request->data['User']['username'],
'User.password' => $this->request->data['User']['password'],
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
if ($this->User->find('count', $options) == 1) {
$this->Session->setFlash("Loged in!", "admin/flash/success");
} else {
$this->Session->setFlash("User or pass is wrong!", "admin/flash/warning");
}
}
}

public function logout() {

}

public function register() {
$this->layout = "login";
if ($this->request->is("post")) {
$options = array(
'conditions' => array(
'or' => array(
'User.username' => $this->request->data['User']['username'],
'User.email' => $this->request->data['User']['email'],
)
),
'fields' => array(
'User.id',
'User.username',
'User.email',
'User.fullname'
)
);
if ($this->User->find('count', $options) > 0) {
$this->Session->setFlash("User Exist!", "admin/flash/warning");
} else {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash("Registering is complate. check your email.", "admin/flash/success");
} else {
$this->Session->setFlash("Please try again.", "admin/flash/warning");
}
}
}
}

public function forget() {
$this->layout = "login";
}

}


اگر نیاز به اطلاعات بیشتری دارید لطفا بگید تا بزارم. ممنون.

vahidqara
شنبه 13 اردیبهشت 1393, 09:32 صبح
سلام و درود به کیک پزان.... ببین عزیزم اگه کمی بیشتر مطالعه می کردی حتما موفق میشدی....
من اصلا کاری به کدهای تو ندارم و نمی خوام بگم کجاش درست هست کجاش غلط .. خودت تلاش میکنی بعدا ایراد کارتو در میاری.. :لبخندساده:
تابع logout رو اینجوری بنویس..


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


البته یک سری تنظمیات رو باید از حالت پیش فرض بیرون بیاری البته اگه دوس دارید مثلا در AppController باید logoutRedirect مقدار دهی که خودت دوس دارید انجام بدی..

و اما کد Login
توی کد Login یه سری کارها برای شناسایی کاربر باید انجام بشه که بستگی به طراحی جدول کاربر دارد که مثلا Role براش تعریف کردی یا نه.... به هر حال


public function login(){

$this->set('title_for_layout','ورود');
$this->layout = 'Home';
if($this->request->is('post')){
if($this->Auth->login()){

$this->User->id = $this->Auth->user('id');
$this->User->saveField('lastlogin', date('Y-m-d H:i:s'));
اینجا هم میتونی نقش کاربر رو چک کنی که کاربر عادی هست یا نه



}else{
$this->Session->setFlash(__('نام کاربری یا رمز عبور وجود ندارد','default',array(),'auth'));
$this->redirect($this->Auth->loginAction);
}


}

}



میتونی تابع بالا رو بدون تغییر استفاده کنی یا یک سری تغیرات رو اعمال کنی دوست عزیز....
من نمیدونم چه مشکلی با ثبت کاربر داری آیا؟ همون درج کاربر هستش که یک سری Validate ها رو باید هم سمت کاربر هم سمت سرور اعمال کنی و چک کنی این کاربر وجود دارد یا نه..!
همین :لبخندساده:

masudafsar
یک شنبه 14 اردیبهشت 1393, 12:23 عصر
خیلی ممنون وحید جان.
مشکلم رو کشف کردم.

مشکل اینجا بود که باید در مدل user قبل از هر ذخیره سازی ابتدا پسورد رو کد می کردم که این کار رو انجام نداده بودم تابع زیر رو به مدل اگر اضافه می کردم درست می شد.


public function beforeSave($options = array()) {
/* password hashing */
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}

vahidqara
دوشنبه 15 اردیبهشت 1393, 13:30 عصر
خواهش عزیزم .. آره باید قبل از Login پسورد Hash بشه .. موفق باشی :لبخندساده: