-
Notifications
You must be signed in to change notification settings - Fork 32
✨ web-api: user's privacy settings #6904
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
Merged
Merged
Changes from all commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
5750bbd
return username in profile
pcrespov 1233bd1
updates OAS
pcrespov 8b7e0b4
services/webserver api version: 0.47.0 → 0.48.0
pcrespov 17bed97
cleanup
pcrespov 6337748
cleanup tests
pcrespov 2eb0d91
patch
pcrespov 78b93ec
new table
pcrespov 23aa9c4
adding privacy
pcrespov 6d54ef3
patch
pcrespov be44ee4
move privacy cols to user
pcrespov ad92c5a
updates users plugin
pcrespov 06f13b8
drafts tests
pcrespov 7aebbbf
migration
pcrespov e4594d8
test pss
pcrespov 9ffb6fb
warning
pcrespov f8a019d
fixes migration
pcrespov fb80984
updates tests
pcrespov 245d841
udpates OAS
pcrespov 51fe8e3
examples
pcrespov 279a1c4
cleanup
pcrespov baeba07
updates OAS
pcrespov 982fb6f
update username
pcrespov bfa425d
tests models
pcrespov f15d8fc
model conversion ready
pcrespov abb4aa4
cleanup
pcrespov 1ab8b22
handles errors
pcrespov 174e9c3
changes
pcrespov c66c520
fixes tests
pcrespov 4ed3e04
moves package to models library
pcrespov ffe4ebf
minor
pcrespov 1fc1195
update OAS
pcrespov 2f54321
updates tests
pcrespov 5e10c29
updates tests
pcrespov d16e923
cleanup
pcrespov f6a3ead
api-removed-without-deprecation
pcrespov 0eadc8c
fixes test
pcrespov 5f6c627
Merge branch 'master' into is1779/user-api
odeimaiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
packages/models-library/src/models_library/api_schemas_webserver/users.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| import re | ||
| from datetime import date | ||
| from enum import Enum | ||
| from typing import Annotated, Literal | ||
|
|
||
| from models_library.api_schemas_webserver.groups import MyGroupsGet | ||
| from models_library.api_schemas_webserver.users_preferences import AggregatedPreferences | ||
| from models_library.basic_types import IDStr | ||
| from models_library.emails import LowerCaseEmailStr | ||
| from models_library.users import FirstNameStr, LastNameStr, UserID | ||
| from pydantic import BaseModel, ConfigDict, Field, field_validator | ||
|
|
||
| from ._base import InputSchema, OutputSchema | ||
|
|
||
|
|
||
| class ProfilePrivacyGet(OutputSchema): | ||
| hide_fullname: bool | ||
| hide_email: bool | ||
|
|
||
|
|
||
| class ProfilePrivacyUpdate(InputSchema): | ||
pcrespov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hide_fullname: bool | None = None | ||
| hide_email: bool | None = None | ||
|
|
||
|
|
||
| class ProfileGet(BaseModel): | ||
| # WARNING: do not use InputSchema until front-end is updated! | ||
| id: UserID | ||
| user_name: Annotated[ | ||
| IDStr, Field(description="Unique username identifier", alias="userName") | ||
| ] | ||
| first_name: FirstNameStr | None = None | ||
| last_name: LastNameStr | None = None | ||
| login: LowerCaseEmailStr | ||
|
|
||
| role: Literal["ANONYMOUS", "GUEST", "USER", "TESTER", "PRODUCT_OWNER", "ADMIN"] | ||
| groups: MyGroupsGet | None = None | ||
| gravatar_id: Annotated[str | None, Field(deprecated=True)] = None | ||
|
|
||
| expiration_date: Annotated[ | ||
| date | None, | ||
| Field( | ||
| description="If user has a trial account, it sets the expiration date, otherwise None", | ||
| alias="expirationDate", | ||
| ), | ||
| ] = None | ||
|
|
||
| privacy: ProfilePrivacyGet | ||
| preferences: AggregatedPreferences | ||
|
|
||
| model_config = ConfigDict( | ||
| # NOTE: old models have an hybrid between snake and camel cases! | ||
| # Should be unified at some point | ||
| populate_by_name=True, | ||
| json_schema_extra={ | ||
| "examples": [ | ||
| { | ||
| "id": 42, | ||
| "login": "[email protected]", | ||
| "userName": "bla42", | ||
| "role": "admin", # pre | ||
| "expirationDate": "2022-09-14", # optional | ||
| "preferences": {}, | ||
| "privacy": {"hide_fullname": 0, "hide_email": 1}, | ||
| }, | ||
| ] | ||
| }, | ||
| ) | ||
|
|
||
| @field_validator("role", mode="before") | ||
| @classmethod | ||
| def _to_upper_string(cls, v): | ||
| if isinstance(v, str): | ||
| return v.upper() | ||
| if isinstance(v, Enum): | ||
| return v.name.upper() | ||
| return v | ||
|
|
||
|
|
||
| class ProfileUpdate(BaseModel): | ||
| # WARNING: do not use InputSchema until front-end is updated! | ||
pcrespov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| first_name: FirstNameStr | None = None | ||
| last_name: LastNameStr | None = None | ||
| user_name: Annotated[IDStr | None, Field(alias="userName")] = None | ||
|
|
||
| privacy: ProfilePrivacyUpdate | None = None | ||
|
|
||
| model_config = ConfigDict( | ||
| json_schema_extra={ | ||
| "example": { | ||
| "first_name": "Pedro", | ||
| "last_name": "Crespo", | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| @field_validator("user_name") | ||
| @classmethod | ||
| def _validate_user_name(cls, value: str): | ||
| # Ensure valid characters (alphanumeric + . _ -) | ||
| if not re.match(r"^[a-zA-Z][a-zA-Z0-9._-]*$", value): | ||
| msg = f"Username '{value}' must start with a letter and can only contain letters, numbers and '_', '.' or '-'." | ||
| raise ValueError(msg) | ||
|
|
||
| # Ensure no consecutive special characters | ||
| if re.search(r"[_.-]{2,}", value): | ||
| msg = f"Username '{value}' cannot contain consecutive special characters like '__'." | ||
| raise ValueError(msg) | ||
|
|
||
| # Ensure it doesn't end with a special character | ||
| if {value[0], value[-1]}.intersection({"_", "-", "."}): | ||
| msg = f"Username '{value}' cannot end or start with a special character." | ||
| raise ValueError(msg) | ||
|
|
||
| # Check reserved words (example list; extend as needed) | ||
| reserved_words = { | ||
| "admin", | ||
| "root", | ||
| "system", | ||
| "null", | ||
| "undefined", | ||
| "support", | ||
| "moderator", | ||
| # NOTE: add here extra via env vars | ||
| } | ||
| if any(w in value.lower() for w in reserved_words): | ||
| msg = f"Username '{value}' cannot be used." | ||
| raise ValueError(msg) | ||
|
|
||
| return value | ||
45 changes: 45 additions & 0 deletions
45
...src/simcore_postgres_database/migration/versions/38c9ac332c58_new_user_privacy_columns.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| """new user privacy columns | ||
|
|
||
| Revision ID: 38c9ac332c58 | ||
| Revises: e5555076ef50 | ||
| Create Date: 2024-12-05 14:29:27.739650+00:00 | ||
|
|
||
| """ | ||
| import sqlalchemy as sa | ||
| from alembic import op | ||
|
|
||
| # revision identifiers, used by Alembic. | ||
| revision = "38c9ac332c58" | ||
| down_revision = "e5555076ef50" | ||
| branch_labels = None | ||
| depends_on = None | ||
|
|
||
|
|
||
| def upgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.add_column( | ||
| "users", | ||
| sa.Column( | ||
| "privacy_hide_fullname", | ||
| sa.Boolean(), | ||
| server_default=sa.text("true"), | ||
| nullable=False, | ||
| ), | ||
| ) | ||
| op.add_column( | ||
| "users", | ||
| sa.Column( | ||
| "privacy_hide_email", | ||
| sa.Boolean(), | ||
| server_default=sa.text("true"), | ||
| nullable=False, | ||
| ), | ||
| ) | ||
| # ### end Alembic commands ### | ||
|
|
||
|
|
||
| def downgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_column("users", "privacy_hide_email") | ||
| op.drop_column("users", "privacy_hide_fullname") | ||
| # ### end Alembic commands ### |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.