/** * Execute the console command. * * @return void */ public function fire() { $domain = $this->argument('domain'); $username = $this->option('username'); $password = $this->option('password'); if (!$username) { $username = $this->ask('<info>Administrator username for <error>' . $domain . '</error>: </info>'); } if (!strstr($username, '@')) { $username .= '@' . $domain; } if (!$password) { $password = $this->secret('<info>Password: </info>'); } if (!Loader::hasDomain($domain)) { $this->error('Domain ' . $domain . ' not found in configuration.'); exit(1); } $config = Loader::domain($domain); $ldap = new Connection(); $ldap->connect($config['hosts']); if (!$ldap->bind($username, $password)) { $this->error('Bind to ' . $domain . ' with user ' . $username . ' failed.'); exit(1); } $entries = $ldap->search($config['baseDN'], $config['mappings'], '(&(objectClass=user)(objectCategory=person))'); if (!$entries) { $this->error('Users not found.'); exit(1); } $ldapMapping = new LdapMapping($config['mappings']); $class = '\\' . ltrim(Loader::user(), '\\'); $usernameField = strtolower($config['mappings']['fields'][Loader::username()]); foreach ($entries as $entry) { if (!is_array($entry)) { continue; } if (!isset($entry[$usernameField])) { continue; } $model = new $class(); $user = $model->where(Loader::username(), $entry[$usernameField][0])->first(); if ($user) { $model = $user; $this->info('Updating ' . $entry[$usernameField][0]); } else { $this->info('Adding ' . $entry[$usernameField][0]); } $ldapMapping->map($entry, $model); } }
/** * Get provider for credentials * * @param array $credentials * * @return ProviderInterface */ public function provider($credentials) { $this->checkFields($credentials); if (!strstr($credentials[Loader::username()], '@')) { if ($this->defaultDomain()) { $credentials[Loader::username()] .= '@' . $this->defaultDomain(); $this->provider = $this->resolver->get(Loader::domain($this->defaultDomain())['provider'], $credentials); } else { $this->provider = $this->native($credentials); } return $this->provider; } $domain = explode("@", $credentials[Loader::username()])[1]; if (Loader::hasDomain($domain) and $this->provider = $this->resolver->get(Loader::domain($domain)['provider'], $credentials)) { return $this->provider; } return $this->native($credentials); }