Hi there, I'm learning as I go along here following various tutorials.
I'm using cakephp1.3 and I'm trying to sort out a auth and acl system.
I have a login function, a register function and a regcampaign
function (which allows registered users who can pass an acl check to
create a 'campaign')
When I register a new user I write his email (which I'm using instead
of a username as the unique id) to the session, create an aro and then
redirect the user to the regcampaign function which has an acl check.
This all works fine, the user passes the acl check and is allowed to
access the function.
However, when I login as an existing user I use the same code to write
the users email address to session and the user gets redirected to
regcampaign but this time access isn't allowed. I've also made an if
statement in the default.ctp layout which prints out the users email
if it was correctly written to session and it only appears when I
register a new user, but not when I login as an existing one so I know
that the problem is with the login function. I'm tearing my hair out
because the code is the same in each function as far as I can see.
here's my codes, if anyone can help I'd be their best friend.
function login() {
//see if the user is already logged in
if ($this->Session->read('Auth.User')) {
$this->Session->setFlash('You are logged in!');
$this->redirect('/', null, false);
}
$results =
$this->User->findByEmail($this->data['User']['email']);
$this->Session->write('user.email',
$this->data['User']['email']);
$firstlast = $results['User']['first_name'] . ' ' .
$results['User']
['last_name'];
$this->Session->write('user.name', $firstlast);
}
function register() {
if ($this->Session->read('Auth.User')) {
$this->redirect(array('controller' => 'campaigns',
'action' =>
'regcampaign'));
} else {
if(!empty($this->data)) {
if(isset($this->data['User']['password2'])) {
$this->data['User']['password2hashed']
= $this->Auth-
>password($this->data['User']['password2']);
}
if($this->User->save($this->data)) {
$this->Session->setFlash('Your
registration was successful');
$firstlast =
$this->data['User']['first_name'] . '_' . $this-
>data['User']['last_name'];
$this->Session->write('user.email',
$this->data['User']
['email']);
$this->Session->write('user.name',
$firstlast);
$parent =
$this->Acl->Aro->findByAlias('Users');
$aro = new Aro();
$aro->create();
$aro->save(array('alias' =>
$this->data['User']['email'], 'model'
=> 'User', 'foreign_key' => $this->User->id, 'parent_id' =>
$parent['Aro']['id']));
$this->Acl->Aro->save();
//get the current users data and log
him in
$data = $this->User->read();
$this->Auth->login($data);
$this->redirect(array('controller' =>
'pages', 'action' =>
'home'));
} else {
$this->data['User']['password'] = '';
//$this->data['User']['password2'] = '';
$this->Session->setFlash(__('The user
could not be saved. Please,
try again.', true));
}
}
}
function regcampaign() {
$alias = 'regcampaign';
if ($this->Acl->check($this->Session->read('user.email'),
$alias,
'create')) {
if (!empty($this->data)) {
$this->Campaign->create();
if ($this->Campaign->save($this->data)) {
$user =
$this->Session->read('user.email');
$name =
$this->Session->read('user.name');
$parent =
$this->Acl->Aco->findByAlias('Campaigns');
$alias = $name . '_' .
$this->Campaign->id;
$aco = new Aco();
$aco->create();
$aco->save(array('alias' => $alias,
'model' => 'Campaign',
'foreign_key' => $this->Campaign->id, 'parent_id' => $parent['Aco']
['id']));
$this->Acl->allow('users', $alias,
'read');
$this->Acl->allow($this->Session->read('user.email'), $alias);
//$this->Session->setFlash(__('The
campaign has been saved',
true));
$this->redirect(array('action' =>
'index'));
} else {
$this->Session->setFlash(__('The
campaign could not be saved.
Please, try again.', true));
}
}
} else {
$this->Session->setFlash(__('You don\'t have permission
to register
a new campaign'));
$this->redirect(array('action'=>'index'));
}
$users = $this->Campaign->User->find('list');
$charities = $this->Campaign->Charity->find('list');
$this->set(compact('users', 'charities'));
}
--
Our newest site for the community: CakePHP Video Tutorials
http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others
with their CakePHP related questions.
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php