forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrackableBehavior.php
More file actions
178 lines (161 loc) · 4.56 KB
/
TrackableBehavior.php
File metadata and controls
178 lines (161 loc) · 4.56 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
/**
* Trackable Behavior
*/
App::uses('ModelBehavior', 'Model');
App::uses('AuthComponent', 'Controller/Component');
App::uses('CakeSession', 'Model/Datasource');
/**
* Trackable Behavior
*
* Populate `created_by` and `updated_by` fields from session data.
*
* It customize for NetCommons
* mainly customization is for phpmd
*
* @package Croogo.Croogo.Model.Behavior
* @since 1.6
* @author Rachman Chavik <rchavik@gmail.com>
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @link http://www.croogo.org
*/
class TrackableBehavior extends ModelBehavior {
/**
* Default settings
*
* Change values for NetCommons
* created_by to created_user,updated_by to modified_user
*
* @author Kohei Teraguchi <kteraguchi@commonsnet.org>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
*/
protected $_defaults = array(
'userModel' => 'Users.User',
'fields' => array(
'created_by' => 'created_user',
'updated_by' => 'modified_user',
),
);
/**
* Setup
*
* @param Model $model instance of model
* @param array $config array of configuration settings.
* @return void
*/
public function setup(Model $model, $config = array()) {
$this->settings[$model->alias] = Set::merge($this->_defaults, $config);
if ($this->_hasTrackableFields($model)) {
$this->_setupBelongsTo($model);
}
}
/**
* Checks wether model has the required fields
*
* @param Model $model instance of model
* @return bool True if $model has the required fields
*/
protected function _hasTrackableFields(Model $model) {
$fields = $this->settings[$model->alias]['fields'];
return
$model->hasField($fields['created_by']) &&
$model->hasField($fields['updated_by']);
}
/**
* Bind relationship on the fly
*
* @param Model $model instance of model
* @return void
*/
protected function _setupBelongsTo(Model $model) {
if (!empty($model->belongsTo['TrackableCreator'])) {
return;
}
$config = $this->settings[$model->alias];
list($plugin, $modelName) = pluginSplit($config['userModel']);
$className = isset($plugin) ? $plugin . '.' . $modelName : $modelName;
$model->bindModel(array(
'belongsTo' => array(
'TrackableCreator' => array(
'className' => $className,
'foreignKey' => $config['fields']['created_by'],
'fields' => array(
'TrackableCreator.id',
'TrackableCreator.username',
'TrackableCreator.handlename'
)
),
'TrackableUpdater' => array(
'className' => $className,
'foreignKey' => $config['fields']['updated_by'],
'fields' => array(
'TrackableUpdater.id',
'TrackableUpdater.username',
'TrackableUpdater.handlename'
)
),
)
), false);
}
/**
* Fill the created_by and updated_by fields
*
* Note: Since shells do not have Sessions, created_by/updated_by fields
* will not be populated. If a shell needs to populate these fields, you
* can simulate a logged in user by setting `Trackable.Auth` config:
*
* Configure::write('Trackable.User', array('id' => 1));
*
* Note that value stored in this variable overrides session data.
*
* @param Model $model instance of model
* @param array $options Options passed from Model::save().
* @return bool True if the operation should continue, false if it should abort
*/
public function beforeSave(Model $model, $options = array()) {
if (!$this->_hasTrackableFields($model)) {
return true;
}
$config = $this->settings[$model->alias];
// for PHPMD
$userId = $this->__getUserId($model);
if (!isset($userId)) {
return true;
}
$alias = $model->alias;
$createdByField = $config['fields']['created_by'];
$updatedByField = $config['fields']['updated_by'];
if (empty($model->data[$alias][$createdByField])) {
if (!$model->exists()) {
$model->data[$alias][$createdByField] = $userId;
}
}
$model->data[$alias][$updatedByField] = $userId;
if (!empty($model->whitelist)) {
$model->whitelist[] = $createdByField;
$model->whitelist[] = $updatedByField;
}
return true;
}
/**
* Get userId
*
* @param Model $model instance of model
* @return string login userId
*/
private function __getUserId(Model $model) {
$config = $this->settings[$model->alias];
$User = ClassRegistry::init($config['userModel']);
$userPk = $User->primaryKey;
$user = Configure::read('Trackable.Auth.User');
if (!$user && CakeSession::started()) {
$user = AuthComponent::user();
}
$userId = null;
if ($user && array_key_exists($userPk, $user)) {
$userId = $user[$userPk];
}
return $userId;
}
}