Projeto

Geral

Perfil

Estatísticas
| Branch: | Revisão:

expressolivretools / app / Console / Commands / Expresso / GetUsersOULdap.php @ master

Histórico | Ver | Anotar | Baixar (2,39 KB)

1 68c328a2 Alexandre Correia
<?php
2
3
namespace App\Console\Commands\Expresso;
4
5
use App\Commons\Ldap;
6
use Illuminate\Console\Command;
7 ab2f5151 Alexandre Correia
use Illuminate\Support\Facades\File;
8 68c328a2 Alexandre Correia
9
class GetUsersOULdap extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16 ab2f5151 Alexandre Correia
    protected $signature = 'expresso:get-organization-users {--ou=} {cmd?}';
17 68c328a2 Alexandre Correia
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23 ab2f5151 Alexandre Correia
    protected $description = 'lista os usuários de uma organizacao ldap ( OU )';
24 68c328a2 Alexandre Correia
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $arguments = $this->arguments();
43
44
        $organization = $this->option('ou') ?? null;
45
46 ab2f5151 Alexandre Correia
        if( !is_null($organization) ){
47
48
            $users      = $this->getUsersOu($organization);
49
            $fileName   = $arguments['cmd'] ?? null;
50
51
            if( is_null($fileName) ) {
52
                foreach( $users as $user )
53
                {
54
                    $line  = "" ;
55
                    $line .= "cn        : " . ( $user['cn'][0] ?? "" ) . PHP_EOL;
56
                    $line .= "uid       : " . ( $user['uid'][0] ?? "" ) . PHP_EOL ;
57
                    $line .= "uidnumber : " . ( $user['uidnumber'][0] ?? "" ) . PHP_EOL ;
58
                    $line .= "----------------------------------------------------------------------------" . PHP_EOL;
59
                    
60
                    print_r( $line );
61 68c328a2 Alexandre Correia
                }
62 ab2f5151 Alexandre Correia
            }else {
63
                $this->writeFile( $users, $fileName );
64 68c328a2 Alexandre Correia
            }
65
66 ab2f5151 Alexandre Correia
        }
67
        
68 68c328a2 Alexandre Correia
    }
69
70
    private function getUsersOu( $organization )
71
    {
72
        $result = new Ldap();
73
        
74
        return $result->getUsersOu( $organization );
75
    }
76
77
    private function writeFile( array $users, string $fileName )
78
    {
79
        if( is_array($users) && count($users) > 0 ){
80
         
81
            $line  = "";
82
            foreach( $users as $user )
83
            {
84
                $line .= ( $user['cn'][0] ?? "" ) . ";";
85
                $line .= ( $user['uid'][0] ?? "" ) . ";" ;
86
                $line .= ( $user['uidnumber'][0] ?? "" ) . PHP_EOL ;
87
88 ab2f5151 Alexandre Correia
                File::put( $fileName , $line );
89 68c328a2 Alexandre Correia
            }
90
        }
91
92 ab2f5151 Alexandre Correia
        $this->info('Arquivo gerado com sucesso : ' . $fileName );
93 68c328a2 Alexandre Correia
    }
94
}