Example #1
0
    def is_admin(user_name=None, customer_name=None):

        if not user_name:
            return False

        try:

            user = Hierarchy.get_user(user_name)
            if not user:
                return False

            if not customer_name:
                customer_name = user.current_customer

            groups = actions.get_groups_of_user(
                user_name=user_name,
                customer_name=customer_name
            )

            admin_perm = False
            for group in groups:

                if Permission.Admin in group[GroupKey.Permissions]:
                    admin_perm = True
                    break

            if (
                user_name == AdminUser
                or admin_perm
            ):
                return True

        except Exception as e:

            logger.error("Unable to verify `%s` as admin" % user_name)
            logger.exception(e)

            return False
Example #2
0
    def get_groups_of_user(user_name=None, customer_name=None):
        """Gets the groups of a user.

        Args:

            name: Name of the groups wanted.

        Returns:

            A list of Group instances if found, empty list otherwise.
        """

        if(
            not customer_name
            and not user_name
        ):
            return []

        g = []
        try:

#            if Hierarchy.is_admin(user_name):
#
#                print 'getting all groups'
#
#                groups = Hierarchy.get_groups_of_customer(
#                    customer_name
#                )
#
#                g = groups
#
#            else:

                groups = actions.get_groups_of_user(
                    user_name=user_name,
                    customer_name=customer_name
                )

                for group in groups:
                    try:

                        tmp = Group(
                            group[GroupKey.GroupName],
                            group[GroupKey.CustomerId],
                            group[GroupKey.Permissions],
                            group[GroupKey.Id]
                        )
                        g.append(tmp)

                    except Exception as e:

                        logger.error('Skipping group %s.' % group)
                        logger.exception(e)

        except Exception as e:

            logger.error(
                'Could not get groups of user `%s`.'
                % user_name
            )
            logger.exception(e)

        return g