Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add concept of user groups #1062

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions pydatalab/schemas/cell.json
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,64 @@
"name"
]
},
"Group": {
"title": "Group",
"description": "A model that describes a group of users, for the sake\nof applying group permissions.\n\nEach `Person` model can point to a given group.",
"type": "object",
"properties": {
"type": {
"title": "Type",
"default": "groups",
"const": "groups",
"type": "string"
},
"immutable_id": {
"title": "Immutable ID",
"format": "uuid",
"type": "string"
},
"last_modified": {
"title": "Last Modified",
"type": "string",
"format": "date-time"
},
"relationships": {
"title": "Relationships",
"type": "array",
"items": {
"$ref": "#/definitions/TypedRelationship"
}
},
"group_id": {
"title": "Group Id",
"minLength": 1,
"maxLength": 40,
"pattern": "^(?:[a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9._-]+[a-zA-Z0-9])$",
"type": "string"
},
"display_name": {
"title": "Display Name",
"minLength": 1,
"maxLength": 150,
"type": "string"
},
"description": {
"title": "Description",
"type": "string"
},
"group_admins": {
"title": "Group Admins",
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"group_id",
"display_name"
]
},
"AccountStatus": {
"title": "AccountStatus",
"description": "A string enum representing the account status.",
Expand Down Expand Up @@ -354,6 +412,13 @@
"type": "string",
"format": "email"
},
"groups": {
"title": "Groups",
"type": "array",
"items": {
"$ref": "#/definitions/Group"
}
},
"managers": {
"title": "Managers",
"type": "array",
Expand Down
65 changes: 65 additions & 0 deletions pydatalab/schemas/equipment.json
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,64 @@
"name"
]
},
"Group": {
"title": "Group",
"description": "A model that describes a group of users, for the sake\nof applying group permissions.\n\nEach `Person` model can point to a given group.",
"type": "object",
"properties": {
"type": {
"title": "Type",
"default": "groups",
"const": "groups",
"type": "string"
},
"immutable_id": {
"title": "Immutable ID",
"format": "uuid",
"type": "string"
},
"last_modified": {
"title": "Last Modified",
"type": "string",
"format": "date-time"
},
"relationships": {
"title": "Relationships",
"type": "array",
"items": {
"$ref": "#/definitions/TypedRelationship"
}
},
"group_id": {
"title": "Group Id",
"minLength": 1,
"maxLength": 40,
"pattern": "^(?:[a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9._-]+[a-zA-Z0-9])$",
"type": "string"
},
"display_name": {
"title": "Display Name",
"minLength": 1,
"maxLength": 150,
"type": "string"
},
"description": {
"title": "Description",
"type": "string"
},
"group_admins": {
"title": "Group Admins",
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"group_id",
"display_name"
]
},
"AccountStatus": {
"title": "AccountStatus",
"description": "A string enum representing the account status.",
Expand Down Expand Up @@ -309,6 +367,13 @@
"type": "string",
"format": "email"
},
"groups": {
"title": "Groups",
"type": "array",
"items": {
"$ref": "#/definitions/Group"
}
},
"managers": {
"title": "Managers",
"type": "array",
Expand Down
65 changes: 65 additions & 0 deletions pydatalab/schemas/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,64 @@
"name"
]
},
"Group": {
"title": "Group",
"description": "A model that describes a group of users, for the sake\nof applying group permissions.\n\nEach `Person` model can point to a given group.",
"type": "object",
"properties": {
"type": {
"title": "Type",
"default": "groups",
"const": "groups",
"type": "string"
},
"immutable_id": {
"title": "Immutable ID",
"format": "uuid",
"type": "string"
},
"last_modified": {
"title": "Last Modified",
"type": "string",
"format": "date-time"
},
"relationships": {
"title": "Relationships",
"type": "array",
"items": {
"$ref": "#/definitions/TypedRelationship"
}
},
"group_id": {
"title": "Group Id",
"minLength": 1,
"maxLength": 40,
"pattern": "^(?:[a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9._-]+[a-zA-Z0-9])$",
"type": "string"
},
"display_name": {
"title": "Display Name",
"minLength": 1,
"maxLength": 150,
"type": "string"
},
"description": {
"title": "Description",
"type": "string"
},
"group_admins": {
"title": "Group Admins",
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"group_id",
"display_name"
]
},
"AccountStatus": {
"title": "AccountStatus",
"description": "A string enum representing the account status.",
Expand Down Expand Up @@ -313,6 +371,13 @@
"type": "string",
"format": "email"
},
"groups": {
"title": "Groups",
"type": "array",
"items": {
"$ref": "#/definitions/Group"
}
},
"managers": {
"title": "Managers",
"type": "array",
Expand Down
65 changes: 65 additions & 0 deletions pydatalab/schemas/startingmaterial.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,64 @@
"name"
]
},
"Group": {
"title": "Group",
"description": "A model that describes a group of users, for the sake\nof applying group permissions.\n\nEach `Person` model can point to a given group.",
"type": "object",
"properties": {
"type": {
"title": "Type",
"default": "groups",
"const": "groups",
"type": "string"
},
"immutable_id": {
"title": "Immutable ID",
"format": "uuid",
"type": "string"
},
"last_modified": {
"title": "Last Modified",
"type": "string",
"format": "date-time"
},
"relationships": {
"title": "Relationships",
"type": "array",
"items": {
"$ref": "#/definitions/TypedRelationship"
}
},
"group_id": {
"title": "Group Id",
"minLength": 1,
"maxLength": 40,
"pattern": "^(?:[a-zA-Z0-9]+|[a-zA-Z0-9][a-zA-Z0-9._-]+[a-zA-Z0-9])$",
"type": "string"
},
"display_name": {
"title": "Display Name",
"minLength": 1,
"maxLength": 150,
"type": "string"
},
"description": {
"title": "Description",
"type": "string"
},
"group_admins": {
"title": "Group Admins",
"type": "array",
"items": {
"type": "string"
}
}
},
"required": [
"group_id",
"display_name"
]
},
"AccountStatus": {
"title": "AccountStatus",
"description": "A string enum representing the account status.",
Expand Down Expand Up @@ -367,6 +425,13 @@
"type": "string",
"format": "email"
},
"groups": {
"title": "Groups",
"type": "array",
"items": {
"$ref": "#/definitions/Group"
}
},
"managers": {
"title": "Managers",
"type": "array",
Expand Down
21 changes: 20 additions & 1 deletion pydatalab/src/pydatalab/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from flask_login import LoginManager, UserMixin

from pydatalab.models import Person
from pydatalab.models.people import AccountStatus, Identity, IdentityType
from pydatalab.models.people import AccountStatus, Group, Identity, IdentityType
from pydatalab.models.utils import UserRole
from pydatalab.mongo import flask_mongo

Expand Down Expand Up @@ -68,6 +68,11 @@ def identities(self) -> List[Identity]:
"""Returns the list of identities of the user."""
return self.person.identities

@property
def groups(self) -> List[Group]:
"""Returns the list of groups that the user is a member of."""
return self.person.groups

@property
def identity_types(self) -> List[IdentityType]:
"""Returns a list of the identity types associated with the user."""
Expand All @@ -88,6 +93,20 @@ def get_by_id_cached(user_id):
return get_by_id(user_id)


def groups_lookup() -> dict:
return {
"from": "groups",
"let": {"group_ids": "$group_ids"},
"pipeline": [
{"$match": {"$expr": {"$in": ["$_id", {"$ifNull": ["$$group_ids", []]}]}}},
{"$addFields": {"__order": {"$indexOfArray": ["$$group_ids", "$_id"]}}},
{"$sort": {"__order": 1}},
{"$project": {"_id": 1, "display_name": 1}},
],
"as": "groups",
}


def get_by_id(user_id: str) -> Optional[LoginUser]:
"""Lookup the user database ID and create a new `LoginUser`
with the relevant metadata.
Expand Down
Loading
Loading