def __init__(self, connection=None, loglevel="INFO"):
        #self.connection = connection
        MetaResourceController.__init__(self, connection, loglevel)
        from porper.models.user import User
        self.user = User(self.connection, loglevel)
        from porper.models.user_group import UserGroup
        self.user_group = UserGroup(self.connection, loglevel)
        from porper.models.invited_user import InvitedUser
        self.invited_user = InvitedUser(self.connection, loglevel)
        from porper.models.customer import Customer
        self.customer = Customer(self.connection, loglevel)
        from porper.models.access_token import AccessToken
        self.access_token = AccessToken(self.connection, loglevel)
        from porper.models.function import Function
        self.function = Function(self.connection, loglevel)
        # from porper.models.group import Group
        # self.group = Group(connection)
        #from porper.controllers.token_controller import TokenController
        #self.token_controller = TokenController(self.connection)
        #from porper.controllers.permission_controller import PermissionController
        #self.permission_controller = PermissionController(self.connection)
        # from porper.controllers.user_group_controller import UserGroupController
        # self.user_group_controller = UserGroupController(self.connection)
        # from porper.controllers.role_controller import RoleController
        # self.role_controller = RoleController(self.connection)

        self.permission_name = "user"
Beispiel #2
0
def find_token(group_id):
    from porper.models.user_group import UserGroup
    from porper.models.access_token import AccessToken
    user_group_table = UserGroup(dynamodb)
    access_token_table = AccessToken(dynamodb)
    user_group_objs = user_group_table.find({"group_id": group_id})
    print(user_group_objs)
    for user_group_obj in user_group_objs:
        access_token_objs = access_token_table.find({'user_id': user_group_obj['user_id']})
        #print(access_token_objs)
        if access_token_objs:
            break
    return access_token_objs[0]['access_token']
Beispiel #3
0
    def __init__(self, connection=None, loglevel="INFO"):
        MetaResourceController.__init__(self, connection, loglevel)
        from porper.models.invited_user import InvitedUser
        self.invited_user = InvitedUser(self.connection, loglevel)
        from porper.models.user_group import UserGroup
        self.user_group = UserGroup(self.connection, loglevel)
        from porper.models.group import Group
        self.group = Group(self.connection, loglevel)
        # from porper.controllers.user_group_controller import UserGroupController
        # self.user_group_controller = UserGroupController(self.connection)

        self.permission_name = "invite"
    def __init__(self, connection=None, loglevel="INFO"):

        self.logger = logging.getLogger()
        # loglevel = "INFO"
        logging.basicConfig(level=logging.ERROR)
        aws_lambda_logging.setup(level=loglevel)

        if not connection:
            host = os.environ.get('MYSQL_HOST')
            username = os.environ.get('MYSQL_USER')
            password = os.environ.get('MYSQL_PASSWORD')
            database = os.environ.get('MYSQL_DATABASE')
            self.connection = pymysql.connect(
                host,
                user=username,
                passwd=password,
                db=database,
                cursorclass=pymysql.cursors.DictCursor)
            self.logger.debug("@@@@@@@@new connection created")
        else:
            self.connection = connection
        # from porper.controllers.user_controller import UserController
        # self.user_controller = UserController(self.connection)
        # from porper.controllers.invited_user_controller import InvitedUserController
        # self.invited_user_controller = InvitedUserController(self.connection)
        # from porper.controllers.token_controller import TokenController
        # self.token_controller = TokenController(self.connection)
        # from porper.models.access_token import AccessToken
        # self.access_token = AccessToken(self.connection)
        from porper.models.user import User
        self.user = User(self.connection, loglevel)
        from porper.models.user_group import UserGroup
        self.user_group = UserGroup(self.connection, loglevel)
        from porper.models.group import Group
        self.group = Group(self.connection, loglevel)
        from porper.models.invited_user import InvitedUser
        self.invited_user = InvitedUser(self.connection, loglevel)
        from porper.models.access_token import AccessToken
        self.access_token = AccessToken(self.connection, loglevel)
Beispiel #5
0
    def setup_group(self, team_id, uid):

        is_admin = False

        # create a default group if not exists
        from porper.models.group import Group
        group = Group(self.connection, loglevel)
        group_info = group.find_by_id(team_id)
        if not group_info:
            # if this is the first user in this group, make it as admin
            is_admin = True
            group_info = {"id": team_id, "name": team_id}
            group.create(group_info)

        # now add this user to its default group
        from porper.models.user_group import UserGroup
        user_group = UserGroup(self.connection, loglevel)
        user_group_info = {
            "user_id": uid,
            "group_id": team_id,
            "is_admin": is_admin
        }
        self.logger.debug(ser_group_info)
        user_group.create(user_group_info)
    def __init__(self, connection=None, loglevel="INFO"):
        MetaResourceController.__init__(self, connection, loglevel)
        from porper.models.permission import Permission
        from porper.models.user_group import UserGroup
        self.permission = Permission(self.connection, loglevel)
        self.user_group = UserGroup(self.connection, loglevel)
        from porper.models.user import User
        from porper.models.group import Group
        self.user = User(self.connection, loglevel)
        self.group = Group(self.connection, loglevel)
        from porper.models.access_token import AccessToken
        self.access_token = AccessToken(self.connection, loglevel)
        # from porper.controllers.token_controller import TokenController
        # self.token_controller = TokenController(self.connection)
        # from porper.controllers.user_group_controller import UserGroupController
        # self.user_group_controller = UserGroupController(self.connection)

        from porper.models.permission import ALL
        self.PERMITTED_TO_ALL = ALL
class UserController(MetaResourceController):

    def __init__(self, connection=None, loglevel="INFO"):
        #self.connection = connection
        MetaResourceController.__init__(self, connection, loglevel)
        from porper.models.user import User
        self.user = User(self.connection, loglevel)
        from porper.models.user_group import UserGroup
        self.user_group = UserGroup(self.connection, loglevel)
        from porper.models.invited_user import InvitedUser
        self.invited_user = InvitedUser(self.connection, loglevel)
        from porper.models.customer import Customer
        self.customer = Customer(self.connection, loglevel)
        from porper.models.access_token import AccessToken
        self.access_token = AccessToken(self.connection, loglevel)
        from porper.models.function import Function
        self.function = Function(self.connection, loglevel)
        # from porper.models.group import Group
        # self.group = Group(connection)
        #from porper.controllers.token_controller import TokenController
        #self.token_controller = TokenController(self.connection)
        #from porper.controllers.permission_controller import PermissionController
        #self.permission_controller = PermissionController(self.connection)
        # from porper.controllers.user_group_controller import UserGroupController
        # self.user_group_controller = UserGroupController(self.connection)
        # from porper.controllers.role_controller import RoleController
        # self.role_controller = RoleController(self.connection)

        self.permission_name = "user"


    def create(self, access_token, params):
        """
        possible attributes in params
            - id, group_id, is_admin
            - id, email, auth_type, name, family_name and given_name
        """

        # # if this is the first user, save it as an admin
        # users = self.user.find({})
        # if len(users) == 0:
        #     # set this user to the admin
        #     self.user.create(params)
        #     from porper.models.group import Group
        #     group = Group(self.dynamodb)
        #     admin_groups = group.find_admin_groups()
        #     if not admin_groups:
        #         raise Exception("No admin group found")
        #     self.user_group.create({
        #         'user_id': params['id'],
        #         'group_id': admin_groups[0]['id']
        #     })
        #     return params['id']

        # find current user information including id and level
        self.find_user_level(access_token)
        if not self.is_permitted(self.permission_name, self.permission_write):
            raise Exception("not permitted")

        if not self.is_member(group_id=params['group_id']):
            raise Exception("not permitted")

        # add the given user to the specified group
        return self.user_group.create(
            {
                "user_id": params['id'],
                "group_id": params["group_id"]
            }
        )

        # # find if the given user already exists
        # rows = self.user.find({"email": params['email'], "auth_type": params['auth_type']})
        # if len(rows) > 0:
        #     print('already exists')
        #     return rows[0]['id']
        #
        # # find if the given user is already invited
        # invited_users = self.invited_user.find({'email':params['email'], 'auth_type':params['auth_type']})
        # if len(invited_users) == 0:
        #     raise Exception("Please invite this user first")
        #
        # # add user_group_rel first to check the permission
        # # if the current user is not admin and group admin, it will fail
        # self.user_group_controller.create(
        #     access_token,
        #     {
        #         'user_id': params['id'],
        #         'group_id': invited_users[0]['group_id'],
        #         'is_admin': invited_users[0]['is_admin']
        #     }
        # )
        #
        # # check if the current user is the admin of the invited user's group
        # #if current_user['level'] != self.USER_LEVEL_ADMIN and not self.is_group_admin(current_user['user_id'], invited_users[0]['group_id']):
        # #    raise Exception("Not permitted")
        #
        # # create a user and add it to the specified group
        # self.user.create({
        #     'id': params['id'],
        #     'email': params['email'],
        #     'auth_type': params['auth_type'],
        #     'name': params['name'],
        #     'family_name': params['family_name'],
        #     'given_name': params['given_name'],
        #     'customer_id': params['customer_id']
        # })
        # self.invited_user.update({
        #     'email':params['email'],
        #     'auth_type':params['auth_type'],
        #     'state':self.invited_user.REGISTERED
        # })
        # return params['id']


    # def delete_access_tokens(self, access_token, user_id):
    #     # Remove the access tokens of the user that is being deleted
    #     # access_token_rows = self.access_token.find({"user_id": params['id']})
    #     # print("Access Tokens found")
    #     # if len(access_token_rows) > 0:
    #     #     for acctoken in access_token_rows:
    #     #         access_token_id=acctoken.get('access_token')
    #     #         print(access_token_id)
    #     #         self.access_token.delete(access_token_id)
    #     self.find_user_level(access_token)
    #     if self.is_permitted(self.permission_name, self.permission_write)
    #         raise Exception("not permitted")
    #
    #     # find the group where the given user belongs
    #     rows = self.user_group.find_groups([user_id])
    #
    #
    #
    #
    #     return self.access_token.delete_by_user(user_id)


    # now only remove the given user from a given group
    # it does NOT remove the given user from User table!!!!
    def delete(self, access_token, params):
        """
        possible attributes in params
            - id, group_id
        """
        self.find_user_level(access_token)
        if not self.is_permitted(self.permission_name, self.permission_write):
            raise Exception("not permitted")

        if not params.get('id'):
           raise Exception("id must be provided")

        if 'group_id' not in params:
            # find the customer_id of this given user
            if not self.is_admin:
                if not self.is_customer_admin:
                    raise Exception("not permitted")
                items = self.customer.find({'user_id': id})
                if not items or not self.is_member(customer_id=items[0]['id']):
                    raise Exception("not permitted")

            # find user info before removing it
            user_info = self.user.find_by_id(params['id'])

            # remove this user from all groups
            self.user_group.delete(user_id=params['id'])

            # set this user's invite state to deleted
            self.invited_user.update_state(user_info['email'], user_info['auth_type'], self.invited_user.DELETED)

        else:
            if not self.is_admin:
                customer_id = None
                group_id = None
                if self.is_customer_admin:
                    customer_id = self.customer_id
                else:
                    group_id = params['group_id']
                if not self.is_member(customer_id=customer_id, group_id=group_id):
                    raise Exception("not permitted")
            return self.user_group.delete(user_id=params['id'], group_id=params["group_id"])

        # removeuser=params.get('removeuser')
        # if removeuser=="Y":
        #     # remove this user from all groups
        #     user_groups = self.user_group_controller.find(access_token, {'user_id': params['id']})
        #     for user_group in user_groups:
        #         self.user_group_controller.delete(access_token, user_group)
        #         # set this user's invite state to deleted
        #     user = self.user.find_by_id(params['id'])
        #     # find if the given user already exists
        #     # Remove the access tokens of the user that is being deleted
        #     access_token_rows = self.access_token.find({"user_id": params['id']})
        #     print("Access Tokens found")
        #     if len(access_token_rows) > 0:
        #         for acctoken in access_token_rows:
        #             access_token_id=acctoken.get('access_token')
        #             print(access_token_id)
        #             self.access_token.delete(access_token_id)
        #
        #     #self.invited_user.delete({'email': params['email'], 'auth_type': params['auth_type']})
        #     return self.user.delete(params['id'])


        # user_groups = self.user_group_controller.find(access_token, {'group_id': params['group_id']})
        # if params['group_id'] == self.ADMIN_GROUP_ID:
        #     if len(user_groups) == 1:
        #         raise Exception("You cannot remove this user because there must be at least one user in admin group")

        ###TODO: remove all permissions assigned to this user!!!!


    """def find_buddy_ids(self, user_id):
         user_groups = self.user_group.find({'user_id': user_id})
         group_ids = [ user_group['group_id'] for user_group in user_groups ]
         user_groups = self.user_group.find_by_group_ids(group_ids)
         user_ids = [ user_group['user_id'] for user_group in user_groups ]
         return user_ids"""


    def find(self, access_token, params):
        """
        possible attributes in params
            - detail: find all groups and functions of the current user
            - group_id: find all users in this given group
            - id: find a specific user
            - ids: find specific users
            - None: No condition
            - any combination of email, auth_type, name, family_name and given_name
        """

        self.find_user_level(access_token)

        if params.get("detail"):
            return self.find_detail(access_token, params)

        # This is to get information of myself
        if params.get("id") and self.user_id == params['id']:
            params['user_id'] = params['id']
            del params['id']
            ret = self.user.find(params, user_id=self.user_id)
            if ret:
                return ret[0]

        if 'closed' in params:
            if not self.is_permitted(self.permission_name, self.permission_read):
                raise Exception("not permitted")

        ######### NOTICE
        #### When params has 'ids', no other conditions cannot be used together!!!!
        if params.get("ids"):
            if self.is_admin:
                ret = self.user.find_by_ids(self.params['ids'])
            else:
                if 'closed' in params:
                    # allow users/groups of the groups only this user belongs
                    del params['closed']
                    if self.is_customer_admin:
                        ret = self.user.find_by_ids(self.params['ids'], customer_id=self.customer_id)
                    elif not self.is_admin:
                        ret = self.user.find_by_ids(self.params['ids'], user_id=self.user_id)
                else:
                    # allow all users/groups of the customer this user belongs
                    ret = self.user.find_by_ids(self.params['ids'], customer_id=self.customer_id)

        else:
            if self.is_admin:
                ret = self.user.find(params)
            else:
                if 'closed' in params:
                    # allow users/groups of the groups only this user belongs
                    del params['closed']
                    if self.is_customer_admin:
                        ret = self.user.find(params, customer_id=self.customer_id)
                    elif not self.is_admin:
                        ret = self.user.find(params, user_id=self.user_id)
                else:
                    # allow all users/groups of the customer this user belongs
                    ret = self.user.find(params, customer_id=self.customer_id)

        user = {}
        for u in ret:
            id = u['id']
            if id not in user:
                user[id] = {
                    'id': id,
                    "email": u['email'],
                    "customer_id": u['customer_id'],
                    "family_name": u['family_name'],
                    "given_name": u['given_name'],
                    "name": u['name'],
                    "auth_type": u['auth_type'],
                }
                if u['group_id']:
                    groups =  [
                        {
                            'id': u['group_id'],
                            'name': u['group_name'],
                            'customer_id': u['customer_id'],
                            'role_id': u['role_id']
                        }
                    ]
                else:
                    groups = []
                user[id]['groups'] = groups
            else:
                if u['group_id']:
                    user[id]['groups'].append(
                        {
                            'id': u['group_id'],
                            'name': u['group_name'],
                            'customer_id': u['customer_id'],
                            'role_id': u['role_id']
                        }
                    )

        ret = list(user.values())

        if ret and 'id' in params:
            return ret[0]

        return ret


        # ######### NOTICE
        # #### When params has 'group_id', no other conditions cannot be used together!!!!
        # if params.get("group_id"):
        #     # #user_ids = self.user_group_controller.find(access_token, {'group_id': params['group_id']})
        #     # #if len(user_ids) == 0:  return []
        #     # user_groups = self.user_group_controller.find(access_token, {'group_id': params['group_id']})
        #     # user_ids = [ user_group['user_id'] for user_group in user_groups ]
        #     # if len(user_ids) == 0:  return []
        #     # return self.add_groups(self.user.find_by_ids(user_ids))

        # ######### NOTICE
        # #### When params has 'id', no other conditions cannot be used together!!!!
        # if params.get("id"):
        #     # it's me!
        #     if params['id'] == current_user['id']:
        #         return self.add_groups(self.user.find_by_id(params['id']))
        #     # if there is any group(s) where the current user and the given user belong together, return the given user info
        #     #group_ids = self.user_group_controller.find(access_token, {'user_id': params['id']})
        #     #if len(group_ids) == 0: raise Exception("not permitted")
        #     user_groups = self.user_group_controller.find(access_token, {'user_id': params['id']})
        #     if len(user_groups) == 0: raise Exception("not permitted")
        #     return self.add_groups(self.user.find_by_id(params['id']))
        #
        #
        # if not params:
        #     # in case there is no params
        #     users = self.user.find({})
        # else:
        #     # for other parameters
        #     users = self.user.find(params)
        # if current_user['level'] == self.USER_LEVEL_ADMIN:
        #     return self.add_groups(users)
        # # return only the users who are in the same group with the current user among the returned users
        # user_groups = self.user_group_controller.find(access_token, {'user_id': current_user['id']})
        # group_ids = [ user_group['group_id'] for user_group in user_groups]
        # user_groups = self.user_group_controller.find(access_token, {'group_ids': group_ids})
        # user_ids = [ user_group['user_id'] for user_group in user_groups]
        # return self.add_groups([ user for user in users if user['id'] in user_ids])


    def find_detail(self, access_token, params):

        # current_users = self.access_token.find({'access_token': access_token})
        # if not current_users:
        #     raise Exception("unauthorized")
        # user_id = current_users[0]['user_id']
        # current_user = self.user.find_by_id(user_id)
        user = self.access_token.find_user(access_token)
        if not user:
            raise Exception("not unauthorized")

        user['groups'] = self.group.find({'user_id': self.user_id})
        user['customer_id'] = user['groups'][0]['customer_id']

        function = {}
        for f in self.function.find({'user_id': self.user_id}):
            id = f['id']
            if id not in function:
                function[id] = {
                    'id': id,
                    'name': f['name'],
                    'permissions': [{'resource': f['p_resource_name'], 'action': f['p_action']}]
                }
            else:
                function[id]['permissions'].append({'resource': f['p_resource_name'], 'action': f['p_action']})
        user['functions'] = list(function.values())

        # user_groups = self.user_group_controller.find(access_token, {'user_id': user_id})
        # if user_groups:
        #     group_ids = [ user_group['group_id'] for user_group in user_groups]
        #     groups = self.group.find_by_ids(group_ids)
        #     functions = []
        #     for group in groups:
        #         if group.get('role_id'):
        #             role = self.role_controller.find(access_token, {'id': group['role_id']})
        #             functions += role['functions']
        #
        #     # remove duplicates
        #     unique_functions = []
        #     for function in functions:
        #         duplicates = [f["id"] for f in unique_functions if f["id"] == function["id"]]
        #         if duplicates:  continue
        #         unique_functions.append(function)
        # else:
        #     groups = []
        #     unique_functions = []
        #
        # current_user['groups'] = groups
        # current_user['functions'] = unique_functions
        return user
Beispiel #8
0
import json
import sys
sys.path.append("..")

import os
import boto3
region = os.environ.get('AWS_DEFAULT_REGION')
dynamodb = boto3.resource('dynamodb',region_name=region)

from porper.models.user_group import UserGroup
obj = UserGroup(dynamodb)
ret = obj.find({})
file = open('../data/migration/user_group.json', 'w')
file.write(json.dumps(ret))
file.close()



### AccessToken
from porper.models.access_token import AccessToken
obj = AccessToken(dynamodb)
#ret = obj.find({})
items = []
last_evaluated_key = None
while True:
    if last_evaluated_key:
        ret = obj.table.scan(ExclusiveStartKey=last_evaluated_key)
    else:
        ret = obj.table.scan()
    print(ret['Items'])
Beispiel #9
0
        if f in ADMIN_FUNCTION_IDS:
            r['is_admin'] = True
            break

non_admin_role_ids = [r['id'] for r in roles if r.get('is_admin') is None]

# now find non-admin groups
admin_group_ids = [
    g['id'] for g in groups
    if 'role_id' in g and g['role_id'] not in non_admin_role_ids
]
#non_admin_group_ids = [g for g in group_ids if g not in admin_group_ids]

###### Find non-admin users
from porper.models.user_group import UserGroup
user_group = UserGroup(dynamodb)
admin_user_groups = user_group.find_by_group_ids(admin_group_ids)
admin_user_ids = [ug['user_id'] for ug in admin_user_groups]
all_user_groups = user_group.find({})
all_user_ids = [ug['user_id'] for ug in all_user_groups]
non_admin_user_ids = [u for u in all_user_ids if u not in admin_user_ids]

# finally non-admin groups & users
non_admin_user_group_ids = user_group.find_by_user_ids(non_admin_user_ids)

# fill customer ids
from porper.models.group import Group
group = Group(dynamodb)
for ug_id in non_admin_user_group_ids:
    ug_id['customer_id'] = group.find_by_id(ug_id['group_id'])['customer_id']
class AuthController():
    def __init__(self, connection=None, loglevel="INFO"):

        self.logger = logging.getLogger()
        # loglevel = "INFO"
        logging.basicConfig(level=logging.ERROR)
        aws_lambda_logging.setup(level=loglevel)

        if not connection:
            host = os.environ.get('MYSQL_HOST')
            username = os.environ.get('MYSQL_USER')
            password = os.environ.get('MYSQL_PASSWORD')
            database = os.environ.get('MYSQL_DATABASE')
            self.connection = pymysql.connect(
                host,
                user=username,
                passwd=password,
                db=database,
                cursorclass=pymysql.cursors.DictCursor)
            self.logger.debug("@@@@@@@@new connection created")
        else:
            self.connection = connection
        # from porper.controllers.user_controller import UserController
        # self.user_controller = UserController(self.connection)
        # from porper.controllers.invited_user_controller import InvitedUserController
        # self.invited_user_controller = InvitedUserController(self.connection)
        # from porper.controllers.token_controller import TokenController
        # self.token_controller = TokenController(self.connection)
        # from porper.models.access_token import AccessToken
        # self.access_token = AccessToken(self.connection)
        from porper.models.user import User
        self.user = User(self.connection, loglevel)
        from porper.models.user_group import UserGroup
        self.user_group = UserGroup(self.connection, loglevel)
        from porper.models.group import Group
        self.group = Group(self.connection, loglevel)
        from porper.models.invited_user import InvitedUser
        self.invited_user = InvitedUser(self.connection, loglevel)
        from porper.models.access_token import AccessToken
        self.access_token = AccessToken(self.connection, loglevel)

    def authenticate(self, params):

        user_id = params['user_id']
        email = params.get('email')
        family_name = params.get('family_name')
        given_name = params.get('given_name')
        name = params.get('name')
        customer_id = params.get('customer_id')
        auth_type = params['auth_type']
        access_token = params['access_token']
        refresh_token = params['refresh_token']
        # print("Before invite")
        # items = self.invited_user.find({'email': params['email'], 'auth_type': params['auth_type']})
        # if items and auth_type == "sso":
        #    customer_id=items[0]['customer_id']
        #    print(customer_id)
        #if not invited_user:
        #   print("Invited user not found")
        #else:
        #   print("Printing invited user")
        #   print(invited_user)
        user = self.user.find_by_id(user_id)
        if not user:
            # create this user
            params = {'id': user_id, 'auth_type': auth_type}
            if name:
                params['name'] = name
            if family_name:
                params['family_name'] = family_name
            if given_name:
                params['given_name'] = given_name
            if customer_id:
                params['customer_id'] = customer_id
            if email:
                params['email'] = email.lower()

            # # find admin user's access_token to replace this user's access_token to create a user
            # users = self.user.find({})
            # if not users:
            #     # this is the first user, so no need to set access_token
            #     admin_access_token = None
            # else:
            #     admin_access_token = self.access_token.find_admin_token()
            # user_id = self.user_controller.create(admin_access_token, params)
            user_id = self.add_user(params)

        # now save the tokens
        # return self.token_controller.save(access_token, refresh_token, user_id)
        params = {
            "access_token": access_token,
            "refresh_token": refresh_token,
            "user_id": user_id
        }
        self.access_token.create(params)

        user_info = self.user.find_by_id(user_id)
        user_info['user_id'] = user_info['id']
        user_info['access_token'] = access_token
        user_info['groups'] = self.group.find({"user_id": user_id})
        user_info['customer_id'] = user_info['groups'][0]['customer_id']

        return user_info

    def add_user(self, params):

        # if this is the first user, save it as an admin
        users = self.user.find({})
        if len(users) == 0:
            # set this user to the admin
            self.user.create(params)
            admin_groups = self.group.find_admin_groups()
            if not admin_groups:
                raise Exception("No admin group found")
            self.user_group.create({
                'user_id': params['id'],
                'group_id': admin_groups[0]['id']
            })
            return params['id']

        # # add the given user to the specified group
        # if params.get("group_id"):
        #     return self.user_group.create(
        #         {
        #             "user_id": params['id'],
        #             "group_id": params["group_id"]
        #         }
        #     )
        #
        # # find if the given user already exists
        # rows = self.user.find({"email": params['email'], "auth_type": params['auth_type']})
        # if len(rows) > 0:
        #     print('already exists')
        #     return rows[0]['id']

        # find if the given user is already invited
        invited_users = self.invited_user.find({
            'email': params['email'],
            'auth_type': params['auth_type']
        })
        if len(invited_users) == 0:
            raise Exception("Please invite this user first")

        # add user_group_rel first to check the permission
        # if the current user is not admin and group admin, it will fail

        # check if the current user is the admin of the invited user's group
        #if current_user['level'] != self.USER_LEVEL_ADMIN and not self.is_group_admin(current_user['user_id'], invited_users[0]['group_id']):
        #    raise Exception("Not permitted")

        # create a user and add it to the specified group
        self.user.create({
            'id': params['id'],
            'email': params['email'],
            'auth_type': params['auth_type'],
            'name': params['name'],
            'family_name': params['family_name'],
            'given_name': params['given_name'],
            #'customer_id': params['customer_id']
        })
        self.user_group.create({
            'user_id': params['id'],
            'group_id': invited_users[0]['group_id']
        })
        self.invited_user.update_state(params['email'], params['auth_type'],
                                       self.invited_user.REGISTERED)

        self.commit()

        return params['id']

    # def find_groups(self, user_id):
    #     from porper.models.user_group import UserGroup
    #     user_group = UserGroup(self.connection)
    #     user_groups = user_group.find({'user_id': user_id})
    #     return user_groups

    def commit(self):
        self.connection.commit()

    def rollback(self):
        self.connection.rollback()
import uuid
import datetime

ADMIN_GROUP_ID = 'ffffffff-ffff-ffff-ffff-ffffffffffff'

#########################################################################################################
# Preparation
#########################################################################################################
### Create a first user, admin
# We'll use model classes because we don't have any access_token
# after the admin user is created, create an access_token for following tests
from porper.models.user import User
from porper.models.user_group import UserGroup
from porper.models.access_token import AccessToken
user = User(dynamodb)
user_group = UserGroup(dynamodb)
access_token = AccessToken(dynamodb)

admin_user = {
    "auth_type": "sso",
    "email": "*****@*****.**",
    "family_name": "Admin",
    "given_name": "User",
    "id": str(uuid.uuid4()),
    "name": "Admin User"
}
user_ret = user.create(admin_user)
group_ret = user_group.create({
    'user_id': user_ret['id'],
    'group_id': ADMIN_GROUP_ID,
    'is_admin': True