def change_pw(n_clicks, current, new): if not n_clicks or not current or not new: raise PreventUpdate if not UserService.check_password(current_user, current): return False, True user_service = UserService() user_service.changepw(current_user.username, new) return True, False
def try_log_in(n_clicks, username, password): if current_user.is_authenticated: return dbc.Toast( "You are already logged in", id="login-state-notification", header="Success", icon="success", ) if not n_clicks or not username or not password: raise PreventUpdate user = UserService().find(username, password) if user: login_user(user) return dbc.Toast( f"Welcome, {user.username}!", id="login-state-notification", header="Success", icon="success", ) return dbc.Toast( "Invalid username or password", id="login-state-notification", header="Error", dismissable=True, duration=4000, icon="danger", )
def delete_account(noti): if not noti: raise PreventUpdate time.sleep(1) UserService().delete_user(current_user) logout_user() return "/logout"
def validate_new_password(value): if not value: return False, False, None valid, feedback = UserService.validate_password(value) if feedback: feedback = "New " + feedback return valid, not valid, feedback
def validate_password(ctx, param, value): from mallennlp.services.user import UserService ok, reason = UserService.validate_password(value) if not ok: raise click.BadParameter(click.style(reason, fg="red")) return value
def update_selected_permissions(value): if not value: return None user = UserService().find(value, check_password=False) if not user: return None return user.permissions.name
def save_profile(n_clicks, fullname, nickname, role, email, phone): if not n_clicks: raise PreventUpdate current_user.fullname = fullname current_user.nickname = nickname current_user.role = role current_user.email = email current_user.phone = phone UserService().update_user(current_user) return True
def user_group(config, ctx): """ Manage dashboard users. """ from mallennlp.services.db import get_db_from_cli from mallennlp.services.user import UserService db = get_db_from_cli(config) user_service = UserService(db=db) ctx.obj = user_service
def delete_user_account(n_clicks, username): if not n_clicks or not username: raise PreventUpdate UserService().delete_by_username(username) return ( dbc.Toast( "User's account successfully deleted", header="Account deleted", icon="danger", duration=4000, id="settings-user-account-deleted-noti", is_open=True, dismissable=True, ), [ {"label": u, "value": u} for u in UserService().iter_usernames() if u != current_user.username ], None, )
def update_user_permissions(n_clicks, username, value): if not n_clicks or not username or not value: raise PreventUpdate permissions = getattr(Permissions, value) if UserService().set_permissions(username, permissions): return dbc.Toast( f"Successfully updated user permissions to {value.replace('_', ' + ')}.", header="Success", icon="success", duration=4000, id="settings-update-user-permissions-success", is_open=True, dismissable=True, ) return dbc.Toast( "Failed to update user permissions", header="Failed", icon="danger", duration=4000, id="settings-update-user-permissions-fail", is_open=True, dismissable=True, )
def init_project(verb: str, name: str, path: Path, username: str, password: str, **kwargs): from mallennlp.domain.config import ProjectConfig, ServerConfig from mallennlp.domain.user import Permissions from mallennlp.services.db import init_db, get_db_from_cli from mallennlp.services.experiment import ExperimentService from mallennlp.services.config import Config from mallennlp.services.user import UserService if (path / Config.CONFIG_PATH).exists(): raise click.ClickException( click.style("project already exists", fg="red")) # Initialize config. project_options = { k: v for k, v in kwargs.items() if not k.startswith("server_") and v is not None } server_options = { k[7:]: v for k, v in kwargs.items() if k.startswith("server_") and v is not None } config = Config( ProjectConfig(path, name=name, **project_options), ServerConfig(path, **server_options), ) # Ensure the server's instance path exists. os.makedirs(config.server.instance_path) # Initialize the database file. init_db(config) # Add the user to the database. db = get_db_from_cli(config) user_service = UserService(db=db) user_service.create(username, password, permissions=Permissions.ADMIN) # Find existing experiments and add to database (does nothing if new project). experiment_entries = [ s.get_db_fields() for s in ExperimentService.find_experiments() ] if experiment_entries: click.echo( f"Found {click.style(str(len(experiment_entries)), fg='green')} existing experiments" ) ExperimentService.init_db_table(db=db, entries=experiment_entries) # Save the config to the 'Project.toml' file in the project directory. config.to_toml(path) click.echo( f"{verb} project named {click.style(name, fg='green', bold=True)}") click.echo( f" --> To edit the project's config, run {click.style('mallennlp edit', fg='yellow')} " f"from within the project " f"directory or edit the {click.style('Project.toml', fg='green')} file directly." ) click.echo( f" --> To serve the dashboard, run {click.style('mallennlp serve', fg='yellow')} " f"from within the project directory.") click.echo( f" --> You can log in to the dashboard with the username {click.style(username, bold=True, fg='green')}" )
def load_user(userid: str): return UserService().get(userid)
def service(db): return UserService(db)
def user_service(db): return UserService(db)
def change_username(n_clicks, value): if not n_clicks: raise PreventUpdate UserService().change_username(current_user, value) return True
def new_username_feedback(value): if not value or value == current_user.username: return False, False, None valid = UserService().find(value, check_password=False) is None feedback = "Username already exists" if not valid else None return valid, not valid, feedback
def get_users_elements(self): return [ dbc.Form( [ dbc.FormGroup( [ dbc.Label("Select a user to manage their account"), dcc.Dropdown( id="settings-select-user", options=[ {"label": u, "value": u} for u in UserService().iter_usernames() if u != current_user.username ], ), ] ) ] ), html.Br(), html.H5("Set permissions"), html.Hr(), dbc.Label("Permissions"), dbc.Form( [ dbc.FormGroup( [ dcc.Dropdown( id="settings-set-user-permissions", options=[ { "label": p.name.replace("_", " + "), "value": p.name, } for p in Permissions ], disabled=True, ), dbc.Button( "Save", n_clicks=0, id="settings-save-user-permissions", disabled=True, color="primary", ), ], className="mr-3", ) ], inline=True, ), html.Br(), html.Br(), html.H5("Delete account"), html.Hr(), html.P("Permanently delete their account."), dbc.Button( "Delete account", n_clicks=0, id="settings-delete-user-account", color="danger", disabled=True, ), dcc.ConfirmDialog( id="settings-confirm-delete-user-account", message="Are you sure you want to delete their account? " "This cannot be undone.", ), ]