-
Notifications
You must be signed in to change notification settings - Fork 3
Cognition integration provider #302
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
base: dev
Are you sure you want to change the base?
Changes from all commits
24e6e5c
f7efa5d
e425c84
2d54ed6
b246d1d
3d36c65
4b0ab79
5620cd0
fcce2c7
bb5746c
ae22650
d78ab1a
a776438
b3eae84
1b0bd86
2728ba4
c7a2b7c
5171baa
e781ccb
3893760
0f8a518
02e8655
5632966
d5c72e9
5bfeb6b
d1494c6
550332f
308df12
b4e8cd2
ecd7a8b
6406398
c904383
9e5a363
775d788
c440d53
d849ef5
fd0267d
cbb034b
a14138a
99fd438
00c3667
c4f3466
c8f5118
d998b48
3f0aa18
841a968
3fa5341
5540a1a
74c25b3
43c8a45
d33d1ad
70e3f2f
53930c4
a48e0c3
5f158f8
fec2418
1a6991e
bd53656
c6726e7
53393a1
2bd60c8
10eb229
3bdb623
950036a
3e4f540
95cb724
70a850d
6f74eae
ea94747
59a7e14
93b6a66
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
"""adds cognition group management | ||
|
||
Revision ID: 6868ac66ea92 | ||
Revises: 36f087da55b1 | ||
Create Date: 2025-06-26 12:58:16.408919 | ||
|
||
""" | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "6868ac66ea92" | ||
down_revision = "36f087da55b1" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"group", | ||
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("organization_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("name", sa.String(), nullable=True), | ||
sa.Column("description", sa.String(), nullable=True), | ||
sa.Column("created_at", sa.DateTime(), nullable=True), | ||
sa.Column("created_by", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("meta_data", sa.JSON(), nullable=True), | ||
sa.ForeignKeyConstraint(["created_by"], ["user.id"], ondelete="SET NULL"), | ||
sa.ForeignKeyConstraint( | ||
["organization_id"], ["organization.id"], ondelete="CASCADE" | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
sa.UniqueConstraint("name"), | ||
schema="cognition", | ||
) | ||
op.create_index( | ||
op.f("ix_cognition_group_created_by"), | ||
"group", | ||
["created_by"], | ||
unique=False, | ||
schema="cognition", | ||
) | ||
op.create_index( | ||
op.f("ix_cognition_group_organization_id"), | ||
"group", | ||
["organization_id"], | ||
unique=False, | ||
schema="cognition", | ||
) | ||
op.create_table( | ||
"group_member", | ||
sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False), | ||
sa.Column("group_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=True), | ||
sa.Column("created_at", sa.DateTime(), nullable=True), | ||
sa.ForeignKeyConstraint( | ||
["group_id"], ["cognition.group.id"], ondelete="CASCADE" | ||
), | ||
sa.ForeignKeyConstraint(["user_id"], ["user.id"], ondelete="CASCADE"), | ||
sa.PrimaryKeyConstraint("id"), | ||
schema="cognition", | ||
) | ||
op.create_index( | ||
op.f("ix_cognition_group_member_group_id"), | ||
"group_member", | ||
["group_id"], | ||
unique=False, | ||
schema="cognition", | ||
) | ||
op.create_index( | ||
op.f("ix_cognition_group_member_user_id"), | ||
"group_member", | ||
["user_id"], | ||
unique=False, | ||
schema="cognition", | ||
) | ||
op.add_column("user", sa.Column("oidc_identifier", sa.String(), nullable=True)) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_column("user", "oidc_identifier") | ||
op.drop_index( | ||
op.f("ix_cognition_group_member_user_id"), | ||
table_name="group_member", | ||
schema="cognition", | ||
) | ||
op.drop_index( | ||
op.f("ix_cognition_group_member_group_id"), | ||
table_name="group_member", | ||
schema="cognition", | ||
) | ||
op.drop_table("group_member", schema="cognition") | ||
op.drop_index( | ||
op.f("ix_cognition_group_organization_id"), | ||
table_name="group", | ||
schema="cognition", | ||
) | ||
op.drop_index( | ||
op.f("ix_cognition_group_created_by"), table_name="group", schema="cognition" | ||
) | ||
op.drop_table("group", schema="cognition") | ||
# ### end Alembic commands ### |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -263,3 +263,15 @@ def check_user_exists(email: str) -> bool: | |
if i["traits"]["email"].lower() == email.lower(): | ||
return True | ||
return False | ||
|
||
|
||
def get_user_from_search(email: str) -> bool: | ||
request = requests.get( | ||
f"{KRATOS_ADMIN_URL}/identities?preview_credentials_identifier_similar={quote(email)}" | ||
) | ||
if request.ok: | ||
identities = request.json() | ||
for i in identities: | ||
if i["traits"]["email"].lower() == email.lower(): | ||
return i | ||
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. typing, same applies to |
||
return None |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,16 +15,24 @@ | |
data_slice, | ||
information_source, | ||
general, | ||
attribute, | ||
embedding, | ||
) | ||
from submodules.model import daemon | ||
from fast_api.types import HuddleData, ProjectSize | ||
from controller.task_master import manager as task_master_manager | ||
from submodules.model.enums import TaskType, RecordTokenizationScope | ||
from submodules.model.business_objects import util as db_util | ||
from submodules.model.integration_objects.helper import ( | ||
REFINERY_ATTRIBUTE_ACCESS_GROUPS, | ||
REFINERY_ATTRIBUTE_ACCESS_USERS, | ||
) | ||
from submodules.s3 import controller as s3 | ||
from service.search import search | ||
from controller.auth import kratos | ||
from submodules.model.util import sql_alchemy_to_dict | ||
from controller.embedding import connector | ||
|
||
|
||
ALL_PROJECTS_WHITELIST = { | ||
"id", | ||
|
@@ -53,6 +61,77 @@ def get_all_projects(organization_id: str) -> List[Project]: | |
return project.get_all(organization_id) | ||
|
||
|
||
def get_all_projects_with_access_management(organization_id: str) -> List[Project]: | ||
return project.get_all_with_access_management(organization_id) | ||
|
||
|
||
def activate_access_management(project_id): | ||
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. typing |
||
relative_position = attribute.get_relative_position(project_id) | ||
if relative_position is None: | ||
relative_position = 1 | ||
else: | ||
relative_position += 1 | ||
filter_attributes = [ | ||
REFINERY_ATTRIBUTE_ACCESS_GROUPS, | ||
REFINERY_ATTRIBUTE_ACCESS_USERS, | ||
] | ||
attribute.create( | ||
project_id=project_id, | ||
relative_position=relative_position, | ||
name=filter_attributes[0], | ||
data_type=enums.DataTypes.PERMISSION.value, | ||
user_created=False, | ||
visibility=enums.AttributeVisibility.HIDE.value, | ||
with_commit=True, | ||
state=enums.AttributeState.AUTOMATICALLY_CREATED.value, | ||
) | ||
attribute.create( | ||
project_id=project_id, | ||
relative_position=relative_position + 1, | ||
name=filter_attributes[1], | ||
data_type=enums.DataTypes.PERMISSION.value, | ||
user_created=False, | ||
visibility=enums.AttributeVisibility.HIDE.value, | ||
with_commit=True, | ||
state=enums.AttributeState.AUTOMATICALLY_CREATED.value, | ||
) | ||
all_embeddings = embedding.get_all_embeddings_by_project_id(project_id) | ||
for embedding_item in all_embeddings: | ||
prev_filter_attributes = embedding_item.filter_attributes or [] | ||
new_filter_attributes = list(set(prev_filter_attributes + filter_attributes)) | ||
embedding_item.filter_attributes = new_filter_attributes | ||
general.commit() | ||
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. commit after the for loop? the if statement below also issues another commit |
||
if connector.update_attribute_payloads_for_neural_search( | ||
project_id, str(embedding_item.id) | ||
): | ||
embedding.update_embedding_filter_attributes( | ||
project_id, | ||
str(embedding_item.id), | ||
new_filter_attributes, | ||
with_commit=True, | ||
) | ||
|
||
|
||
def deactivate_access_management(project_id: str) -> None: | ||
record.delete_access_management_attributes(project_id) | ||
access_groups_attribute = attribute.get_by_name( | ||
project_id, REFINERY_ATTRIBUTE_ACCESS_GROUPS | ||
) | ||
access_users_attribute = attribute.get_by_name( | ||
project_id, REFINERY_ATTRIBUTE_ACCESS_USERS | ||
) | ||
if access_groups_attribute: | ||
attribute.delete(project_id, access_groups_attribute.id, with_commit=True) | ||
if access_users_attribute: | ||
attribute.delete(project_id, access_users_attribute.id, with_commit=True) | ||
|
||
|
||
def is_access_management_activated(project_id: str) -> bool: | ||
access_groups = attribute.get_by_name(project_id, REFINERY_ATTRIBUTE_ACCESS_GROUPS) | ||
access_users = attribute.get_by_name(project_id, REFINERY_ATTRIBUTE_ACCESS_USERS) | ||
return access_groups is not None and access_users is not None | ||
|
||
|
||
def get_all_projects_by_user(organization_id) -> List[Project]: | ||
projects = project.get_all_by_user_organization_id(organization_id) | ||
project_dicts = sql_alchemy_to_dict( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can the existing function
get_userid_from_mail
be used to avoid code duplication?