16 protected $name =
'license-import:csv';
32 parent::__construct();
42 $filename = $this->argument(
'filename');
45 if (!$this->option(
'testrun')==
'true') {
46 $this->comment(
'======= Importing Licenses from '.$filename.
' =========');
48 $this->comment(
'====== TEST ONLY License Import for '.$filename.
' ====');
49 $this->comment(
'============== NO DATA WILL BE WRITTEN ==============');
52 if (! ini_get(
"auto_detect_line_endings")) {
53 ini_set(
"auto_detect_line_endings",
'1');
56 $csv = Reader::createFromPath($this->argument(
'filename'));
57 $csv->setNewline(
"\r\n");
62 $nbInsert = $csv->each(
function ($row) use ($duplicates) {
67 if (array_key_exists(
'0',$row)) {
68 $user_name = trim($row[0]);
73 if (array_key_exists(
'1',$row)) {
74 $user_email = trim($row[1]);
79 if (array_key_exists(
'2',$row)) {
80 $user_username = trim($row[2]);
85 if (array_key_exists(
'3',$row)) {
86 $user_license_name = trim($row[3]);
88 $user_license_name =
'';
91 if (array_key_exists(
'4',$row)) {
92 $user_license_serial = trim($row[4]);
94 $user_license_serial =
'';
97 if (array_key_exists(
'5',$row)) {
98 $user_licensed_to_name = trim($row[5]);
100 $user_licensed_to_name =
'';
103 if (array_key_exists(
'6',$row)) {
104 $user_licensed_to_email = trim($row[6]);
106 $user_licensed_to_email =
'';
109 if (array_key_exists(
'7',$row)) {
110 $user_license_seats = trim($row[7]);
112 $user_license_seats =
'';
115 if (array_key_exists(
'8',$row)) {
116 $user_license_reassignable = trim($row[8]);
117 if ($user_license_reassignable!=
'') {
118 if ((strtolower($user_license_reassignable)==
'yes') || (strtolower($user_license_reassignable)==
'true') || ($user_license_reassignable==
'1')) {
119 $user_license_reassignable = 1;
122 $user_license_reassignable = 0;
125 $user_license_reassignable = 0;
128 if (array_key_exists(
'9',$row)) {
129 $user_license_supplier = trim($row[9]);
131 $user_license_supplier =
'';
134 if (array_key_exists(
'10',$row)) {
135 $user_license_maintained = trim($row[10]);
137 if ($user_license_maintained!=
'') {
138 if ((strtolower($user_license_maintained)==
'yes') || (strtolower($user_license_maintained)==
'true') || ($user_license_maintained==
'1')) {
139 $user_license_maintained = 1;
142 $user_license_maintained = 0;
147 $user_license_maintained =
'';
150 if (array_key_exists(
'11',$row)) {
151 $user_license_notes = trim($row[11]);
153 $user_license_notes =
'';
156 if (array_key_exists(
'12',$row)) {
158 $user_license_purchase_date = date(
"Y-m-d 00:00:01", strtotime($row[12]));
160 $user_license_purchase_date =
'';
163 $user_license_purchase_date = 0;
167 if (is_numeric($user_name)) {
168 $this->comment(
'User '.$user_name.
' is not a name - assume this user already exists');
172 } elseif ($user_name==
'') {
173 $this->comment(
'No user data provided - skipping user creation, just adding license');
180 $name = explode(
" ", $user_name);
181 $first_name =
$name[0];
182 $email_last_name =
'';
183 $email_prefix = $first_name;
185 if (!array_key_exists(1,
$name)) {
187 $email_last_name = $last_name;
188 $email_prefix = $first_name;
190 $last_name = str_replace($first_name,
'',$user_name);
192 if ($this->option(
'email_format')==
'filastname') {
193 $email_last_name.=str_replace(
' ',
'',$last_name);
194 $email_prefix = $first_name[0].$email_last_name;
196 } elseif ($this->option(
'email_format')==
'firstname.lastname') {
197 $email_last_name.=str_replace(
' ',
'',$last_name);
198 $email_prefix = $first_name.
'.'.$email_last_name;
200 } elseif ($this->option(
'email_format')==
'firstname') {
201 $email_last_name.=str_replace(
' ',
'',$last_name);
202 $email_prefix = $first_name;
209 $user_username = $email_prefix;
212 if ($user_email==
'') {
213 if ($first_name==
'Unknown') {
216 $email = strtolower($email_prefix).
'@'.$this->option(
'domain');
217 $user_email = str_replace(
"'",
'',$email);
221 $this->comment(
'Full Name: '.$user_name);
222 $this->comment(
'First Name: '.$first_name);
223 $this->comment(
'Last Name: '.$last_name);
224 $this->comment(
'Username: '.$user_username);
225 $this->comment(
'Email: '.$user_email);
226 $this->comment(
'License Name: '.$user_license_name);
227 $this->comment(
'Serial No: '.$user_license_serial);
228 $this->comment(
'Licensed To Name: '.$user_licensed_to_name);
229 $this->comment(
'Licensed To Email: '.$user_licensed_to_email);
230 $this->comment(
'Seats: '.$user_license_seats);
231 $this->comment(
'Reassignable: '.$user_license_reassignable);
232 $this->comment(
'Supplier: '.$user_license_supplier);
233 $this->comment(
'Maintained: '.$user_license_maintained);
234 $this->comment(
'Notes: '.$user_license_notes);
235 $this->comment(
'Purchase Date: '.$user_license_purchase_date);
237 $this->comment(
'------------- Action Summary ----------------');
239 if ($user_username!=
'') {
240 if ($user = User::where(
'username', $user_username)->whereNotNull(
'username')->first()) {
241 $this->comment(
'User '.$user_username.
' already exists');
244 $user = Sentry::createUser(array(
245 'first_name' => $first_name,
246 'last_name' => $last_name,
247 'email' => $user_email,
248 'username' => $user_username,
249 'password' => substr(str_shuffle(
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 10),
251 'permissions' => array(
255 'notes' =>
'User importerd through license importer' 259 $userGroup = Sentry::findGroupById(3);
262 $user->addGroup($userGroup);
263 $this->comment(
'User '.$first_name.
' created');
267 $user->user_id = NULL;
272 if ($supplier = Supplier::where(
'name', $user_license_supplier)->first()) {
273 $this->comment(
'Supplier '.$user_license_supplier.
' already exists');
275 $supplier =
new Supplier();
276 $supplier->name = e($user_license_supplier);
277 $supplier->user_id = 1;
279 if ($supplier->save()) {
280 $this->comment(
'Supplier '.$user_license_supplier.
' was created');
282 $this->comment(
'Something went wrong! Supplier '.$user_license_supplier.
' was NOT created');
290 $license->name = e($user_license_name);
291 if ($user_license_purchase_date!=
'') {
292 $license->purchase_date = $user_license_purchase_date;
294 $license->purchase_date = NULL;
296 $license->serial = e($user_license_serial);
297 $license->seats = e($user_license_seats);
298 $license->supplier_id = $supplier->id;
299 $license->user_id = 1;
300 if ($user_license_purchase_date!=
'') {
301 $license->purchase_date = $user_license_purchase_date;
303 $license->purchase_date = NULL;
305 $license->license_name = $user_licensed_to_name;
306 $license->license_email = $user_licensed_to_email;
307 $license->notes = e($user_license_notes);
309 if ($license->save()) {
310 $this->comment(
'License '.$user_license_name.
' with serial number '.$user_license_serial.
' was created');
313 $license_seat_created = 0;
315 for ($x = 0; $x < $user_license_seats; $x++) {
317 $license_seat =
new LicenseSeat();
318 $license_seat->license_id = $license->id;
322 $license_seat->assigned_to = $user->id;
324 $license_seat->assigned_to = NULL;
327 if ($license_seat->save()) {
328 $license_seat_created++;
332 if ($license_seat_created > 0) {
333 $this->comment($license_seat_created.
' seats were created');
335 $this->comment(
'Something went wrong! NO seats for '.$user_license_name.
' were created');
341 $this->comment(
'Something went wrong! License '.$user_license_name.
' was NOT created');
345 $this->comment(
'=====================================');
362 array(
'filename', InputArgument::REQUIRED,
'File for the CSV import.'),
375 array(
'domain', null, InputOption::VALUE_REQUIRED,
'Email domain for generated email addresses.', null),
376 array(
'email_format', null, InputOption::VALUE_REQUIRED,
'The format of the email addresses that should be generated. Options are firstname.lastname, firstname, filastname', null),
377 array(
'testrun', null, InputOption::VALUE_REQUIRED,
'Test the output without writing to the database or not.', null),
getArguments()
Get the console command arguments.
getOptions()
Get the console command options.
__construct()
Create a new command instance.
fire()
Execute the console command.