/** * 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); } }
/** * @inheritdoc */ public function authenticate() { $users = Loader::domain($this->domain)['users']; if (!isset($users[$this->username])) { return null; } $password = $users[$this->username]; if (Hash::check($this->password, $password)) { $newUser = $this->model(); $newUser->{Loader::username()} = $this->username; $newUser->{Loader::password()} = $password; $newUser->enabled = true; $newUser->save(); return $newUser; } return null; }
/** * 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); }
/** * @inheritdoc */ public function authenticate() { $this->config = Loader::domain($this->domain); $ldap = new Connection(); $ldap->connect($this->config['hosts']); if ($find = $this->resolver->native()->findBy(Loader::username(), $this->username)) { $this->model = $find; $this->model->{Loader::password()} = null; $this->model->save(); } if (!$ldap->bind($this->username, $this->password)) { Log::warning('Cannot bind to LDAP with ' . $this->username); return null; } $user = $ldap->searchEntry($this->config['baseDN'], $this->config['mappings'], 'samaccountname=' . $this->login); if (!$user) { Log::warning('User ' . $this->username . ' not found in baseDN.'); return null; } return $this->register($user); }
/** * @inheritdoc */ public function authenticate() { if (!extension_loaded('imap')) { throw new \Exception("Cannot use IMAP provider without imap module.", 1); } $this->config = Loader::domain($this->domain); foreach ($this->config['hosts'] as $name => $address) { try { $this->connection = \imap_open("{" . $address . "/novalidate-cert}", $this->username, $this->password, null, 1, array("DISABLE_AUTHENTICATOR" => "GSSAPI")); if ($this->connection) { break; } } catch (\Exception $e) { Log::warning(' [IMAP] Cannot connect to ' . $name . ': ' . $e->getMessage()); } } if (!$this->connection) { return false; } if ($user = $this->resolver->native()->findBy($this->config['mappings'][Loader::username()], $this->username)) { return $user; } return false; }