Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: autocreate new user when http external auth succeeded #3221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/login/index.php
Expand Up @@ -5,6 +5,10 @@

// http auth
if( !empty($_SERVER['PHP_AUTH_USER']) ) {
if (isset($config['httpauth_autocreate_user']) && $config['httpauth_autocreate_user'] === true) {
// auto-create guest accounts for http auth
$User->check_or_create($_SERVER['PHP_AUTH_USER']);
}
// try to authenticate
$User->authenticate ($_SERVER['PHP_AUTH_USER'], '');
// Redirect user where he came from, if unknown go to dashboard.
Expand Down
1 change: 1 addition & 0 deletions config.dist.php
Expand Up @@ -65,6 +65,7 @@
$config['resolve_emptyonly'] = true; // if true it will only update the ones without DNS entry!
$config['resolve_verbose'] = true; // verbose response - prints results, cron will email it to you!
$config['disable_main_login_form'] = false; // disable main login form if you want use another authentification method by default (SAML, LDAP, etc.)
$config['httpauth_autocreate_user'] = false; // don't create new user on http auth success first login


/**
Expand Down
56 changes: 56 additions & 0 deletions functions/classes/class.User.php
Expand Up @@ -746,6 +746,62 @@ public function is_folder_favourite ($subnetId) {
* -------------------------------
*/

/**
* Check / create automatically the user account without
* permissions.
* This is interesting for http auth backend/
*
* @access public
* @param string $username
* @return void
*/
public function check_or_create ($username) {
try {
$user = $this->Database->findObject("users", "username", $username);
}
catch (Exception $e) {
$this->Result->show("danger", _("Error: ").$e->getMessage(), true);
}

// if not result return false
$usert = (array) $user;
if(sizeof($usert)==0) {
$Admin = new Admin($this->Database, $admin_required = false);

// user properties (no group and http auth)
$values = array(
"id" => "",
"real_name" => $username,
"username" => $username,
"email" => $username."@example.com", // mandatory for user edit
"role" => "User",
"authMethod" => 2, // http
"lang" => 1,
"mailNotify" => "No",
"mailChangelog" => "No",
"theme" => "default",
"disabled" => "No",
);

// no permission
foreach ($this->get_modules_with_permissions() as $m) {
$permissions[$m] = 0;
}
$values['module_permissions'] = json_encode($permissions);

// execute
if(!$Admin->object_modify("users", "add", "id", $values)) {
$Result->show("danger", _("User")." add "._("failed").'!', true);
}
} else {
// Authorization change (logout maybe)
if ($this->user->id != $user->id) {
$this->user = null;
}
}
}


/**
* Main function for authenticating users
*
Expand Down