doAuthSecurityUser.class.php 8.56 KB
Newer Older
Игорь's avatar
init    
Игорь committed
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php

/*
 * This file is part of the symfony package.
 * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

class doAuthSecurityUser extends sfBasicSecurityUser
{
    protected $user = null;
    protected $user_info = null;

    /**
     * Initializes the doAuthSecurityUser object.
     *
     * @param sfEventDispatcher $dispatcher The event dispatcher object
     * @param sfStorage $storage The session storage object
     * @param array $options An array of options
     */
    public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
    {
        parent::initialize($dispatcher, $storage, $options);
        if (!$this->isAuthenticated()) {
            $this->getAttributeHolder()->removeNamespace('doUser');
            $this->user = null;
        } elseif(sfConfig::get('sf_app') == 'arm') {
            $old_credentials = $this->getCredentials();

            $new_credentials = array();

            $account = $this->getAccount();

            if ($account) {
                $perms = $account->getUserPermissions();

                foreach ($perms as $perm) {
                    $ex_module = explode('-', $perm->getCredential());
                    foreach (explode('|', $ex_module[1]) as $action) {
                        $credential = $ex_module[0] . '_' . $action;
                        $this->addCredential($credential);
                        $new_credentials[] = $credential;
                    }
                }

                $user_groupss = Doctrine_Query::create()
                    ->select("ugs.*")
                    ->from("UserGroupUsers ugs")
                    ->where("ugs.user_id = " . $this->getUserId())
                    ->execute();

                foreach ($user_groupss as $user_groups) {
                    $user_group = Doctrine::getTable("UserGroup")->find($user_groups->getUserGroupId());
                    if ($user_group) {
                        foreach ($user_group->getUserGroupPermissions() as $user_group_permission) {
                            $ex_module = explode('-', $user_group_permission->getCredential());
                            foreach (explode('|', $ex_module[1]) as $action) {
                                $credential = $ex_module[0] . '_' . $action;
                                $this->addCredential($credential);
                                $new_credentials[] = $credential;
                            }
                        }
                    }
                }

                $diff = array_diff($old_credentials, $new_credentials);
                foreach ($diff as $credential) {
                    $this->removeCredential($credential);
                }

            }
        }
    }


    /**
     * Returns the referer uri.
     *
     * @param string $default The default uri to return
     * @return string $referer The referer
     */
    public function getReferer($default)
    {
        $referer = $this->getAttribute('referer', $default);
        $this->getAttributeHolder()->remove('referer');

        return $referer;
    }

    /**
     * Sets the referer.
     *
     * @param string $referer
     */
    public function setReferer($referer)
    {
        if (!$this->hasAttribute('referer')) {
            $this->setAttribute('referer', $referer);
        }
    }

    /**
     * Returns whether or not the user has the given credential.
     *
     * @param string $credential The credential name
     * @param boolean $useAnd Whether or not to use an AND condition
     * @return boolean
     */
    public function hasCredential($credential, $useAnd = true)
    {
        if ($this->getAccount()->getUsername() == 'root' || $this->getAccount()->getUsername() == 'Ильжан') {
            return true;
        }

        if (empty($credential)) {
            return false;
        }

        if (!$this->getAccount()) {
            return false;
        }

        return parent::hasCredential($credential, $useAnd);
    }

    /**
     * Returns whether or not the user is a super admin.
     *
     * @return boolean
     */
    public function isAdmin()
    {
        return $this->getAccount() ? $this->getAccount()->getIsSuperAdmin() : false;
    }

    public function isAuthenticated()
    {
        return $this->isAuthenticate() && $this->getAccount() && $this->getAccount()->getIsActive() ? (sfConfig::get('sf_app') == 'arm' ? $this->isAdmin() : true) : false;
    }

    /**
     * Returns whether or not the user is anonymous.
     *
     * @return boolean
     */
    public function isAnonymous()
    {
        return !$this->isAuthenticated();
    }

    /**
     * Signs in the user on the application.
     *
     * @param doAuthUser $user The doAuthUser id
     * @param boolean $remember Whether or not to remember the user
     * @param Doctrine_Connection $con A Doctrine_Connection object
     */
    public function signIn($user, $remember = false, $con = null)
    {

        // we remove a non-user storage
        $this->getAttributeHolder()->removeNamespace('doPreUser');
        // signin

        $this->setAttribute('user_id', $user->getId(), 'doUser');
        $this->setAuthenticated(true);
        $this->clearCredentials();

        // save last login
        $user->setLastLogin(date('Y-m-d H:i:s'));
        $user->save($con);

        $storage_options = sfContext::getInstance()->getStorage()->getOptions();
        if(isset($storage_options['db_table']) && $session = $this->getSession()){
            $session->setUserId($this->getUserId());
            $session->save();
        }

        // remember?
        if ($remember) {
            // save to cookie
            $hash = base64_encode(serialize(array($user->getUsername(), md5(rand()), doAuthTools::rememberHash($user))));

            $context = sfContext::getInstance();

            $expiration_age = sfConfig::get('app_doAuth_remember_key_expiration_age', 356 * 24 * 3600);
            // make key as a cookie
            $remember_cookie = sfConfig::get('app_doAuth_remember_cookie_name', 'doRemember');
            sfContext::getInstance()->getResponse()->setCookie($remember_cookie, $hash, time() + $expiration_age);
        }

        $this->dispatcher->notify(new sfEvent($this, 'user.signed_in'));
    }

    /**
     * Signs out the user.
     *
     */
    public function signOut()
    {
        $this->getAttributeHolder()->removeNamespace('doUser');
        $this->user = null;
        $this->clearCredentials();
        $this->setAuthenticated(false);
        $expiration_age = sfConfig::get('app_doAuth_remember_key_expiration_age', 356 * 24 * 3600);
        $remember_cookie = sfConfig::get('app_doAuth_remember_cookie_name', 'doRemember');
        sfContext::getInstance()->getResponse()->setCookie($remember_cookie, '', time() - $expiration_age);
        $this->dispatcher->notify(new sfEvent($this, 'user.signed_out'));
    }

    /**
     * Returns the related doAuthUser.
     *
     * @return User
     */
    public function getAccount()
    {


        if (!$this->user && $id = $this->getAttribute('user_id', null, 'doUser')) {

            $this->user = Doctrine::getTable('User')->find($id);


            if (!$this->user) {
                // the user does not exist anymore in the database
                $this->signOut();
                return false;
                //throw new sfException('The user does not exist anymore in the database.');
            }
        }

        return $this->user;
    }

    public function getUserInfo()
    {
        if (!$this->user_info) {
            $this->user_info = false;
        }

        return $this->user_info;
    }

    /**
     * Returns the unique identifier for user. Typically it is id field in User model
     *
     * @return <type>
     *
     */

    public function getUserId()
    {
        return $this->getAttribute('user_id', '', 'doUser');

    }

    /**
     * Returns the string representation of the object.
     *
     * @return string
     */
    public function __toString()
    {
        return $this->getAccount()->__toString();
    }

    /**
     * Returns the doAuthUser object's username.
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->getAccount()->getUsername();
    }

    public function getSession()
    {
        $storage_options = sfContext::getInstance()->getStorage()->getOptions();
        if(isset($storage_options['db_table'])){
            return Doctrine::getTable($storage_options['db_table'])->find(session_id());
        }
        return false;
    }
}