Skip to content

Commit

Permalink
Use Flask-Security-Too as flask-security isn't maintained anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
publicarray committed Oct 23, 2022
1 parent 4640a22 commit 43e5266
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 167 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ Synology Package Repository
3. Create the tables with `python manage.py create`
4. Populate the database with some fake packages with `python manage.py populate`
5. Add an user with `python manage.py user create -u Admin -e [email protected] -p adminadmin`
6. Grant the created user with Administrator permissions `python manage.py user add_role -u [email protected] -r admin`
7. Grant the created user with Package Administrator permissions `python manage.py user add_role -u [email protected] -r package_admin`
8. Grant the created user with Developer permissions `python manage.py user add_role -u [email protected] -r developer`
6. Grant the created user with Administrator permissions `python manage.py roles add [email protected] admin`
7. Grant the created user with Package Administrator permissions `python manage.py roles add [email protected] package_admin`
8. Grant the created user with Developer permissions `python manage.py roles add [email protected] developer`

To reset the environment, clean up with `python manage.py clean`.

Expand Down
109 changes: 42 additions & 67 deletions manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@
import shutil
import click

from flask import Flask
from flask.cli import FlaskGroup
# from flask_security.script import (
# ActivateUserCommand,
# AddRoleCommand,
# CreateRoleCommand,
# CreateUserCommand,
# DeactivateUserCommand,
# RemoveRoleCommand,
# commit,
# )
# from flask_security.utils import encrypt_password
from flask import current_app
from flask.cli import FlaskGroup, AppGroup

from spkrepo import create_app
from spkrepo.ext import db
from spkrepo.models import Architecture, Package, user_datastore
from spkrepo.utils import populate_db

cli = FlaskGroup(create_app)
# click.option("-c", "--config", dest="config", required=False)

@click.option("-c", "--config", help="config", required=False)

def date_handler(obj):
return obj.isoformat() if hasattr(obj, "isoformat") else obj
Expand All @@ -35,60 +24,46 @@ def date_handler(obj):
def pprint(obj):
print(json.dumps(obj, default=date_handler, sort_keys=True, indent=4))


# User commands
# class CreateSpkrepoUserCommand(CreateUserCommand):
# """Create a user"""

# option_list = (
# Option("-u", "--username", dest="username", default=None),
# Option("-e", "--email", dest="email", default=None),
# Option("-p", "--password", dest="password", default=None),
# Option("-a", "--active", dest="active", default=""),
# Option("-c", "--confirmed", dest="confirmed", default=""),
# )

# @commit
# def run(self, **kwargs):
# # handle confirmed
# if re.sub(r"\s", "", str(kwargs.pop("confirmed"))).lower() in [
# "",
# "y",
# "yes",
# "1",
# "active",
# ]:
# kwargs["confirmed_at"] = datetime.datetime.now()

# # sanitize active input
# ai = re.sub(r"\s", "", str(kwargs["active"]))
# kwargs["active"] = ai.lower() in ["", "y", "yes", "1", "active"]

# from flask_security.forms import ConfirmRegisterForm
# from werkzeug.datastructures import MultiDict

# form = ConfirmRegisterForm(MultiDict(kwargs), csrf_enabled=False)

# if form.validate():
# kwargs["password"] = encrypt_password(kwargs["password"])
# user_datastore.create_user(**kwargs)
# print("User created successfully.")
# kwargs["password"] = "****"
# pprint(kwargs)
# else:
# print("Error creating user")
# pprint(form.errors)


# UserCommand = Manager(usage="Perform user actions")
# UserCommand.add_command("create", CreateSpkrepoUserCommand)
# UserCommand.add_command("activate", ActivateUserCommand)
# UserCommand.add_command("deactivate", DeactivateUserCommand)
# UserCommand.add_command("create_role", CreateRoleCommand)
# UserCommand.add_command("remove_role", RemoveRoleCommand)
# UserCommand.add_command("add_role", AddRoleCommand)
# click.add_command("user", UserCommand)

user_cli = AppGroup('user', help="Perform user actions")
@user_cli.command('create')
@click.option("-u", "--username", help="username", default=None)
@click.option("-e", "--email", help="email", default=None)
@click.option("-p", "--password", help="password", default=None)
@click.option("-a", "--active", help="active", default="")
@click.option("-c", "--confirmed", help="confirmed", default="")
def create_user(username, email, password, active, confirmed):
kwargs = {
"username": username,
"email": email,
"password": password,
"active": active,
"confirmed": confirmed
}

# handle confirmed
if re.sub(r"\s", "", str(kwargs.pop("confirmed"))).lower() in [
"",
"y",
"yes",
"1",
"active",
]:
kwargs["confirmed_at"] = datetime.datetime.now()

# sanitize active input
ai = re.sub(r"\s", "", str(kwargs["active"]))
kwargs["active"] = ai.lower() in ["", "y", "yes", "1", "active"]

from flask_security import hash_password
kwargs["password"] = hash_password(kwargs["password"])
user_datastore.create_user(**kwargs)
user_datastore.commit()
print("User created successfully.")
kwargs["password"] = "****"
pprint(kwargs)

cli.add_command(user_cli)

@cli.command()
def drop():
Expand Down
Loading

0 comments on commit 43e5266

Please sign in to comment.