Skip to content

Commit 699a01f

Browse files
committed
added change_permissions command
1 parent 6aa4917 commit 699a01f

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import typing as t
2+
3+
from piccolo.apps.user.tables import BaseUser
4+
from piccolo.utils.warnings import colored_string, Level
5+
6+
if t.TYPE_CHECKING:
7+
from piccolo.columns import Column
8+
9+
10+
async def change_permissions(
11+
username: str,
12+
admin: t.Optional[bool] = None,
13+
superuser: t.Optional[bool] = None,
14+
active: t.Optional[bool] = None,
15+
):
16+
"""
17+
Change a user's permissions.
18+
19+
:param username:
20+
Change the permissions for this user.
21+
:param admin:
22+
Set `admin` for the user (true / false).
23+
:param superuser:
24+
Set `superuser` for the user (true / false).
25+
:param active:
26+
Set `active` for the user (true / false).
27+
28+
"""
29+
if not await BaseUser.exists().where(BaseUser.username == username).run():
30+
print(
31+
colored_string(
32+
f"User {username} doesn't exist!", level=Level.medium
33+
)
34+
)
35+
return
36+
37+
params: t.Dict[Column, bool] = {}
38+
39+
if admin is not None:
40+
params[BaseUser.admin] = admin
41+
42+
if superuser is not None:
43+
params[BaseUser.superuser] = superuser
44+
45+
if active is not None:
46+
params[BaseUser.active] = active
47+
48+
await BaseUser.update(params).where(BaseUser.username == username).run()
49+
50+
print(f"Updated permissions for {username}")

piccolo/apps/user/commands/create.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,17 @@ def get_is_superuser() -> bool:
4444
return superuser == "y"
4545

4646

47+
def get_is_active() -> bool:
48+
while True:
49+
active = input("Active? Enter y or n:\n")
50+
if active in ("y", "n"):
51+
break
52+
else:
53+
print("Unrecognised option")
54+
55+
return active == "y"
56+
57+
4758
def create():
4859
"""
4960
Create a new user.
@@ -61,13 +72,14 @@ def create():
6172

6273
is_admin = get_is_admin()
6374
is_superuser = get_is_superuser()
75+
is_active = get_is_active()
6476

6577
user = BaseUser(
6678
username=username,
6779
password=password,
6880
admin=is_admin,
6981
email=email,
70-
active=True,
82+
active=is_active,
7183
superuser=is_superuser,
7284
)
7385
user.save().run_sync()

piccolo/apps/user/piccolo_app.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from piccolo.conf.apps import AppConfig
44
from .commands.change_password import change_password
5+
from .commands.change_permissions import change_permissions
56
from .commands.create import create
67
from .tables import BaseUser
78

@@ -16,5 +17,5 @@
1617
),
1718
table_classes=[BaseUser],
1819
migration_dependencies=[],
19-
commands=[create, change_password],
20+
commands=[create, change_password, change_permissions],
2021
)

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ asyncpg>=0.21.0
33
black
44
colorama>=0.4.0
55
Jinja2>=2.11.0
6-
targ>=0.1.0
6+
targ>=0.2.0
77
inflection>=0.5.1

0 commit comments

Comments
 (0)