123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace Modules\CpManage\Http\Controllers;
- use Illuminate\Support\Collection;
- use Illuminate\Support\Facades\DB;
- use Modules\User\Models\User;
- trait UserTrait
- {
-
- protected $currentUser;
-
- protected function getCurrentUser(): User {
- if(!$this->currentUser) {
- $this->currentUser = $this->getLoginUser();
- }
- return $this->currentUser;
- }
-
- protected function listUserRoles():Collection {
- return $this->getCurrentUser()->roles->pluck('identify');
- }
-
- public function userIsCp():bool {
- return $this->listUserRoles()->contains('cp');
- }
-
- public function getUserCpName():string|null {
- if($this->userIsCp()) {
- return DB::table('user_belong_to_cp')
- ->where([
- 'is_enabled' => 1,
- 'user_id' => $this->getCurrentUser()->id,
- ])->value('cp_name');
- } else {
- return null;
- }
- }
- }
|