mcurry / cakephp_static_user

This URL has Read+Write access

unknown (author)
Fri Oct 24 12:01:38 -0700 2008
cakephp_static_user / README
100644 32 lines (27 sloc) 0.895 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
 * Static methods that can be used to retrieve the logged in user
 * from anywhere
 *
 * Copyright (c) 2008 Matt Curry
 * www.PseudoCoder.com
 * http://github.com/mcurry/cakephp/tree/master/snippets/static_user
 * http://www.pseudocoder.com/archives/2008/10/06/accessing-user-sessions-from-models-or-anywhere-in-cakephp-revealed/
 *
 * @author Matt Curry <matt@pseudocoder.com>
 * @license MIT
 *
 */
 
/* Description */
A snippet of code that can be dropped into your User model, which will allow you to access the
logged in user from anywhere in a CakePHP app.
 
/* Notes */
This code assumes you are using the core Auth component.
 
/* Instructions */
1) Paste the code into your User model.
 
2) In your AppController::beforeFilter add:
App::import('Model', 'User');
User::store($this->Auth->user());
 
/* Usage */
User::get('id');
User::get('username');
User::get('Model.fieldname');