tags:

views:

1048

answers:

3

I have a User object that, upon successful authentication, is tucked into the session (sans security info) for easy recall and for determining whether we have an authenticated user or anonymous session. There are several paths by which the user can alter some or all of his or her information and I'd like to keep that session value up to date. The obvious answer is to update the value in the afterSave() callback, but that, of course, violates MVC.

Is there another way of capturing every change in one place so that I don't have to drop session writes all over the place? I can't think of anything, nor have I been able to find any other ideas. Am I the only person trying to do something like this?

Thanks.

Final Solution: I marked neilcrookes' response as the answer, frankly, because there doesn't seem to be the better way. Since this way violates my OCD senses, though, I took a slightly different path. I decided to have my User::authenticate() method return the authenticated user object to the caller so it can do whatever it wants with it. One of the things that the callers "want" to do is to drop that value in the session. It's redundancy, but it's very, very limited. In my mind, that felt better than accessing the session from the model (though it's certainly a damned if you do, damned if you don't scenario).

+5  A: 

Some might disagree but I'd screw MVC, do it in Model::afterSave() and use $_SESSION - test for the session before writing to it, in case it's not started for example you are saving against the model in a shell or something.

MVC is a general pattern - a guideline, you can bang your head against it trying to figure out how to achieve something that doesn't quite fit, or just do it another way and move onto to something more important.

Bring on the flames.

neilcrookes
Agreed. Screw the purists, long live the pragmatists.
inkedmn
Agreed with neilcrookes' answer
Travis Leleu
+1  A: 
//in users controller 
if ($this->User->save()) {
    $this->Auth->login($this->User->read());
    $this->Session->setFlash[.. etc]

And for the record, I do not agree with the answer of neilcrooks, but I will refrain from feeding the troll.

Alexander Morland
setFlash != Session Variable
ByteNirvana
you are quite write. but it's the Auth login part that is important.
Alexander Morland
@Alexander Morland, Read The Question - he has lots of paths (controller actions) where a user can update their details, and he wants to keep the data that loaded into the session on login, up-to-date with these changes. Your reply is not answering the question.
neilcrookes
sure is it. Auth::login includes a part where it writes the userdata to the session
Alexander Morland
True, it'll write to the session. But don't you need to feed it the user's password? Where are you going to get an *unhashed* password from?
Travis Leleu
you dont need it unhashed.
Alexander Morland
A: 

I had exactly the same problem, and I used the solution given by Alexander Morland, and it worked like a charm for me. Thanx

Pierre