mirror of
https://github.com/GSA/notifications-api.git
synced 2026-07-25 02:19:45 -04:00
try UserState enum
This commit is contained in:
@@ -3,6 +3,7 @@ from sqlalchemy.sql.expression import func
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import VersionOptions, autocommit, version_class
|
||||
from app.enums import UserState
|
||||
from app.models import Domain, Organization, Service, User
|
||||
|
||||
|
||||
@@ -125,7 +126,7 @@ def dao_get_users_for_organization(organization_id):
|
||||
return (
|
||||
db.session.query(User)
|
||||
.join(User.organizations)
|
||||
.where(Organization.id == organization_id, User.state == "active")
|
||||
.where(Organization.id == organization_id, User.state == UserState.ACTIVE)
|
||||
.order_by(User.created_at)
|
||||
.all()
|
||||
)
|
||||
|
||||
@@ -2,6 +2,7 @@ from sqlalchemy import select
|
||||
|
||||
from app import db
|
||||
from app.dao.dao_utils import autocommit
|
||||
from app.enums import UserState
|
||||
from app.models import ServiceUser, User
|
||||
|
||||
|
||||
@@ -17,7 +18,7 @@ def dao_get_active_service_users(service_id):
|
||||
stmt = (
|
||||
select(ServiceUser)
|
||||
.join(User, User.id == ServiceUser.user_id)
|
||||
.where(User.state == "active", ServiceUser.service_id == service_id)
|
||||
.where(User.state == UserState.ACTIVE, ServiceUser.service_id == service_id)
|
||||
)
|
||||
return db.session.execute(stmt).scalars().all()
|
||||
|
||||
|
||||
@@ -22,6 +22,7 @@ from app.enums import (
|
||||
NotificationStatus,
|
||||
NotificationType,
|
||||
ServicePermissionType,
|
||||
UserState,
|
||||
)
|
||||
from app.models import (
|
||||
AnnualBilling,
|
||||
@@ -738,7 +739,9 @@ def dao_resume_service(service_id):
|
||||
|
||||
def dao_fetch_active_users_for_service(service_id):
|
||||
|
||||
stmt = select(User).where(User.services.any(id=service_id), User.state == "active")
|
||||
stmt = select(User).where(
|
||||
User.services.any(id=service_id), User.state == UserState.ACTIVE
|
||||
)
|
||||
result = db.session.execute(stmt)
|
||||
return result.scalars().all()
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from app import db
|
||||
from app.dao.dao_utils import autocommit
|
||||
from app.dao.permissions_dao import permission_dao
|
||||
from app.dao.service_user_dao import dao_get_service_users_by_user_id
|
||||
from app.enums import AuthType, PermissionType
|
||||
from app.enums import AuthType, PermissionType, UserState
|
||||
from app.errors import InvalidRequest
|
||||
from app.models import Organization, Service, User, VerifyCode
|
||||
from app.utils import escape_special_characters, get_archived_db_column_value, utc_now
|
||||
@@ -233,7 +233,7 @@ def dao_archive_user(user):
|
||||
user.password = str(uuid.uuid4())
|
||||
# Changing the current_session_id signs the user out
|
||||
user.current_session_id = "00000000-0000-0000-0000-000000000000"
|
||||
user.state = "inactive"
|
||||
user.state = UserState.INACTIVE
|
||||
|
||||
db.session.add(user)
|
||||
|
||||
@@ -243,7 +243,7 @@ def user_can_be_archived(user):
|
||||
|
||||
for service in active_services:
|
||||
other_active_users = [
|
||||
x for x in service.users if x.state == "active" and x != user
|
||||
x for x in service.users if x.state == UserState.ACTIVE and x != user
|
||||
]
|
||||
|
||||
if not other_active_users:
|
||||
|
||||
@@ -25,6 +25,12 @@ class AuthType(StrEnum):
|
||||
WEBAUTHN = "webauthn_auth"
|
||||
|
||||
|
||||
class UserState(StrEnum):
|
||||
ACTIVE = "active"
|
||||
INACTIVE = "inactive"
|
||||
PENDING = "pending"
|
||||
|
||||
|
||||
class CallbackType(StrEnum):
|
||||
DELIVERY_STATUS = "delivery_status"
|
||||
COMPLAINT = "complaint"
|
||||
|
||||
@@ -28,6 +28,7 @@ from app.enums import (
|
||||
ServicePermissionType,
|
||||
TemplateProcessType,
|
||||
TemplateType,
|
||||
UserState,
|
||||
)
|
||||
from app.hashing import check_hash, hashpw
|
||||
from app.history_meta import Versioned
|
||||
@@ -70,6 +71,7 @@ _enum_column_names = {
|
||||
PermissionType: "permission_types",
|
||||
AgreementType: "agreement_types",
|
||||
AgreementStatus: "agreement_statuses",
|
||||
UserState: "user_states",
|
||||
}
|
||||
|
||||
|
||||
@@ -135,7 +137,9 @@ class User(db.Model):
|
||||
)
|
||||
logged_in_at = db.Column(db.DateTime, nullable=True)
|
||||
failed_login_count = db.Column(db.Integer, nullable=False, default=0)
|
||||
state = db.Column(db.String, nullable=False, default="pending")
|
||||
state = enum_column(
|
||||
UserState, index=True, nullable=False, default=UserState.PENDING
|
||||
)
|
||||
platform_admin = db.Column(db.Boolean, nullable=False, default=False)
|
||||
current_session_id = db.Column(UUID(as_uuid=True), nullable=True)
|
||||
auth_type = enum_column(AuthType, index=True, nullable=False, default=AuthType.SMS)
|
||||
|
||||
@@ -32,7 +32,7 @@ from app.dao.users_dao import (
|
||||
save_user_attribute,
|
||||
use_user_code,
|
||||
)
|
||||
from app.enums import CodeType, KeyType, NotificationType, TemplateType
|
||||
from app.enums import CodeType, KeyType, NotificationType, TemplateType, UserState
|
||||
from app.errors import InvalidRequest, register_errors
|
||||
from app.models import Permission, Service
|
||||
from app.notifications.process_notifications import (
|
||||
@@ -179,10 +179,10 @@ def archive_user(user_id):
|
||||
def activate_user(user_id):
|
||||
check_suspicious_id(user_id)
|
||||
user = get_user_by_id(user_id=user_id)
|
||||
if user.state == "active":
|
||||
if user.state == UserState.ACTIVE:
|
||||
raise InvalidRequest("User already active", status_code=400)
|
||||
|
||||
user.state = "active"
|
||||
user.state = UserState.ACTIVE
|
||||
save_model_user(user)
|
||||
return jsonify(data=user.serialize()), 200
|
||||
|
||||
@@ -191,10 +191,10 @@ def activate_user(user_id):
|
||||
def deactivate_user(user_id):
|
||||
check_suspicious_id(user_id)
|
||||
user = get_user_by_id(user_id=user_id)
|
||||
if user.state == "pending":
|
||||
if user.state == UserState.PENDING:
|
||||
raise InvalidRequest("User already inactive", status_code=400)
|
||||
|
||||
user.state = "pending"
|
||||
user.state = UserState.PENDING
|
||||
save_model_user(user)
|
||||
return jsonify(data=user.serialize()), 200
|
||||
|
||||
|
||||
Reference in New Issue
Block a user