expressolivretools / app / Console / Commands / Expresso / GetUsersOULdap.php @ 68c328a2
Histórico | Ver | Anotar | Baixar (2,91 KB)
1 |
<?php
|
---|---|
2 |
|
3 |
namespace App\Console\Commands\Expresso; |
4 |
|
5 |
use App\Commons\Ldap; |
6 |
use Illuminate\Console\Command; |
7 |
use Illuminate\Support\Facades\DB; |
8 |
use Storage; |
9 |
|
10 |
class GetUsersOULdap extends Command |
11 |
{ |
12 |
/**
|
13 |
* The name and signature of the console command.
|
14 |
*
|
15 |
* @var string
|
16 |
*/
|
17 |
protected $signature = 'expresso:get-users-organizations {--ou=} {cmd?}'; |
18 |
|
19 |
/**
|
20 |
* The console command description.
|
21 |
*
|
22 |
* @var string
|
23 |
*/
|
24 |
protected $description = 'Lista os usuarios de uma organização ( OU )'; |
25 |
|
26 |
/**
|
27 |
* Create a new command instance.
|
28 |
*
|
29 |
* @return void
|
30 |
*/
|
31 |
public function __construct() |
32 |
{ |
33 |
parent::__construct();
|
34 |
} |
35 |
|
36 |
/**
|
37 |
* Execute the console command.
|
38 |
*
|
39 |
* @return mixed
|
40 |
*/
|
41 |
public function handle() |
42 |
{ |
43 |
$arguments = $this->arguments(); |
44 |
|
45 |
$organization = $this->option('ou') ?? null; |
46 |
|
47 |
if( isset($arguments['cmd']) && $arguments['cmd'] == 'help' ) |
48 |
{ |
49 |
$this->help();
|
50 |
|
51 |
} else {
|
52 |
|
53 |
if( !is_null($organization) ){ |
54 |
|
55 |
$users = $this->getUsersOu($organization); |
56 |
$fileName = $arguments['cmd'] ?? null; |
57 |
|
58 |
if( is_null($fileName) ) { |
59 |
foreach( $users as $user ) |
60 |
{ |
61 |
$line = "" ; |
62 |
$line .= "cn : " . ( $user['cn'][0] ?? "" ) . PHP_EOL; |
63 |
$line .= "uid : " . ( $user['uid'][0] ?? "" ) . PHP_EOL ; |
64 |
$line .= "uidnumber : " . ( $user['uidnumber'][0] ?? "" ) . PHP_EOL ; |
65 |
$line .= "----------------------------------------------------------------------------" . PHP_EOL; |
66 |
|
67 |
print_r( $line ); |
68 |
} |
69 |
}else {
|
70 |
$this->writeFile( $users, $fileName ); |
71 |
} |
72 |
|
73 |
} |
74 |
} |
75 |
|
76 |
$this->newLine();
|
77 |
} |
78 |
|
79 |
private function getUsersOu( $organization ) |
80 |
{ |
81 |
$result = new Ldap(); |
82 |
|
83 |
return $result->getUsersOu( $organization ); |
84 |
} |
85 |
|
86 |
private function help() |
87 |
{ |
88 |
$this->newLine();
|
89 |
$this->info('EXEMPLO DE UTILIZACAO : '); |
90 |
$this->info('Ex: php artisan get-users-organizations --ou=<ORGANIZATION> { print | file }'); |
91 |
$this->newLine();
|
92 |
} |
93 |
|
94 |
private function writeFile( array $users, string $fileName ) |
95 |
{ |
96 |
$this->newLine();
|
97 |
|
98 |
if( is_array($users) && count($users) > 0 ){ |
99 |
|
100 |
$line = ""; |
101 |
foreach( $users as $user ) |
102 |
{ |
103 |
$line .= ( $user['cn'][0] ?? "" ) . ";"; |
104 |
$line .= ( $user['uid'][0] ?? "" ) . ";" ; |
105 |
$line .= ( $user['uidnumber'][0] ?? "" ) . PHP_EOL ; |
106 |
|
107 |
Storage::put('/EXPRESSO/'.$fileName, $line ); |
108 |
} |
109 |
} |
110 |
|
111 |
$this->info('Arquivo gerado com sucesso : /EXPRESSO/'. $fileName ); |
112 |
} |
113 |
} |