Skip to content

Instantly share code, notes, and snippets.

@samhernandez
Last active December 2, 2022 21:40
Show Gist options
  • Save samhernandez/1c53b7c75c64f8842fb6da27b9263e9d to your computer and use it in GitHub Desktop.
Save samhernandez/1c53b7c75c64f8842fb6da27b9263e9d to your computer and use it in GitHub Desktop.
Craft 3 gain access to admin account for support cases or when owner loses access
{#
Resets the username, password, and email address
of the first found Admin account in case of
lost admin access or for support cases.
#}
{% set values = {
username: 'me',
password: craft.app.security.hashPassword('mypassword'),
email: 'me@site.com',
passwordResetRequired: 0
} %}
{% set where = {
id: craft.query().select('id').from('{{%users}}').where({admin: 1}).scalar()
} %}
{% set rowsAffected = craft.query()
.createCommand()
.update('{{%users}}', values, where)
.execute()
%}
Rows affected: {{ rowsAffected }}
@lukeholder
Copy link

Replace 'users' with '{{%users}}'

@samhernandez
Copy link
Author

Thanks dude!

@tr3motino
Copy link

THX!

@Siyabulela
Copy link

This is great, thanks man. You might wanna add passwordResetRequired: 0. It didn't work for me until I added that line under email:. I checked Require a password reset on next login by mistake

@samhernandez
Copy link
Author

@Siyabulela thanks for that! updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment