Beispiel #1
0
    def post(self, name):
        schema = GroupSchema()
        obj = schema.loads(request.get_data(as_text=True))
        if obj.errors:
            return {"errors": obj.errors}, 400

        group_name_obj = GroupNameSchemaBase().load({"name": name})
        if group_name_obj.errors:
            return {"errors": group_name_obj.errors}, 400

        if db.session.query(exists().where(Group.name == name)).scalar():
            raise Conflict("Group exists yet")

        group = Group()
        group.name = name
        group.capabilities = obj.data.get("capabilities") or []
        db.session.add(group)
        db.session.commit()

        logger.info('group created',
                    extra={
                        'group': group.name,
                        'capabilities': group.capabilities
                    })
        schema = GroupSchema()
        return schema.dump({"name": obj.data.get("name")})
Beispiel #2
0
    def post(self, name):
        """
        ---
        summary: Create a new group
        description: |
            Creates a new group.

            Requires `manage_users` capability.
        security:
            - bearerAuth: []
        tags:
            - group
        parameters:
            - in: path
              name: name
              schema:
                type: string
              description: Group name
        responses:
            200:
                description: When group was created successfully
            400:
                description: When group name or request body is invalid
            403:
                description: When user doesn't have `manage_users` capability
            409:
                description: When group exists yet
        """
        schema = GroupSchema()
        obj = schema.loads(request.get_data(as_text=True))
        if obj.errors:
            return {"errors": obj.errors}, 400

        group_name_obj = GroupNameSchemaBase().load({"name": name})
        if group_name_obj.errors:
            return {"errors": group_name_obj.errors}, 400

        if db.session.query(exists().where(Group.name == name)).scalar():
            raise Conflict("Group exists yet")

        group = Group()
        group.name = name
        group.capabilities = obj.data.get("capabilities") or []
        db.session.add(group)
        db.session.commit()

        logger.info('group created',
                    extra={
                        'group': group.name,
                        'capabilities': group.capabilities
                    })
        schema = GroupSchema()
        return schema.dump({"name": obj.data.get("name")})