Skip to content

Commit

Permalink
(fix)[User] replace OWNER with ADMIN
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaisonneuve committed Jun 26, 2024
1 parent 98ac9d5 commit 0c8eb2e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ We recommend installing using Ubuntu 22.04 with python 3.10.
Required packages are listed in the ```settings.json``` file, for now the packages must be installed manually.

```bash
pip install openpyxl==3.1.4 awesome-slugify==1.6.5 fastapi==0.111.0 pydantic==2.7.4 peewee==3.17.5 psutil==5.9.8 pyjwt==2.8.0 pymysql==1.1.1 python-multipart==0.0.9 starlette-context==0.3.6 uvicorn==0.30.1 charset-normalizer==3.3.2 dill==0.3.8 numpy==1.26.4 pandas==2.2.2 matplotlib==3.9.0 scipy==1.13.1 scikit-learn==1.5.0 openai==1.34.0 typing_extensions==4.12.2 boto3==1.34.127 "boto3-stubs[s3]==1.34.127" plotly==5.22.0 simplejson==3.19.2 xmltodict==0.13.0 streamlit==1.35.0 beautifulsoup4==4.12.3 grpcio==1.64.1
pip install openpyxl==3.1.4 awesome-slugify==1.6.5 fastapi==0.111.0 pydantic==2.7.4 peewee==3.17.5 psutil==5.9.8 pyjwt==2.8.0 pymysql==1.1.1 python-multipart==0.0.9 starlette-context==0.3.6 uvicorn==0.30.1 charset-normalizer==2.1.1 dill==0.3.8 numpy==1.26.4 pandas==2.2.2 matplotlib==3.9.0 scipy==1.13.1 scikit-learn==1.5.0 openai==1.34.0 typing_extensions==4.12.2 boto3==1.34.127 "boto3-stubs[s3]==1.34.127" plotly==5.22.0 simplejson==3.19.2 xmltodict==0.13.0 streamlit==1.35.0 beautifulsoup4==4.12.3 grpcio==1.64.1
```

#### Usage
Expand Down
8 changes: 4 additions & 4 deletions src/gws_core/user/current_user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,14 @@ def check_is_sysuser(cls):
raise UnauthorizedException(detail="Unauthorized: sysuser required")

@classmethod
def check_is_owner(cls):
def check_is_admin(cls):
try:
user = CurrentUserService.get_and_check_current_user()
except:
raise UnauthorizedException(detail="Unauthorized: owner required")
raise UnauthorizedException(detail="Unauthorized: admin required")

if not user.is_owner:
raise UnauthorizedException(detail="Unauthorized: owner required")
if not user.is_admin:
raise UnauthorizedException(detail="Unauthorized: admin required")


class AuthenticateUser:
Expand Down
15 changes: 2 additions & 13 deletions src/gws_core/user/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@

@final
class User(Model):
"""
User class
:property email: The user email
:type email: `str`
:property group: The user group (`sysuser`, `admin`, `owner` or `user`)
:type group: `str`
:property is_active: True if the is active, False otherwise
:type is_active: `bool`
"""

email: str = CharField(default=False, index=True)
first_name: str = CharField(default=False)
last_name: str = CharField(default=False)
Expand Down Expand Up @@ -68,8 +57,8 @@ def full_name(self):
return " ".join([self.first_name, self.last_name]).strip()

@property
def is_owner(self):
return self.group == UserGroup.OWNER
def is_admin(self):
return self.group == UserGroup.ADMIN

@property
def is_sysuser(self):
Expand Down
4 changes: 2 additions & 2 deletions src/gws_core/user/user_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Enum to define the role needed for a protocol
class UserGroup(Enum):
SYSUSER = "SYSUSER"
OWNER = "OWNER"
ADMIN = "ADMIN"
USER = "USER"

@classmethod
Expand All @@ -21,7 +21,7 @@ def get_value(cls, user_group: 'UserGroup') -> int:

switcher = {
UserGroup.SYSUSER: 0,
UserGroup.OWNER: 5,
UserGroup.ADMIN: 5,
UserGroup.USER: 15,
}
return switcher.get(user_group)
Expand Down
8 changes: 4 additions & 4 deletions src/gws_core/user/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ def set_user_active(cls, id_: str, is_active: bool) -> User:
if not is_active:
if user.is_sysuser:
raise BadRequestException("Cannot deactivate the system user")
if user.is_owner:
# check if this is the last owner
if User.select().where((User.group == UserGroup.OWNER) & (User.is_active == True)).count() == 1:
raise BadRequestException("Cannot deactivate the last owner")
if user.is_admin:
# check if this is the last admin
if User.select().where((User.group == UserGroup.ADMIN) & (User.is_active == True)).count() == 1:
raise BadRequestException("Cannot deactivate the last admin")

user.is_active = is_active
return user.save()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_gws_core/test_dev_env_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_dev_login(self):
email="[email protected]",
first_name="Firstname test",
last_name="Lastname test",
group=UserGroup.OWNER,
group=UserGroup.ADMIN,
is_active=True,
theme=UserTheme.LIGHT_THEME,
lang=UserLanguage.EN,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_gws_core/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def test_deactivate_user(self):
user.email = "[email protected]"
user.first_name = "Michel"
user.last_name = "Pol"
user.group = UserGroup.OWNER
user.group = UserGroup.ADMIN
user.save()

user2: User = User()
user2.email = "[email protected]"
user2.first_name = "Jack"
user2.last_name = "Beauregard"
user2.group = UserGroup.OWNER
user2.group = UserGroup.ADMIN
user2.save()

user3: User = User()
Expand All @@ -86,7 +86,7 @@ def test_deactivate_user(self):
UserService.deactivate_user(user.id)
UserService.deactivate_user(user3.id)

# can't deactivate the last owner
# can't deactivate the last admin
self.assertRaises(BadRequestException, UserService.deactivate_user, user2.id)

def test_user_search(self):
Expand Down

0 comments on commit 0c8eb2e

Please sign in to comment.