akubiczek /
applicake-backend
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Console\Commands; |
||
| 4 | |||
| 5 | use App\Models\Tenant; |
||
| 6 | use App\Models\User; |
||
| 7 | use App\Services\TenantManager; |
||
| 8 | use App\TenantUser; |
||
| 9 | use Illuminate\Console\Command; |
||
| 10 | use Illuminate\Support\Facades\DB; |
||
| 11 | use Symfony\Component\Console\Exception\RuntimeException; |
||
| 12 | |||
| 13 | class CreateTenantUser extends Command |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The name and signature of the console command. |
||
| 17 | * |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $signature = 'user:create {tenantId} {--email=} {--name=} {--password=}'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The console command description. |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $description = 'Creates new user for a tenant'; |
||
| 28 | |||
| 29 | protected $tenantManager; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Create a new command instance. |
||
| 33 | * |
||
| 34 | * @param TenantManager $tenantManager |
||
| 35 | */ |
||
| 36 | public function __construct(TenantManager $tenantManager) |
||
| 37 | { |
||
| 38 | parent::__construct(); |
||
| 39 | |||
| 40 | $this->tenantManager = $tenantManager; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Execute the console command. |
||
| 45 | * |
||
| 46 | * @throws \Throwable |
||
| 47 | * |
||
| 48 | * @return mixed |
||
| 49 | */ |
||
| 50 | public function handle() |
||
| 51 | { |
||
| 52 | $tenantId = $this->argument('tenantId'); |
||
| 53 | $tenant = Tenant::find($tenantId); |
||
| 54 | |||
| 55 | if (!$tenant) { |
||
| 56 | throw new RuntimeException('Tenant with ID = '.$tenantId.' does not exist.'); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 57 | } |
||
| 58 | |||
| 59 | $this->comment('Creating new user for tenant with subdomain \''.$tenant->subdomain.'\'.'); |
||
| 60 | |||
| 61 | $email = $this->option('email'); |
||
| 62 | if (!$email) { |
||
| 63 | $email = $this->ask('User\'s email'); |
||
| 64 | } |
||
| 65 | |||
| 66 | $num = TenantUser::where('username', $email)->count(); |
||
| 67 | if ($num) { |
||
| 68 | throw new RuntimeException('User with email \''.$email.'\' already exists.'); |
||
| 69 | } |
||
| 70 | |||
| 71 | $name = $this->option('name'); |
||
| 72 | if (!$name) { |
||
| 73 | $name = $this->ask('User\'s full name'); |
||
| 74 | } |
||
| 75 | |||
| 76 | $password = $this->option('password'); |
||
| 77 | if (!$password) { |
||
| 78 | $password = $this->secret('User\'s password'); |
||
| 79 | } |
||
| 80 | |||
| 81 | $tenantUser = new TenantUser(); |
||
| 82 | $tenantUser->username = $email; |
||
| 83 | $tenantUser->tenant_id = $tenant->id; |
||
|
0 ignored issues
–
show
|
|||
| 84 | |||
| 85 | $this->tenantManager->setTenant($tenant); |
||
| 86 | $user = new User(); |
||
| 87 | $user->email = $email; |
||
| 88 | $user->name = $name; |
||
| 89 | $user->password = bcrypt($password); |
||
| 90 | |||
| 91 | $user->assignRole('super-admin'); |
||
| 92 | |||
| 93 | DB::transaction(function () use ($tenantUser, $user) { |
||
| 94 | $tenantUser->save(); |
||
| 95 | $user->save(); |
||
| 96 | }); |
||
| 97 | |||
| 98 | $this->comment('Created user with username \''.$user->email.'\'.'); |
||
| 99 | } |
||
| 100 | } |
||
| 101 |