Skip to content

Commit

Permalink
ADD user disable toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
wabscale committed Jan 13, 2024
1 parent 491f7bd commit 099701d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
7 changes: 3 additions & 4 deletions api/anubis/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@
import copy
import gzip
import os
import enum
from datetime import datetime, timedelta

from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import scoped_session, deferred, undefer, relationship, InstrumentedAttribute
from sqlalchemy.orm import scoped_session, deferred, relationship, InstrumentedAttribute
from sqlalchemy.sql.schema import Column, ForeignKey

from anubis.constants import THEIA_DEFAULT_OPTIONS, DB_COLLATION, DB_CHARSET
from anubis.models.enum import UserSource
from anubis.models.id import default_id_length, default_id
from anubis.models.sqltypes import String, Text, DateTime, Boolean, JSON, Integer, Enum
from anubis.utils.data import human_readable_timedelta
from anubis.models.enum import UserSource


db = SQLAlchemy(session_options={"autoflush": False})
db.session: scoped_session
Expand Down Expand Up @@ -91,6 +89,7 @@ def data(self):
"release_email_enabled": self.release_email_enabled,
"created": str(self.created),
"source": self.source.name,
"disabled": self.disabled,
**get_user_permissions(self),
}

Expand Down
35 changes: 35 additions & 0 deletions api/anubis/views/super/students.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ def super_students_toggle_superuser(id: str):
return success_response({"status": f"{other.name} is no longer a superuser", "variant": "success"})


@students_.route("/toggle-disabled/<string:id>")
@require_superuser()
@json_response
def super_students_toggle_disabled(id: str):
"""
Toggle disable a user.
:param id:
:return:
"""

# Get the other user
other: User = User.query.filter(User.id == id).first()

# If the other user was not found, then stop
req_assert(other is not None, message="user does not exist")

# Assert that user being disabled is not a superuser
req_assert(not other.is_superuser, message="Cannot disable super user")

# Toggle the superuser field
other.disabled = not other.disabled

# Commit the change
db.session.commit()

# Pass back the status based on if the other is now disabled
if other.disabled:
return success_response({"status": f"{other.name} is now disabled", "variant": "warning"})

# Pass back the status based on if the other user is now no disabled
else:
return success_response({"status": f"{other.name} is now enabled", "variant": "success"})


@students_.route("/toggle-anubis_developer/<string:id>")
@require_superuser()
@json_response
Expand Down
21 changes: 20 additions & 1 deletion web/src/pages/core/super/Users.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ const toggleField = (field) => (id, {setStudents, setEdits}, enqueueSnackbar) =>
setStudents((students) => {
for (const student of students) {
if (student.id === id) {
student['is_' + field] = !student['is_' + field];
let field_label = field;
if (!(field_label in student)) {
field_label = 'is_' + field;
}
student[field_label] = !student[field_label];
}
}
return students;
Expand All @@ -67,6 +71,7 @@ const toggleField = (field) => (id, {setStudents, setEdits}, enqueueSnackbar) =>

const toggleSuperuser = toggleField('superuser');
const toggleDeveloper = toggleField('anubis_developer');
const toggleDisabled = toggleField('disabled');

const useColumns = (pageState, enqueueSnackbar) => () => ([
{
Expand Down Expand Up @@ -112,6 +117,20 @@ const useColumns = (pageState, enqueueSnackbar) => () => ([
</Tooltip>
),
},
{
field: 'disabled',
headerName: 'Disabled',
renderCell: (params) => (
<React.Fragment>
<Switch
checked={params.row.disabled}
color={'primary'}
onClick={toggleDisabled(params.row.id, pageState, enqueueSnackbar)}
/>
</React.Fragment>
),
width: 150,
},
{
field: 'is_anubis_developer',
headerName: 'Developer',
Expand Down

0 comments on commit 099701d

Please sign in to comment.