Projeto

Geral

Perfil

Estatísticas
| Branch: | Revisão:

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

Histórico | Ver | Anotar | Baixar (2,39 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\File;
8

    
9
class GetUsersOULdap extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'expresso:get-organization-users {--ou=} {cmd?}';
17

    
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'lista os usuários de uma organizacao ldap ( OU )';
24

    
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
        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
                }
62
            }else {
63
                $this->writeFile( $users, $fileName );
64
            }
65

    
66
        }
67
        
68
    }
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
                File::put( $fileName , $line );
89
            }
90
        }
91

    
92
        $this->info('Arquivo gerado com sucesso : ' . $fileName );
93
    }
94
}