Ejemplo n.º 1
0
Archivo: auth.py Proyecto: obasys/hikka
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("password", type=str, required=True)
        parser.add_argument("email", type=str, required=True)
        args = parser.parse_args()

        account = UserService.get_by_email(args["email"])
        if account is None:
            return abort("account", "not-found")

        login = UserService.login(args["password"], account.password)
        if not login:
            return abort("account", "login-failed")

        UserService.update(account, login=datetime.now)
        token = Token.create("login", account.username)
        data = Token.validate(token)

        result["data"] = {
            "token": token,
            "expire": data["payload"]["expire"],
            "username": data["payload"]["meta"]
        }

        return result
Ejemplo n.º 2
0
    def decorator(*args, **kwargs):
        account = UserService.auth(request.headers.get("Authentication"))
        if account is None:
            return abort("account", "login-failed")

        request.account = account
        return view_function(*args, **kwargs)
Ejemplo n.º 3
0
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("action",
                            type=str,
                            required=True,
                            choices=("add", "remove"))
        parser.add_argument("username", type=str, required=True)
        parser.add_argument("scope", type=str, required=True)
        parser.add_argument("name", type=str, required=True)
        args = parser.parse_args()

        account = UserService.get_by_username(args["username"])
        if account is None:
            return abort("account", "not-found")

        if args["action"] == "add":
            PermissionService.add(account, args["scope"], args["name"])

        elif args["action"] == "remove":
            PermissionService.remove(account, args["scope"], args["name"])

        result["data"] = account.list_permissions()
        return result
Ejemplo n.º 4
0
Archivo: auth.py Proyecto: obasys/hikka
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("token", type=str, required=True)
        args = parser.parse_args()

        data = Token.validate(args["token"])
        if not data["valid"]:
            return abort("general", "token-invalid-type")

        account = UserService.get_by_username(data["payload"]["meta"])
        if account is None:
            return abort("account", "not-found")

        if data["payload"]["action"] != "activation":
            return abort("general", "token-invalid-type")

        activated = PermissionService.check(account, "global", "activated")
        if activated:
            return abort("account", "activated")

        PermissionService.add(account, "global", "activated")
        result["data"] = {"username": account.username, "activated": True}

        return result
Ejemplo n.º 5
0
def account(username):
    account = UserService.get_by_username(username)
    if not account:
        response = abort("account", "not-found")
        flask_abort(response)

    return account
Ejemplo n.º 6
0
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("username", type=str, required=True)
        args = parser.parse_args()

        account = UserService.get_by_username(args["username"])
        if account is None:
            return abort("account", "not-found")

        result["data"] = account.list_permissions()
        return result
Ejemplo n.º 7
0
Archivo: auth.py Proyecto: obasys/hikka
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("username", type=str, required=True)
        parser.add_argument("password", type=str, required=True)
        parser.add_argument("email", type=str, required=True)
        args = parser.parse_args()

        account = UserService.get_by_username(args["username"])
        if account is not None:
            return abort("account", "username-exist")

        account_check = UserService.get_by_email(args["email"])
        if account_check is not None:
            return abort("account", "email-exist")

        admin = len(UserService.list()) == 0
        account = UserService.signup(args["username"], args["email"],
                                     args["password"])

        # Make first registered user admin
        if admin:
            PermissionService.add(account, "global", "activated")
            PermissionService.add(account, "global", "admin")

        result["data"] = {"username": account.username}

        activation_token = Token.create("activation", account.username)

        # Display activation code only in debug mode
        if config.debug:
            result["data"]["code"] = activation_token

        mail = Email()
        mail.account_confirmation(account.email, activation_token)

        return result
Ejemplo n.º 8
0
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("members", type=list, default=[], location="json")
        parser.add_argument("admins", type=list, default=[], location="json")
        parser.add_argument("avatar", type=FileStorage, location="files")
        parser.add_argument("description", type=str, required=True)
        parser.add_argument("name", type=str, required=True)
        parser.add_argument("slug", type=str, required=True)
        args = parser.parse_args()

        team = TeamService.get_by_slug(args["slug"])
        if team is not None:
            return abort("team", "slug-exists")

        avatar = None
        if args["avatar"] is not None:
            helper = UploadHelper(request.account, args["avatar"], "avatar")
            data = helper.upload_image()

            if type(data) is Response:
                return data

            avatar = data

        team = TeamService.create(args["name"], args["slug"],
                                  args["description"])

        if avatar is not None:
            TeamService.update_avatar(team, avatar)

        for username in args["members"]:
            account = UserService.get_by_username(username)

            if account is None:
                return abort("account", "not-found")

            TeamService.add_member(team, account)
            if account.username in args["admins"]:
                PermissionService.add(account, "global", "publishing")

        result["data"] = team.dict(True)
        return result
Ejemplo n.º 9
0
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("username", type=str, required=True)
        parser.add_argument("admin", type=bool, default=False)
        parser.add_argument("slug", type=str, required=True)
        args = parser.parse_args()

        team = TeamService.get_by_slug(args["slug"])
        if team is None:
            return abort("team", "not-found")

        account = UserService.get_by_username(args["username"])
        if account is None:
            return abort("account", "not-found")

        TeamService.remove_member(team, account)
        PermissionService.remove(account, "global", "publishing")

        result["data"] = team.dict(True)
        return result
Ejemplo n.º 10
0
    def post(self):
        result = {"error": None, "data": {}}

        parser = reqparse.RequestParser()
        parser.add_argument("franchises",
                            type=list,
                            default=[],
                            location="json")
        parser.add_argument("subtitles",
                            type=list,
                            default=[],
                            location="json")
        parser.add_argument("voiceover",
                            type=list,
                            default=[],
                            location="json")
        parser.add_argument("aliases", type=list, default=[], location="json")
        parser.add_argument("genres", type=list, default=[], location="json")
        parser.add_argument("description", type=str, required=True)
        parser.add_argument("category", type=str, required=True)
        parser.add_argument("title", type=dict, required=True)
        parser.add_argument("team", type=str, required=True)
        parser.add_argument("year", type=int, required=True)
        parser.add_argument("state", type=str, default=None)
        args = parser.parse_args()

        title_parser = reqparse.RequestParser()
        title_parser.add_argument("jp",
                                  type=str,
                                  default=None,
                                  location=("title", ))
        title_parser.add_argument("ua", type=str, location=("title", ))
        title_args = title_parser.parse_args(req=args)

        for alias in args["aliases"]:
            if type(alias) is not str:
                return abort("general", "alias-invalid-type")

        team = TeamService.get_by_slug(args["team"])
        if team is None:
            return abort("team", "not-found")

        if request.account not in team.members:
            return abort("account", "not-team-member")

        if not PermissionService.check(request.account, "global",
                                       "publishing"):
            return abort("account", "permission")

        category = DescriptorService.get_by_slug("category", args["category"])
        if category is None:
            return abort("category", "not-found")

        state = DescriptorService.get_by_slug("state", args["state"])
        if state is None:
            return abort("state", "not-found")

        if args["description"] is None:
            return abort("general", "missing-field")

        genres = []
        for slug in args["genres"]:
            genre = DescriptorService.get_by_slug("genre", slug)
            if genre is not None:
                genres.append(genre)
            else:
                return abort("genre", "not-found")

        franchises = []
        for slug in args["franchises"]:
            franchise = DescriptorService.get_by_slug("franchise", slug)
            if franchise is not None:
                franchises.append(franchise)
            else:
                return abort("franchise", "not-found")

        subtitles = []
        for username in args["subtitles"]:
            subtitles_account = UserService.get_by_username(username)
            if subtitles_account is not None:
                subtitles.append(subtitles_account)

            else:
                return abort("account", "not-found")

        voiceover = []
        for username in args["voiceover"]:
            voiceover_account = UserService.get_by_username(username)
            if voiceover_account is not None:
                voiceover.append(voiceover_account)

            else:
                return abort("account", "not-found")

        title = AnimeService.get_title(title_args["ua"], title_args["jp"])
        search = utils.create_search(title_args["ua"], title_args["jp"],
                                     args["aliases"])
        slug = utils.create_slug(title_args["ua"])

        anime = AnimeService.create(title, slug, args["description"],
                                    args["year"], search, category, state,
                                    genres, franchises, [team], subtitles,
                                    voiceover, args["aliases"])

        result["data"] = anime.dict()
        return result