UserAttribute.php 15.3 KB
Newer Older
Viral Solani's avatar
Viral Solani committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
<?php

namespace App\Models\Access\User\Traits\Attribute;

/**
 * Class UserAttribute.
 */
trait UserAttribute
{
    /**
     * @return mixed
     */
    public function canChangeEmail()
    {
        return config('access.users.change_email');
    }

    /**
     * @return bool
     */
    public function canChangePassword()
    {
23
        return !app('session')->has(config('access.socialite_session_name'));
Viral Solani's avatar
Viral Solani committed
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
    }

    /**
     * @return string
     */
    public function getStatusLabelAttribute()
    {
        if ($this->isActive()) {
            return "<label class='label label-success'>".trans('labels.general.active').'</label>';
        }

        return "<label class='label label-danger'>".trans('labels.general.inactive').'</label>';
    }

    /**
     * @return string
     */
    public function getConfirmedLabelAttribute()
    {
        if ($this->isConfirmed()) {
            return "<label class='label label-success'>".trans('labels.general.yes').'</label>';
        }

        return "<label class='label label-danger'>".trans('labels.general.no').'</label>';
    }

    /**
     * @return mixed
     */
    public function getPictureAttribute()
    {
        return $this->getPicture();
    }

    /**
     * @param bool $size
     *
     * @return mixed
     */
    public function getPicture($size = false)
    {
65
        if (!$size) {
Viral Solani's avatar
Viral Solani committed
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
            $size = config('gravatar.default.size');
        }

        return gravatar()->get($this->email, ['size' => $size]);
    }

    /**
     * @param $provider
     *
     * @return bool
     */
    public function hasProvider($provider)
    {
        foreach ($this->providers as $p) {
            if ($p->provider == $provider) {
                return true;
            }
        }

        return false;
    }

    /**
     * @return bool
     */
    public function isActive()
    {
        return $this->status == 1;
    }

    /**
     * @return bool
     */
    public function isConfirmed()
    {
        return $this->confirmed == 1;
    }

    /**
     * @return string
     */
    public function getShowButtonAttribute($class)
108 109
    {
        if (access()->allow('show-user')) {
Viral Solani's avatar
Viral Solani committed
110 111 112 113 114 115 116 117 118 119 120
            return '<a class="'.$class.'" href="'.route('admin.access.user.show', $this).'">
                    <i data-toggle="tooltip" data-placement="top" title="View" class="fa fa-eye"></i>
                </a>';
        }
    }

    /**
     * @return string
     */
    public function getEditButtonAttribute($class)
    {
121
        if (access()->allow('edit-user')) {
Viral Solani's avatar
Viral Solani committed
122 123 124 125 126 127 128 129 130 131 132
            return '<a class="'.$class.'" href="'.route('admin.access.user.edit', $this).'">
                    <i data-toggle="tooltip" data-placement="top" title="Edit" class="fa fa-pencil"></i>
                </a>';
        }
    }

    /**
     * @return string
     */
    public function getChangePasswordButtonAttribute($class)
    {
133
        if (access()->user()->id == $this->id && access()->allow('edit-user')) {
Viral Solani's avatar
Viral Solani committed
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
            return '<a class="'.$class.'" href="'.route('admin.access.user.change-password', $this).'">
                        <i class="fa fa-refresh" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.backend.access.users.change_password').'">
                        </i>
                    </a>';
        }
    }

    /**
     * @return string
     */
    public function getStatusButtonAttribute($class)
    {
        if ($this->id != access()->id()) {
            switch ($this->status) {
                case 0:
149
                    if (access()->allow('activate-user')) {
Viral Solani's avatar
Viral Solani committed
150
                        $name = $class == '' ? 'Active' : '';
151 152

                        return '<a class="'.$class.'" href="'.route('admin.access.user.mark', [$this, 1]).'"><i class="fa fa-check-square" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.backend.access.users.activate').'"></i>'.$name.'</a>';
Viral Solani's avatar
Viral Solani committed
153 154 155 156
                    }
                // No break

                case 1:
157
                    if (access()->allow('deactivate-user')) {
Viral Solani's avatar
Viral Solani committed
158
                        $name = ($class == '') ? 'Deactive' : '';
159 160

                        return '<a class="'.$class.'" href="'.route('admin.access.user.mark', [$this, 0]).'"><i class="fa fa-square" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.backend.access.users.deactivate').'"></i>'.$name.'</a>';
Viral Solani's avatar
Viral Solani committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
                    }
                // No break

                default:
                    return '';
                // No break
            }
        }

        return '';
    }

    /**
     * @return string
     */
    public function getConfirmedButtonAttribute($class)
    {
178
        if (!$this->isConfirmed() && access()->allow('edit-user')) {
Viral Solani's avatar
Viral Solani committed
179 180 181 182 183 184 185 186 187 188 189
            return '<a class="'.$class.'" href="'.route('admin.access.user.account.confirm.resend', $this).'"><i class="fa fa-refresh" data-toggle="tooltip" data-placement="top" title='.trans('buttons.backend.access.users.resend_email').'"></i></a> ';
        }

        return '';
    }

    /**
     * @return string
     */
    public function getDeleteButtonAttribute($class)
    {
190
        if ($this->id != access()->id() && access()->allow('delete-user')) {
Viral Solani's avatar
Viral Solani committed
191
            $name = $class == '' ? 'Delete' : '';
192

Viral Solani's avatar
Viral Solani committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206
            return '<a class="'.$class.'" href="'.route('admin.access.user.destroy', $this).'"
                 data-method="delete"
                 data-trans-button-cancel="'.trans('buttons.general.cancel').'"
                 data-trans-button-confirm="'.trans('buttons.general.crud.delete').'"
                 data-trans-title="'.trans('strings.backend.general.are_you_sure').'"><i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.general.crud.delete').'"></i>'.$name.'</a>';
        }

        return '';
    }

    /**
     * @return string
     */
    public function getRestoreButtonAttribute($class)
207 208
    {
        if (access()->allow('delete-user')) {
Viral Solani's avatar
Viral Solani committed
209 210 211 212 213 214 215
            return '<a class="'.$class.'" href="'.route('admin.access.user.restore', $this).'" name="restore_user"><i class="fa fa-refresh" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.backend.access.users.restore_user').'"></i></a> ';
        }
    }

    /**
     * @return string
     */
216
    public function getDeletePermanentlyButtonAttribute($class)
Viral Solani's avatar
Viral Solani committed
217
    {
218
        return '<a class="'.$class.'" href="'.route('admin.access.user.delete-permanently', $this).'" name="delete_user_perm"><i class="fa fa-trash" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.backend.access.users.delete_permanently').'"></i></a> ';
Viral Solani's avatar
Viral Solani committed
219 220 221 222 223 224 225 226 227 228 229
    }

    /**
     * @return string
     */
    public function getLoginAsButtonAttribute($class)
    {
        $name = $class == '' ? 'Login As' : '';
        /*
         * If the admin is currently NOT spoofing a user
         */
230
        if (access()->allow('login-as-user') && (!session()->has('admin_user_id') || !session()->has('temp_user_id'))) {
Viral Solani's avatar
Viral Solani committed
231
            //Won't break, but don't let them "Login As" themselves
232
            if ($this->id != access()->id()) {
Viral Solani's avatar
Viral Solani committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                return '<a class="'.$class.'" href="'.route('admin.access.user.login-as',
                    $this).'"><i class="fa fa-lock" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.backend.access.users.login_as',
                    ['user' => $this->name]).'"></i>'.$name.'</a>';
            }
        }

        return '';
    }

    /**
     * @return string
     */
    public function getClearSessionButtonAttribute($class)
    {
        $name = $class == '' ? 'Clear Session' : '';

249
        if ($this->id != access()->id() && config('session.driver') == 'database' && access()->allow('clear-user-session')) {
Viral Solani's avatar
Viral Solani committed
250 251 252 253 254 255 256 257 258 259 260 261
            return '<a class="'.$class.'" href="'.route('admin.access.user.clear-session', $this).'"
			 	 data-trans-button-cancel="'.trans('buttons.general.cancel').'"
                 data-trans-button-confirm="'.trans('buttons.general.continue').'"
                 data-trans-title="'.trans('strings.backend.general.are_you_sure').'"
                 name="confirm_item"><i class="fa fa-times" data-toggle="tooltip" data-placement="top" title="'.trans('buttons.backend.access.users.clear_session').'"></i>'.$name.'</a>';
        }

        return '';
    }

    public function checkAdmin()
    {
262
        if ($this->id != 1) {
Viral Solani's avatar
Viral Solani committed
263 264 265 266 267
            return '<div class="btn-group dropup">
                        <button type="button" class="btn btn-default btn-flat dropdown-toggle" data-toggle="dropdown">
                            <span class="glyphicon glyphicon-option-vertical"></span>
                        </button>
                        <ul class="dropdown-menu dropdown-menu-right">
268 269 270 271
                        <li>'.$this->getStatusButtonAttribute('').'</li>
                        <li>'.$this->getClearSessionButtonAttribute('').'</li>
                        <li>'.$this->getDeleteButtonAttribute('').'</li>
                        <li>'.$this->getLoginAsButtonAttribute('').'</li>
Viral Solani's avatar
Viral Solani committed
272
                        </ul>
273
                    </div>';
Viral Solani's avatar
Viral Solani committed
274 275 276 277
        }
    }

    /**
278 279
     * Get logged in user permission related to user management grid.
     *
Viral Solani's avatar
Viral Solani committed
280 281 282 283 284 285
     * @return array
     */
    public function getUserPermission()
    {
        $userPermission = [];
        $attributePermission = ['8', '10', '11', '12', '13', '14', '15'];
286 287
        foreach (access()->user()->permissions as $permission) {
            if (in_array($permission->id, $attributePermission)) {
Viral Solani's avatar
Viral Solani committed
288 289 290 291 292 293 294 295
                $userPermission[] = $permission->name;
            }
        }

        return $userPermission;
    }

    /**
296 297
     * Get action button attribute by permission name.
     *
Viral Solani's avatar
Viral Solani committed
298
     * @param string $permissionName
299 300
     * @param int    $counter
     *
Viral Solani's avatar
Viral Solani committed
301 302 303 304 305 306 307
     * @return string
     */
    public function getActionButtonsByPermissionName($permissionName, $counter)
    {
        // check if counter is less then 3 then apply button client
        $class = ($counter <= 3) ? 'btn btn-default btn-flat' : '';

308
        switch ($permissionName) {
Viral Solani's avatar
Viral Solani committed
309 310
            case 'show-user':
                $button = ($counter <= 3) ? $this->getShowButtonAttribute($class) : '<li>'
311
                    .$this->getShowButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
312 313 314
                    '</li>';
                break;
            case 'edit-user':
315 316
                $button = ($counter <= 3) ? $this->getEditButtonAttribute($class) : '<li>'
                    .$this->getEditButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
317
                    '</li>';
318 319
                $button .= ($counter <= 3) ? $this->getChangePasswordButtonAttribute($class) : '<li>'
                    .$this->getChangePasswordButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
320 321 322
                    '</li>';
                break;
            case 'activate-user':
323 324 325
                if (\Route::currentRouteName() == 'admin.access.user.deactivated.get') {
                    $button = ($counter <= 3) ? $this->getStatusButtonAttribute($class) : '<li>'
                    .$this->getStatusButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
326
                    '</li>';
327
                } else {
Viral Solani's avatar
Viral Solani committed
328 329 330 331
                    $button = '';
                }
                break;
            case 'deactivate-user':
332 333 334
                if (\Route::currentRouteName() == 'admin.access.user.get') {
                    $button = ($counter <= 3) ? $this->getStatusButtonAttribute($class) : '<li>'
                    .$this->getStatusButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
335
                    '</li>';
336
                } else {
Viral Solani's avatar
Viral Solani committed
337 338 339 340
                    $button = '';
                }
                break;
            case 'delete-user':
341 342 343
                if (access()->user()->id != $this->id) {
                    $button = ($counter <= 3) ? $this->getDeleteButtonAttribute($class) : '<li>'
                        .$this->getDeleteButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
344
                        '</li>';
345
                } else {
Viral Solani's avatar
Viral Solani committed
346 347 348 349
                    $button = '';
                }
                break;
            case 'login-as-user':
350 351 352
                if (access()->user()->id != $this->id) {
                    $button = ($counter <= 3) ? $this->getLoginAsButtonAttribute($class) : '<li>'
                        .$this->getLoginAsButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
353
                        '</li>';
354
                } else {
Viral Solani's avatar
Viral Solani committed
355 356 357 358
                    $button = '';
                }
                break;
            case 'clear-user-session':
359 360 361
                if (access()->user()->id != $this->id) {
                    $button = ($counter <= 3) ? $this->getClearSessionButtonAttribute($class) : '<li>'
                        .$this->getClearSessionButtonAttribute($class).
Viral Solani's avatar
Viral Solani committed
362
                        '</li>';
363
                } else {
Viral Solani's avatar
Viral Solani committed
364 365 366 367 368 369 370 371 372 373 374
                    $button = '';
                }
                break;
            default:
                $button = '';
                break;
        }

        return $button;
    }

375 376
    public function getNameAttribute()
    {
377
        return $this->first_name.' '.$this->last_name;
378 379
    }

Viral Solani's avatar
Viral Solani committed
380 381 382 383 384 385
    /**
     * @return string
     */
    public function getActionButtonsAttribute()
    {
        if ($this->trashed()) {
386 387 388 389
            return '<div class="btn-group action-btn">
                        '.$this->getRestoreButtonAttribute('btn btn-default btn-flat').'
                        '.$this->getDeletePermanentlyButtonAttribute('btn btn-default btn-flat').'
                    </div>';
Viral Solani's avatar
Viral Solani committed
390
        }
391

Viral Solani's avatar
Viral Solani committed
392
        // Check if role have all permission
393
        if (access()->user()->roles[0]->all) {
Viral Solani's avatar
Viral Solani committed
394
            return '<div class="btn-group action-btn">
395 396 397 398 399 400
                    '.$this->getShowButtonAttribute('btn btn-default btn-flat').'
                    '.$this->getEditButtonAttribute('btn btn-default btn-flat').'
                    '.$this->getChangePasswordButtonAttribute('btn btn-default btn-flat').'
                    '.$this->checkAdmin().'
                </div>';
        } else {
Viral Solani's avatar
Viral Solani committed
401 402 403 404 405
            $userPermission = $this->getUserPermission();
            $permissionCounter = count($userPermission);
            $actionButton = '<div class="btn-group action-btn">';
            $i = 1;

406 407 408
            if (access()->user()->id == $this->id) {
                if (in_array('clear-user-session', $userPermission)) {
                    $permissionCounter = $permissionCounter - 1;
Viral Solani's avatar
Viral Solani committed
409 410
                }

411 412
                if (in_array('login-as-user', $userPermission)) {
                    $permissionCounter = $permissionCounter - 1;
Viral Solani's avatar
Viral Solani committed
413 414
                }

415 416
                if (in_array('delete-user', $userPermission)) {
                    $permissionCounter = $permissionCounter - 1;
Viral Solani's avatar
Viral Solani committed
417 418
                }

419 420
                if (in_array('deactivate-user', $userPermission)) {
                    $permissionCounter = $permissionCounter - 1;
Viral Solani's avatar
Viral Solani committed
421 422
                }
            }
423 424 425 426

            foreach ($userPermission as $value) {
                if ($i != 3) {
                    $actionButton = $actionButton.''.$this->getActionButtonsByPermissionName($value, $i);
Viral Solani's avatar
Viral Solani committed
427
                }
428 429 430 431 432 433

                if ($i == 3) {
                    $actionButton = $actionButton.''.$this->getActionButtonsByPermissionName($value, $i);

                    if ($permissionCounter > 3) {
                        $actionButton = $actionButton.'
Viral Solani's avatar
Viral Solani committed
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
                            <div class="btn-group dropup">
                            <button type="button" class="btn btn-default btn-flat dropdown-toggle" data-toggle="dropdown">
                                <span class="glyphicon glyphicon-option-vertical"></span>
                            </button>
                            <ul class="dropdown-menu dropdown-menu-right">';
                    }
                }
                $i++;
            }
            $actionButton .= '</ul></div></div>';

            return $actionButton;
        }
    }
}