Bin will be removed on June 1, 2014 in favor of http://gist.github.com. Please save all your pastes before then. So long, and thanks for all the fish!×
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/**
 * This is a VERY specific component for sharing the logged in user's data with all the parts of the mvc process.
 * It is important that this component not be used for anything but it's intended use. It will allow you send the
 * user array to the view, or set it to a property in the active controller, or set it as property of the active
 * controller's main model. It is also possible to set the data to controllers and models through methods (for
 * instance you may wish to use this feature when working with componnts and behaviors).
 *
 * By default, the component is set up to send the user data to the view and setting a property on both the controller
 * and the model by the name 'activeUser'. It is also set up to by default require a $this->UserData->setUserData($user);
 * call in your controller::beforeFilter(). This is called the "manual" approach. You can change to approach to
 * automatically retrieve the data from Auth by setting the config value of 'from' to 'auth'.
 *
 * When using the 'controller_method' and 'model_method' features, it is important to keep two things in mind. First of
 * all that these methods MUST be present, either in the model/controller or a behavior. It is also important to keep in
 * mind that if you set this option in the AppController, you must implement the method for all controllers/models.
 *
 ****
 *
 * Example 1 :
 *
 * I have a Boat model with a BoatController and an Index view. User login is handled by Auth component. The default
 * values is good for me, so I declare my compeonent like this :
 *
 * public $components = array('UserData'=>array('from'=>'auth'));
 *
 * In my model i can do this :
 *
 * function save($data, $validate = true) {
 *      $data['Boat']['captain'] = $this->activeUser['User]['name'];
 *      return parent::save($data,$validate);
 * }
 *
 * In my controller :
 *
 * function index() {
 *      $this->set('boats',$this->find('all',array('conditions'=>array('Boat.captain_id'=>$this->activeUser['User']['id'])));
 * }
 *
 * And in my view I can do this :
 *
 * [..] $html->link($activeUser['User]['name'], '/profile/'.$activeUser['User]['id']); [..]
 *
 ****
 *
 * Example 2 :
 *  
 * I have same situation as above, but if I wasnt using auth and wanted to call the variable 'client', I could set up my
 * component like this :
 *
 * public $components = array('UserData'=>array('view_variable'=>'client','controller_variable'=>'client','model_variable'=>'client'));
 *
 * Then I would use "$this->client" in model and $client in views, etc.
 *
 * I would have to put in the client data to the component manually though (as that is the default), and that needs to happen in the
 * beforeFilter action:
 *
 * $this->UserData->setUserData($client);
 *
 * @author Alexander 'alkemann' Morland
 * @version 0.1
 *
 */
class UserDataComponent extends Object
{
    private $controller = null;
    private $settings = array();
    private $activeUser = null;
   
    private $default = array(
        'from' => 'manual', // manual or auth atm; implement method name to extend
        'view_variable' => 'activeUser',
        'controller_variable' => 'activeUser',
        'controller_method' => false,
        'model_variable' => 'activeUser',
        'model_method' => false  // set to name of method
    );
   
    //called before Controller::beforeFilter()    
    function initialize(&$controller, $config = array()) {        
        $this->settings = am($this->default, $config);
        // saving the controller reference for later use        
        $this->controller =& $controller;    
    }    
   
    //called after Controller::beforeFilter()    
    function startup(&$controller) {    
        $userData = null;
        if (!method_exists($this,$this->settings['from'])) {
            /** @todo generate a user error */
            return false;
        }
       
        $userData = $this->{$this->settings['from']}();
       
        // send userdata to controller
        if (is_string($this->settings['controller_variable'])) {
            $this->controller->{$this->settings['controller_variable']} = $userData;
        }
       
        // send userdata to model
        if (
            is_string($this->settings['model_method']) &&
            is_object($this->controller->{$this->controller->modelClass})
        ) {
                $this->controller->{$this->controller->modelClass}->{$this->settings['model_method']}($userData);
        } elseif (is_string($this->settings['model_variable'])) {
            $this->controller->{$this->controller->modelClass}->{$this->settings['model_variable']} = $userData;
        }
       
        // send user data to view      
       
        if (is_string($this->settings['controller_method'])) {
            $this->controller->{$this->controller->modelClass}->{$this->settings['model_method']}($userData);
        } else if (is_string($this->settings['view_variable'])) {
            $this->controller->set($this->settings['view_variable'], $userData);
        }
    }    
   
    private function manual() {
        return $this->activeUser;
    }
    public function setUserData($data) {
        $this->activeUser = $data;
    }
   
    private function auth() {
        if (isset($this->controller->Auth)) {
            return $this->controller->Auth->user();
        } else {
            return null;
        }      
    }
 
}
?>