forked from TheRestartProject/restarters.net
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserCreate.php
More file actions
167 lines (146 loc) · 5.42 KB
/
Copy pathUserCreate.php
File metadata and controls
167 lines (146 loc) · 5.42 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
<?php
namespace App\Console\Commands;
use App\Models\Role;
use App\Services\DiscourseService;
use App\Models\User;
use App\WikiSyncStatus;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
class UserCreate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:create {name} {email} {password} {language} {repair_network_id}
{--role=4 : Role ID (1=ROOT, 2=ADMINISTRATOR, 3=HOST, 4=RESTARTER, 5=GUEST, 6=NETWORK_COORDINATOR)}
{--consent-past-data= : Consent date for past data in YYYY-MM-DD format, defaults to today}
{--consent-future-data= : Consent date for future data in YYYY-MM-DD format, defaults to today}
{--consent-gdpr= : Consent date for GDPR in YYYY-MM-DD format, defaults to today}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a user with specified attributes.';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle(DiscourseService $discourseService): void
{
$name = $this->argument('name');
$email = $this->argument('email');
$password = $this->argument('password');
$language = $this->argument('language');
$repair_network_id = $this->argument('repair_network_id');
$role = $this->option('role');
// Get today's date for default consent values
$today = Carbon::today()->format('Y-m-d');
// Get consent dates from options or use today if not set
$consent_past_data = $this->option('consent-past-data') ?: $today;
$consent_future_data = $this->option('consent-future-data') ?: $today;
$consent_gdpr = $this->option('consent-gdpr') ?: $today;
// Validate consent dates
foreach ([
'consent_past_data' => $consent_past_data,
'consent_future_data' => $consent_future_data,
'consent_gdpr' => $consent_gdpr,
] as $label => $date) {
if (!$this->validateDate($date)) {
$this->error("Invalid date for $label: $date. Expected format: YYYY-MM-DD");
return;
}
}
if (User::where('email', $email)->count() > 0)
{
$this->info("User $email already exists - leaving unmodified");
return;
}
$rules = [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
];
$validator = Validator::make([
'name' => $name,
'email' => $email,
'password' => $password,
], $rules);
if ($validator->fails())
{
$this->error("Invalid parameters " . $validator->messages()->toJson());
} else
{
$userData = [
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
'recovery' => substr(bin2hex(openssl_random_pseudo_bytes(32)), 0, 24),
'recovery_expires' => strftime('%Y-%m-%d %X', time() + (24 * 60 * 60)),
'calendar_hash' => Str::random(15),
'username' => '',
'wiki_sync_status' => WikiSyncStatus::CreateAtLogin,
'language' => $language,
'repair_network' => $repair_network_id,
'consent_past_data' => $consent_past_data,
'consent_future_data' => $consent_future_data,
'consent_gdpr' => $consent_gdpr,
];
$user = User::create($userData);
if ($user)
{
$user->role = $role;
$user->save();
$this->info("User created {$user->name} (#{$user->id})");
$this->info("Role: {$this->getRoleName($user->role)}");
if (config('restarters.features.discourse_integration')) {
$user->generateAndSetUsername();
$discourseService->syncSso($user);
}
} else
{
$this->error("User creation failed");
}
$user->generateAndSetUsername();
}
}
/**
* Get the role name from the role ID.
*
* @param int $roleId
* @return string
*/
private function getRoleName($roleId)
{
$roles = [
Role::ROOT => 'ROOT',
Role::ADMINISTRATOR => 'ADMINISTRATOR',
Role::HOST => 'HOST',
Role::RESTARTER => 'RESTARTER',
Role::GUSET => 'GUEST',
Role::NETWORK_COORDINATOR => 'NETWORK_COORDINATOR',
];
return $roles[$roleId] ?? 'Unknown Role';
}
/**
* Validate a date string for proper format.
*/
private function validateDate($date)
{
$d = \DateTime::createFromFormat('Y-m-d', $date);
return $d && $d->format('Y-m-d') === $date;
}
}