mirror of
https://github.com/lucaspalomodevelop/eventcally.git
synced 2026-03-13 00:07:22 +00:00
CLI for admin rights #36
This commit is contained in:
parent
53d144b58f
commit
f0dc5367a8
@ -31,6 +31,12 @@ flask db upgrade
|
||||
flask run --host 0.0.0.0
|
||||
```
|
||||
|
||||
## Administration
|
||||
|
||||
```sh
|
||||
flask user add-admin-roles super@hero.com
|
||||
```
|
||||
|
||||
## Configuration
|
||||
|
||||
Create `.env` file in the root directory or pass as environment variables.
|
||||
|
||||
@ -122,5 +122,8 @@ from project.views import (
|
||||
widget,
|
||||
)
|
||||
|
||||
# Command line
|
||||
import project.cli.user
|
||||
|
||||
if __name__ == "__main__": # pragma: no cover
|
||||
app.run()
|
||||
|
||||
0
project/cli/__init__.py
Normal file
0
project/cli/__init__.py
Normal file
17
project/cli/user.py
Normal file
17
project/cli/user.py
Normal file
@ -0,0 +1,17 @@
|
||||
import click
|
||||
from flask.cli import AppGroup
|
||||
from project import app, db
|
||||
from project.services.user import add_admin_roles_to_user
|
||||
|
||||
user_cli = AppGroup("user")
|
||||
|
||||
|
||||
@user_cli.command("add-admin-roles")
|
||||
@click.argument("email")
|
||||
def add_admin_roles(email):
|
||||
add_admin_roles_to_user(email)
|
||||
db.session.commit()
|
||||
click.echo(f"Admin roles were added to {email}.")
|
||||
|
||||
|
||||
app.cli.add_command(user_cli)
|
||||
@ -1,5 +1,5 @@
|
||||
from project import app, db
|
||||
from project.services.user import upsert_user_role, add_admin_roles_to_user
|
||||
from project.services.user import upsert_user_role
|
||||
from project.services.admin_unit import upsert_admin_unit_member_role
|
||||
from project.services.event import upsert_event_category
|
||||
from project.models import Location
|
||||
@ -42,7 +42,6 @@ def create_initial_data():
|
||||
|
||||
upsert_user_role("admin", "Administrator", admin_permissions)
|
||||
upsert_user_role("event_verifier", "Event expert", event_permissions)
|
||||
add_admin_roles_to_user("grams.daniel@gmail.com")
|
||||
|
||||
Location.update_coordinates()
|
||||
|
||||
|
||||
@ -2,23 +2,29 @@ from project import user_datastore
|
||||
from flask_security import hash_password
|
||||
|
||||
|
||||
def upsert_user(email, password="password"):
|
||||
def create_user(email, password):
|
||||
result = user_datastore.find_user(email=email)
|
||||
if result is None:
|
||||
result = user_datastore.create_user(
|
||||
email=email, password=hash_password(password)
|
||||
)
|
||||
|
||||
if result:
|
||||
raise ValueError("User with given email does already exist.")
|
||||
|
||||
result = user_datastore.create_user(email=email, password=hash_password(password))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
def add_roles_to_user(user_name, role_names):
|
||||
user = upsert_user(user_name)
|
||||
def add_roles_to_user(email, role_names):
|
||||
user = find_user_by_email(email)
|
||||
|
||||
if user is None:
|
||||
raise ValueError("User with given email does not exist.")
|
||||
|
||||
for role_name in role_names:
|
||||
user_datastore.add_role_to_user(user, role_name)
|
||||
|
||||
|
||||
def add_admin_roles_to_user(user_name):
|
||||
add_roles_to_user(user_name, ["admin", "event_verifier"])
|
||||
def add_admin_roles_to_user(email):
|
||||
add_roles_to_user(email, ["admin", "event_verifier"])
|
||||
|
||||
|
||||
def upsert_user_role(role_name, role_title, permissions):
|
||||
|
||||
0
tests/cli/__init__.py
Normal file
0
tests/cli/__init__.py
Normal file
19
tests/cli/test_user.py
Normal file
19
tests/cli/test_user.py
Normal file
@ -0,0 +1,19 @@
|
||||
def test_add_admin_roles(client, seeder, app):
|
||||
user_id, admin_unit_id = seeder.setup_base()
|
||||
|
||||
with app.app_context():
|
||||
from project.services.user import get_user
|
||||
|
||||
user = get_user(user_id)
|
||||
assert not user.has_role("admin")
|
||||
|
||||
runner = app.test_cli_runner()
|
||||
result = runner.invoke(args=["user", "add-admin-roles", "test@test.de"])
|
||||
assert "Admin roles were added to test@test.de." in result.output
|
||||
|
||||
with app.app_context():
|
||||
from project.services.user import get_user
|
||||
|
||||
user = get_user(user_id)
|
||||
is_admin = user.has_role("admin")
|
||||
assert is_admin
|
||||
@ -20,10 +20,17 @@ class Seeder(object):
|
||||
def create_user(
|
||||
self, email="test@test.de", password="MeinPasswortIstDasBeste", admin=False
|
||||
):
|
||||
from project.services.user import upsert_user, add_admin_roles_to_user
|
||||
from project.services.user import (
|
||||
find_user_by_email,
|
||||
create_user,
|
||||
add_admin_roles_to_user,
|
||||
)
|
||||
|
||||
with self._app.app_context():
|
||||
user = upsert_user(email, password)
|
||||
user = find_user_by_email(email)
|
||||
|
||||
if user is None:
|
||||
user = create_user(email, password)
|
||||
|
||||
if admin:
|
||||
add_admin_roles_to_user(email)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user