-
Notifications
You must be signed in to change notification settings - Fork 37
[MDS-6691] Save more data about MS users #3724
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01f0540
parse token data in user-profile resource from MS users and update th…
taraepp cb27506
remove console log
taraepp 35363cb
add fix for cypress tests
taraepp 9d3939c
pr comments
taraepp c6bfec5
remove param required for post
taraepp bb28734
rename function
taraepp 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
34 changes: 34 additions & 0 deletions
34
migrations/sql/V2025.10.30.05.04__minespace_user_new_columns.sql
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,34 @@ | ||
| -- Add new columns to minespace_user table to align with user table structure | ||
| ALTER TABLE minespace_user | ||
| ADD COLUMN sub VARCHAR, | ||
| ADD COLUMN email VARCHAR, | ||
| ADD COLUMN given_name VARCHAR, | ||
| ADD COLUMN family_name VARCHAR, | ||
| ADD COLUMN display_name VARCHAR, | ||
| ADD COLUMN identity_provider VARCHAR, | ||
| ADD COLUMN bceid_user_guid VARCHAR, | ||
| ADD COLUMN last_logged_in TIMESTAMPTZ, | ||
| ADD COLUMN create_user VARCHAR(255), | ||
| ADD COLUMN create_timestamp TIMESTAMP WITH TIME ZONE DEFAULT now(), | ||
| ADD COLUMN update_user VARCHAR(255), | ||
| ADD COLUMN update_timestamp TIMESTAMP WITH TIME ZONE DEFAULT now(); | ||
|
|
||
| ALTER TABLE minespace_user | ||
| RENAME COLUMN email_or_username TO bceid_username; | ||
|
|
||
| ALTER TABLE minespace_user | ||
| DROP COLUMN IF EXISTS keycloak_guid; | ||
|
|
||
| COMMENT ON COLUMN minespace_user.sub IS 'User subject identifier from identity provider'; | ||
| COMMENT ON COLUMN minespace_user.email IS 'User email address'; | ||
| COMMENT ON COLUMN minespace_user.given_name IS 'User given/first name'; | ||
| COMMENT ON COLUMN minespace_user.family_name IS 'User family/last name'; | ||
| COMMENT ON COLUMN minespace_user.display_name IS 'User display name'; | ||
| COMMENT ON COLUMN minespace_user.bceid_username IS 'BCeID username (renamed from email_or_username)'; | ||
| COMMENT ON COLUMN minespace_user.identity_provider IS 'Identity provider used for authentication'; | ||
| COMMENT ON COLUMN minespace_user.bceid_user_guid IS 'BCeID user GUID'; | ||
| COMMENT ON COLUMN minespace_user.last_logged_in IS 'Timestamp of last login'; | ||
| COMMENT ON COLUMN minespace_user.create_user IS 'User who created the record'; | ||
| COMMENT ON COLUMN minespace_user.create_timestamp IS 'Timestamp when record was created'; | ||
| COMMENT ON COLUMN minespace_user.update_user IS 'User who last updated the record'; | ||
| COMMENT ON COLUMN minespace_user.update_timestamp IS 'Timestamp when record was last updated'; |
26 changes: 26 additions & 0 deletions
26
migrations/sql/V2025.10.30.05.05__add_minespace_user_version_history_table.sql
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,26 @@ | ||
| -- This file was generated by the generate_history_table_ddl command | ||
| -- The file contains the corresponding history table definition for the minespace_user table | ||
| CREATE TABLE minespace_user_version ( | ||
| create_user VARCHAR(60), | ||
| create_timestamp TIMESTAMP WITHOUT TIME ZONE, | ||
| update_user VARCHAR(60), | ||
| update_timestamp TIMESTAMP WITHOUT TIME ZONE, | ||
| deleted_ind BOOLEAN default FALSE, | ||
| user_id INTEGER NOT NULL, | ||
| bceid_username VARCHAR(100), | ||
| sub VARCHAR, | ||
| email VARCHAR, | ||
| given_name VARCHAR, | ||
| family_name VARCHAR, | ||
| display_name VARCHAR, | ||
| identity_provider VARCHAR, | ||
| bceid_user_guid VARCHAR, | ||
| last_logged_in TIMESTAMP WITHOUT TIME ZONE, | ||
| transaction_id BIGINT NOT NULL, | ||
| end_transaction_id BIGINT, | ||
| operation_type SMALLINT NOT NULL, | ||
| PRIMARY KEY (user_id, transaction_id) | ||
| ); | ||
| CREATE INDEX ix_minespace_user_version_operation_type ON minespace_user_version (operation_type); | ||
| CREATE INDEX ix_minespace_user_version_end_transaction_id ON minespace_user_version (end_transaction_id); | ||
| CREATE INDEX ix_minespace_user_version_transaction_id ON minespace_user_version (transaction_id); |
6 changes: 6 additions & 0 deletions
6
migrations/sql/V2025.10.30.05.06__add_minespace_user_version_history_table_backfill.sql
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,6 @@ | ||
| -- This file was generated by the generate_history_table_ddl command | ||
| -- The file contains the data migration to backfill history records for the minespace_user table | ||
| with transaction AS (insert into transaction(id) values(DEFAULT) RETURNING id) | ||
| insert into minespace_user_version (transaction_id, operation_type, end_transaction_id, "create_user", "create_timestamp", "update_user", "update_timestamp", "deleted_ind", "user_id", "bceid_username", "sub", "email", "given_name", "family_name", "display_name", "identity_provider", "bceid_user_guid", "last_logged_in") | ||
| select t.id, '0', null, "create_user", "create_timestamp", "update_user", "update_timestamp", "deleted_ind", "user_id", "bceid_username", "sub", "email", "given_name", "family_name", "display_name", "identity_provider", "bceid_user_guid", "last_logged_in" | ||
| from "minespace_user",transaction t; |
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
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
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
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
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 |
|---|---|---|
| @@ -1,21 +1,30 @@ | ||
| import uuid, re | ||
|
|
||
| from sqlalchemy.dialects.postgresql import UUID | ||
| from sqlalchemy.orm import validates | ||
| from sqlalchemy.schema import FetchedValue | ||
| from sqlalchemy.ext.hybrid import hybrid_property | ||
| from app.extensions import db | ||
|
|
||
| from app.api.utils.models_mixins import SoftDeleteMixin, Base | ||
| from app.api.utils.models_mixins import HistoryMixin, SoftDeleteMixin, Base, AuditMixin | ||
| from app.api.users.minespace.models.minespace_user_mine import MinespaceUserMine | ||
|
|
||
|
|
||
| class MinespaceUser(SoftDeleteMixin, Base): | ||
| class MinespaceUser(HistoryMixin, SoftDeleteMixin, AuditMixin, Base): | ||
| __tablename__ = 'minespace_user' | ||
| __versioned__ = { | ||
| 'exclude': ['last_logged_in'] | ||
| } | ||
|
|
||
| user_id = db.Column(db.Integer, primary_key=True, server_default=FetchedValue()) | ||
| keycloak_guid = db.Column(UUID(as_uuid=True)) | ||
| email_or_username = db.Column(db.String(100), nullable=False) | ||
| bceid_username = db.Column(db.String(), nullable=False) | ||
|
|
||
| # new data fields must all be nullable for legacy data | ||
| sub = db.Column(db.String()) | ||
| email = db.Column(db.String()) | ||
| given_name = db.Column(db.String()) | ||
| family_name = db.Column(db.String()) | ||
| display_name = db.Column(db.String()) | ||
| identity_provider = db.Column(db.String()) | ||
| bceid_user_guid = db.Column(db.String()) | ||
| last_logged_in = db.Column(db.DateTime()) | ||
|
|
||
| minespace_user_mines = db.relationship('MinespaceUserMine', backref='user', lazy='joined') | ||
|
|
||
|
|
@@ -33,7 +42,7 @@ def find_by_id(cls, id): | |
|
|
||
| @classmethod | ||
| def find_by_guid(cls, user_guid): | ||
| return cls.query.filter_by(keycloak_guid=user_guid).filter_by(deleted_ind=False).first() | ||
| return cls.query.filter_by(bceid_user_guid=user_guid).filter_by(deleted_ind=False).first() | ||
|
|
||
| @classmethod | ||
| def find_by_mine_guid(cls, mine_guid): | ||
|
|
@@ -42,20 +51,51 @@ def find_by_mine_guid(cls, mine_guid): | |
| ).all() | ||
|
|
||
| @classmethod | ||
| def find_by_email(cls, email_or_username): | ||
| return cls.query.filter_by(email_or_username=email_or_username).filter_by( | ||
| def find_by_username(cls, bceid_username): | ||
| return cls.query.filter_by(bceid_username=bceid_username).filter_by( | ||
| deleted_ind=False).first() | ||
|
|
||
| @classmethod | ||
| def create_minespace_user(cls, email_or_username, add_to_session=True): | ||
| minespace_user = cls(email_or_username=email_or_username) | ||
| def create_minespace_user(cls, bceid_username, add_to_session=True): | ||
| minespace_user = cls(bceid_username=bceid_username) | ||
| if add_to_session: | ||
| minespace_user.save(commit=False) | ||
| return minespace_user | ||
|
|
||
| @validates('email_or_username') | ||
| def validate_email(self, key, email_or_username): | ||
| if not email_or_username: | ||
| @classmethod | ||
| def find_by_token_data(cls, **kwargs): | ||
| sub = kwargs.get("sub") | ||
| bceid_username = kwargs.get("bceid_username") | ||
|
|
||
| # if there is a user that has logged in with token data- return that user | ||
| sub_user = cls.query.filter_by(sub=sub).filter_by(deleted_ind=False).first() | ||
| if sub_user: | ||
| return sub_user | ||
|
|
||
| # otherwise look for an older record | ||
| bceid_user = cls.find_by_username(bceid_username) | ||
|
|
||
| return bceid_user | ||
|
|
||
| @classmethod | ||
| def update_from_token_data(cls, **kwargs): | ||
| user = cls.find_by_token_data(**kwargs) | ||
|
|
||
| if user is None: | ||
| return | ||
|
|
||
| for key, value in kwargs.items(): | ||
| setattr(user, key, value) | ||
| user.save() | ||
|
|
||
| return user | ||
|
|
||
|
|
||
| @validates('bceid_username') | ||
| def validate_email(self, key, bceid_username): | ||
| if not bceid_username: | ||
| raise AssertionError('Identifier is not provided.') | ||
| return email_or_username | ||
| if not bceid_username.endswith('@bceid'): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added a BE error when there's a bad BCeID passed in from the FE. I didn't add validation on the FE, figuring that the form will be revamped. |
||
| raise AssertionError('BCeID username must end with "@bceid".') | ||
| return bceid_username | ||
|
|
||
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.