Skip to content

Commit 2feb562

Browse files
authored
Merge pull request piccolo-orm#99 from piccolo-orm/user_permissions_command
added `change_permissions` command
2 parents 6aa4917 + 91dd46a commit 2feb562

File tree

6 files changed

+89
-3
lines changed

6 files changed

+89
-3
lines changed

docs/src/piccolo/authentication/baseuser.rst

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ Change a user's password.
4141
4242
piccolo user change_password
4343
44+
user change_permissions
45+
~~~~~~~~~~~~~~~~~~~~~~~
46+
47+
Change a user's permissions. The options are ``--admin``, ``--superuser`` and
48+
``--active``, which change the corresponding attributes on ``BaseUser``.
49+
50+
For example:
51+
52+
.. code-block:: bash
53+
54+
piccolo user change_permissions some_user --active=true
55+
56+
The Piccolo Admin (see :ref:`Ecosystem`) uses these attributes to control who
57+
can login and what they can do.
58+
59+
* **active** and **admin** - must be true for a user to be able to login.
60+
* **superuser** - must be true for a user to be able to change other user's
61+
passwords.
62+
4463
-------------------------------------------------------------------------------
4564

4665
Within your code
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

tests/apps/user/commands/test_create.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def tearDown(self):
3535
"piccolo.apps.user.commands.create.get_is_superuser",
3636
return_value=True,
3737
)
38+
@patch(
39+
"piccolo.apps.user.commands.create.get_is_active", return_value=True,
40+
)
3841
def test_create(self, *args, **kwargs):
3942
create()
4043

@@ -45,6 +48,7 @@ def test_create(self, *args, **kwargs):
4548
& (BaseUser.username == "bob123")
4649
& (BaseUser.email == "[email protected]")
4750
& (BaseUser.superuser == True)
51+
& (BaseUser.active == True)
4852
)
4953
.run_sync()
5054
)

0 commit comments

Comments
 (0)