Expose user login functionality
This commit is contained in:
parent
97f5ac7e1a
commit
8a5b066839
8 changed files with 246 additions and 8 deletions
|
|
@ -36,10 +36,25 @@ var errLoginUnknownError = errors.New("Login: Unknown Error")
|
|||
|
||||
// Login generates a login link for the user with the given username
|
||||
func (u *Users) Login(ctx context.Context, server *phpx.Server, username string) (dest *url.URL, err error) {
|
||||
return u.LoginWithOpt(ctx, server, username, LoginOptions{
|
||||
Destination: "/",
|
||||
CreateIfMissing: false,
|
||||
GrantAdminRole: false,
|
||||
})
|
||||
}
|
||||
|
||||
type LoginOptions struct {
|
||||
Destination string
|
||||
CreateIfMissing bool
|
||||
GrantAdminRole bool
|
||||
}
|
||||
|
||||
// LoginOrCreate generates a login link for the user with the given username and options
|
||||
func (u *Users) LoginWithOpt(ctx context.Context, server *phpx.Server, username string, opts LoginOptions) (dest *url.URL, err error) {
|
||||
|
||||
// generate a (relative) link
|
||||
var path string
|
||||
err = u.Dependencies.PHP.ExecScript(ctx, server, &path, usersPHP, "get_login_link", username)
|
||||
err = u.Dependencies.PHP.ExecScript(ctx, server, &path, usersPHP, "get_login_link", username, opts.Destination, opts.CreateIfMissing, opts.GrantAdminRole)
|
||||
|
||||
// if something went wrong, return
|
||||
if err != nil {
|
||||
|
|
|
|||
|
|
@ -35,9 +35,21 @@ function check_password_hash($password, $hash): bool {
|
|||
return \Drupal::service('password')->check($password, $hash);
|
||||
}
|
||||
|
||||
function get_login_link($name): string {
|
||||
function get_login_link($name, $destination = "", $update_user = FALSE, $grant_admin = FALSE): string {
|
||||
$account = user_load_by_name($name);
|
||||
if (!$account) return "";
|
||||
if (!$account) {
|
||||
if (!$update_user) return "";
|
||||
$account = create_new_disabled_user($name);
|
||||
}
|
||||
|
||||
if ($update_user && $grant_admin) {
|
||||
$account->addRole('administrator');
|
||||
}
|
||||
|
||||
if ($update_user) {
|
||||
$account->save();
|
||||
}
|
||||
|
||||
|
||||
$timestamp = \Drupal::time()->getRequestTime();
|
||||
return Url::fromRoute(
|
||||
|
|
@ -49,8 +61,17 @@ function get_login_link($name): string {
|
|||
],
|
||||
[
|
||||
'absolute' => false,
|
||||
'query' => ['destination' => '/'],
|
||||
'query' => ['destination' => $destination],
|
||||
'language' => \Drupal::languageManager()->getLanguage($account->getPreferredLangcode()),
|
||||
]
|
||||
)->toString();
|
||||
}
|
||||
|
||||
function create_new_disabled_user($name) {
|
||||
$account = User::create([
|
||||
'name' => $name,
|
||||
]);
|
||||
$account->activate();
|
||||
$account->save();
|
||||
return $account;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue