Пример #1
0
 /** @inheritdoc */
 public function getRole()
 {
     if (ArrayUtils::get($this->config, 'map_group_to_role', false)) {
         $groups = $this->driver->getGroups();
         $primaryGroupDn = ArrayUtils::findByKeyValue($groups, 'primary', true, 'dn');
         $role = RoleADLdap::whereDn($primaryGroupDn)->first();
         if (empty($role)) {
             foreach ($groups as $group) {
                 $groupDn = ArrayUtils::get($group, 'dn');
                 $role = RoleADLdap::whereDn($groupDn)->first();
                 if (!empty($role)) {
                     return $role->role_id;
                 }
             }
             return $this->defaultRole;
         }
         return $role->role_id;
     }
     return $this->defaultRole;
 }
Пример #2
0
 /**
  * Execute the console command.
  *
  * @return mixed
  */
 public function handle()
 {
     if (!class_exists('DreamFactory\\Core\\ADLdap\\Services\\ADLdap')) {
         $this->error('Command unavailable. Please install \'dreamfactory/df-adldap\' package to use this command.');
         return;
     }
     try {
         $serviceName = $this->argument('service');
         $username = $this->option('username');
         $password = $this->option('password');
         /** @type ADLdap $service */
         $service = ServiceHandler::getService($serviceName);
         $serviceModel = Service::find($service->getServiceId());
         $serviceType = $serviceModel->serviceType()->first();
         $serviceGroup = $serviceType->group;
         if ($serviceGroup !== ServiceTypeGroups::LDAP) {
             throw new BadRequestException('Invalid service name [' . $serviceName . ']. Please use a valid Active Directory service');
         }
         $this->line('Contacting your Active Directory server...');
         $service->authenticateAdminUser($username, $password);
         $this->line('Fetching Active Directory groups...');
         $groups = $service->getDriver()->listGroup(['dn', 'description']);
         $roles = [];
         foreach ($groups as $group) {
             $dfRole = RoleADLdap::whereDn($group['dn'])->first();
             if (empty($dfRole)) {
                 $role = ['name' => static::dnToRoleName($group['dn']), 'description' => $group['description'], 'is_active' => true, 'role_adldap_by_role_id' => [['dn' => $group['dn']]]];
                 $this->info('|--------------------------------------------------------------------');
                 $this->info('| DN: ' . $group['dn']);
                 $this->info('| Role Name: ' . $role['name']);
                 $this->info('| Description: ' . $role['description']);
                 $this->info('|--------------------------------------------------------------------');
                 $roles[] = $role;
             }
         }
         $roleCount = count($roles);
         if ($roleCount > 0) {
             $this->warn('Total Roles to import: [' . $roleCount . ']');
             if ($this->confirm('The above roles will be imported into your DreamFactroy instance based on your Active Directory groups. Do you wish to continue?')) {
                 $this->line('Importing Roles...');
                 $payload = ResourcesWrapper::wrapResources($roles);
                 ServiceHandler::handleRequest(Verbs::POST, 'system', 'role', ['continue' => true], $payload);
                 $this->info('Successfully imported all Active Directory groups as Roles.');
             } else {
                 $this->info('Aborted import process. No Roles were imported');
             }
         } else {
             if (count($groups) > 0 && $roleCount === 0) {
                 $this->info('All groups found on the Active Directory server are already imported.');
             } else {
                 $this->warn('No group was found on Active Directory server.');
             }
         }
     } catch (RestException $e) {
         $this->error($e->getMessage());
         if ($this->option('verbose')) {
             $this->error(print_r($e->getContext(), true));
         }
     } catch (\Exception $e) {
         $this->error($e->getMessage());
     }
 }