diff --git a/.env b/.env index 92f7904b..be7405d3 100644 --- a/.env +++ b/.env @@ -4,7 +4,7 @@ ENVIRONMENT=dev DB_HOST=localhost DB_USER=postgres DB_PORT=5432 -DB_PASSWORD=quick +DB_PASSWORD=admin DB_NAME=QuickGpt SUPER_ADMIN_EMAIL=superadmin@email.com @@ -22,4 +22,4 @@ SMTP_USERNAME=shresthasaurab030 SMTP_PASSWORD=huurxwxeorxjorzw LDAP_SERVER=ldap://192.168.101.111 -LDAP_ENABLE=False \ No newline at end of file +LDAP_ENABLE=True \ No newline at end of file diff --git a/alembic/versions/8c4bd1aaf45a_create_model.py b/alembic/versions/8c4bd1aaf45a_create_model.py new file mode 100644 index 00000000..d467b1f9 --- /dev/null +++ b/alembic/versions/8c4bd1aaf45a_create_model.py @@ -0,0 +1,118 @@ +"""Create model + +Revision ID: 8c4bd1aaf45a +Revises: +Create Date: 2024-02-22 13:19:29.947241 + +""" +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = '8c4bd1aaf45a' +down_revision: Union[str, None] = None +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('companies', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_companies_id'), 'companies', ['id'], unique=False) + op.create_index(op.f('ix_companies_name'), 'companies', ['name'], unique=True) + op.create_table('roles', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=100), nullable=True), + sa.Column('description', sa.Text(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_roles_id'), 'roles', ['id'], unique=False) + op.create_index(op.f('ix_roles_name'), 'roles', ['name'], unique=False) + op.create_table('departments', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(), nullable=True), + sa.Column('company_id', sa.Integer(), nullable=True), + sa.Column('total_users', sa.Integer(), nullable=True), + sa.Column('total_documents', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_departments_id'), 'departments', ['id'], unique=False) + op.create_index(op.f('ix_departments_name'), 'departments', ['name'], unique=True) + op.create_table('subscriptions', + sa.Column('sub_id', sa.Integer(), nullable=False), + sa.Column('company_id', sa.Integer(), nullable=True), + sa.Column('start_date', sa.DateTime(), nullable=True), + sa.Column('end_date', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], ), + sa.PrimaryKeyConstraint('sub_id') + ) + op.create_index(op.f('ix_subscriptions_sub_id'), 'subscriptions', ['sub_id'], unique=False) + op.create_table('users', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('email', sa.String(length=225), nullable=False), + sa.Column('hashed_password', sa.String(), nullable=False), + sa.Column('fullname', sa.String(length=225), nullable=False), + sa.Column('is_active', sa.Boolean(), nullable=True), + sa.Column('last_login', sa.DateTime(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=True), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('company_id', sa.Integer(), nullable=True), + sa.Column('department_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], ), + sa.ForeignKeyConstraint(['department_id'], ['departments.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email'), + sa.UniqueConstraint('fullname'), + sa.UniqueConstraint('fullname', name='unique_username_no_spacing') + ) + op.create_table('document', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('filename', sa.String(length=225), nullable=False), + sa.Column('uploaded_by', sa.Integer(), nullable=False), + sa.Column('uploaded_at', sa.DateTime(), nullable=False), + sa.Column('department_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['department_id'], ['departments.id'], ), + sa.ForeignKeyConstraint(['uploaded_by'], ['users.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('filename') + ) + op.create_index(op.f('ix_document_id'), 'document', ['id'], unique=False) + op.create_table('user_roles', + sa.Column('user_id', sa.Integer(), nullable=False), + sa.Column('role_id', sa.Integer(), nullable=False), + sa.Column('company_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['company_id'], ['companies.id'], ), + sa.ForeignKeyConstraint(['role_id'], ['roles.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('user_id', 'role_id', 'company_id'), + sa.UniqueConstraint('user_id', 'role_id', 'company_id', name='unique_user_role') + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('user_roles') + op.drop_index(op.f('ix_document_id'), table_name='document') + op.drop_table('document') + op.drop_table('users') + op.drop_index(op.f('ix_subscriptions_sub_id'), table_name='subscriptions') + op.drop_table('subscriptions') + op.drop_index(op.f('ix_departments_name'), table_name='departments') + op.drop_index(op.f('ix_departments_id'), table_name='departments') + op.drop_table('departments') + op.drop_index(op.f('ix_roles_name'), table_name='roles') + op.drop_index(op.f('ix_roles_id'), table_name='roles') + op.drop_table('roles') + op.drop_index(op.f('ix_companies_name'), table_name='companies') + op.drop_index(op.f('ix_companies_id'), table_name='companies') + op.drop_table('companies') + # ### end Alembic commands ### diff --git a/private_gpt/users/api/v1/routers/auth.py b/private_gpt/users/api/v1/routers/auth.py index 861b4a2c..326ffa99 100644 --- a/private_gpt/users/api/v1/routers/auth.py +++ b/private_gpt/users/api/v1/routers/auth.py @@ -19,7 +19,7 @@ logger = logging.getLogger(__name__) LDAP_SERVER = settings.LDAP_SERVER # LDAP_ENABLE = settings.LDAP_ENABLE -LDAP_ENABLE = False +LDAP_ENABLE = True router = APIRouter(prefix="/auth", tags=["auth"]) diff --git a/private_gpt/users/api/v1/routers/users.py b/private_gpt/users/api/v1/routers/users.py index ad30f5da..5249fe0b 100644 --- a/private_gpt/users/api/v1/routers/users.py +++ b/private_gpt/users/api/v1/routers/users.py @@ -339,9 +339,8 @@ def admin_update_user( ) role = crud.user_role.update(db, db_obj=user_role, obj_in=role_in) - user_in = schemas.UserUpdate(fullname=user_update.fullname, - email=existing_user.email, company_id=existing_user.user_role.company_id, department_id=user_update.department_id) - user = crud.user.update(db, db_obj=existing_user, obj_in=user_in) + user_in = schemas.UserAdmin(fullname=user_update.fullname, department_id=user_update.department_id) + crud.user.update(db, db_obj=existing_user, obj_in=user_in) return JSONResponse( status_code=status.HTTP_200_OK, diff --git a/private_gpt/users/core/config.py b/private_gpt/users/core/config.py index 34d668cc..4cc02b24 100644 --- a/private_gpt/users/core/config.py +++ b/private_gpt/users/core/config.py @@ -8,7 +8,7 @@ SQLALCHEMY_DATABASE_URI = "postgresql+psycopg2://{username}:{password}@{host}:{p port='5432', db_name='QuickGpt', username='postgres', - password="quick", + password="admin", ) class Settings(BaseSettings): diff --git a/private_gpt/users/schemas/__init__.py b/private_gpt/users/schemas/__init__.py index dac90b6c..4493df33 100644 --- a/private_gpt/users/schemas/__init__.py +++ b/private_gpt/users/schemas/__init__.py @@ -1,6 +1,6 @@ from .role import Role, RoleCreate, RoleInDB, RoleUpdate from .token import TokenSchema, TokenPayload -from .user import User, UserCreate, UserInDB, UserUpdate, UserBaseSchema, Profile, UsernameUpdate, DeleteUser, UserAdminUpdate +from .user import User, UserCreate, UserInDB, UserUpdate, UserBaseSchema, Profile, UsernameUpdate, DeleteUser, UserAdminUpdate, UserAdmin from .user_role import UserRole, UserRoleCreate, UserRoleInDB, UserRoleUpdate from .subscription import Subscription, SubscriptionBase, SubscriptionCreate, SubscriptionUpdate from .company import Company, CompanyBase, CompanyCreate, CompanyUpdate diff --git a/private_gpt/users/schemas/user.py b/private_gpt/users/schemas/user.py index bea86244..9544d3b2 100644 --- a/private_gpt/users/schemas/user.py +++ b/private_gpt/users/schemas/user.py @@ -29,6 +29,7 @@ class UserUpdate(BaseModel): last_login: Optional[datetime] = None + class UserLoginSchema(BaseModel): email: EmailStr = Field(alias="email") password: str @@ -71,3 +72,7 @@ class UserAdminUpdate(BaseModel): fullname: str role: str department_id: int + +class UserAdmin(BaseModel): + fullname: str + department_id: int \ No newline at end of file diff --git a/private_gpt/users/utils/ad_auth.py b/private_gpt/users/utils/ad_auth.py index c71f73de..1c7717a6 100644 --- a/private_gpt/users/utils/ad_auth.py +++ b/private_gpt/users/utils/ad_auth.py @@ -7,6 +7,7 @@ class Ldap: self.server = ldap3.Server(server_uri, get_info=ldap3.ALL) print(f"Connected to ldap server: {self.server}") self.conn = ldap3.Connection(self.server, user=ldap_user, password=ldap_pass, auto_bind=True) + print(self.conn) def who_am_i(self): return self.conn.extend.standard.who_am_i()