/** * Creates a user with Sentry by the supplied corp user object. * * @param User $user * * @return bool|User */ private function createUser(User $user) { if ($user->username && $user->email) { $first_name = ''; $last_name = ''; /* * An LDAP user may not have a name, so we'll explode it * by a comma to see if they have a fully separated name */ if ($user->name) { $name = explode(',', $user->name); if (array_key_exists(0, $name)) { $last_name = $name[0]; } if (array_key_exists(1, $name)) { $first_name = $name[1]; } } $data = ['email' => $user->email, 'password' => str_random(20), 'username' => $user->username, 'last_name' => $last_name, 'first_name' => $first_name]; $roles = []; if ($user->group) { $roles[] = $this->sentry->createOrUpdateRole($user->group); } if ($user->type) { $roles[] = $this->sentry->createOrUpdateRole($user->type); } $user = $this->sentry->createUser($data, $roles); return $user; } return false; }
/** * Create or Update a User for authentication for use with ldap. * * @param array $credentials * * @return \Cartalyst\Sentry\Users\Eloquent\User */ public function createOrUpdateLdapUser(array $credentials) { $loginAttribute = $this->config->setPrefix('cartalyst.sentry')->get('users.login_attribute'); $username = $credentials[$loginAttribute]; $password = $credentials['password']; // If a user is found, update their password to match active-directory $user = $this->model->where('username', $username)->first(); if ($user) { $this->sentry->updatePasswordById($user->id, $password); } else { // If a user is not found in the database, create their web account $ldapUser = $this->ldap->user($username); $fullName = explode(',', $ldapUser->name); $lastName = array_key_exists(0, $fullName) ? $fullName[0] : null; $firstName = array_key_exists(1, $fullName) ? $fullName[1] : null; $data = ['email' => $ldapUser->email ? $ldapUser->email : $username, 'username' => $username, 'password' => $password, 'last_name' => (string) $lastName, 'first_name' => (string) $firstName, 'activated' => 1]; // Default all group $roles = ['all']; if (in_array($ldapUser->group, config('maintenance.groups.ldap.administrators'))) { $roles[] = 'administrators'; } else { if (in_array($ldapUser->group, config('maintenance.groups.ldap.workers'))) { $roles[] = 'workers'; } else { $roles[] = 'client'; } } $user = $this->sentry->createUser($data, $roles); } return $user; }
/** * Creates or updates a user using LDAP and Sentry. * * @param array $credentials * * @return mixed */ public function createOrUpdateLdapUser(array $credentials) { $loginAttribute = $this->config->setPrefix('cartalyst.sentry')->get('users.login_attribute'); $username = $credentials[$loginAttribute]; $password = $credentials['password']; // If a user is found, update their password to match active-directory $user = $this->model()->where('username', $username)->first(); if ($user) { $this->sentry->updatePasswordById($user->id, $password); } else { // If a user is not found, create their web account $ldapUser = $this->ldap->user($username); $fullName = explode(',', $ldapUser->name); $lastName = array_key_exists(0, $fullName) ? $fullName[0] : null; $firstName = array_key_exists(1, $fullName) ? $fullName[1] : null; $data = ['email' => $ldapUser->email, 'password' => $password, 'username' => $username, 'last_name' => (string) $lastName, 'first_name' => (string) $firstName, 'activated' => 1]; $user = $this->sentry->createUser($data, ['all_users', 'customers', 'workers']); } return $user; }