Exemple #1
0
    def get_group(group_name=None, customer_name=None):
        """Gets a Group instance.

        Args:


        Returns:

            A Group instance if found, None otherwise.
        """
        if(
            not group_name
            and not customer_name
        ):
            return None

        group = actions.db_get_by_secondary(
            collection=Collection.Groups,
            values=[
                group_name,
                customer_name
            ],
            index=GroupKey.GroupNameAndCustomerId
        )

        if len(group) >= 1:
            for gp in group:
                if gp[GroupKey.CustomerId] == customer_name:
                    g = gp
                    break

            g = group[0]

        else:
            g = None

        if g:

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

            return group

        return None
Exemple #2
0
    def toggle_user_from_customer(user=None, customer=None):
        """Toggles the user for the customer.

         If the user is part of customer then it's removed. If the user is not
         part of the customer then it's added. Changes are not saved to the DB.

         Args:

            user: A User instance.

            customer: A Customer instance.

        Returns:

            True if successfully toggled, False otherwise.
        """

        result = False
        try:

            if (
                not user
                or not customer
            ):
                return result

            result = actions.db_get_by_secondary(
                collection=Collection.UsersPerCustomer,
                values=[
                    user.user_name,
                    customer.customer_name
                ],
                index=UsersPerCustomerKey.UserAndCustomerId
            )

            if len(result) >= 1:

                result = actions.db_delete_by_secondary(
                    collection=Collection.UsersPerCustomer,
                    values=[
                        user.user_name,
                        customer.customer_name
                    ],
                    index=UsersPerCustomerKey.UserAndCustomerId
                )

                ### SAFETY HACK
                ### Make sure a user has at least one customer.
                customer_count = actions.db_get_by_secondary(
                    collection=Collection.UsersPerCustomer,
                    values=user.user_name,
                    index=UsersPerCustomerKey.UserId
                )

                if len(customer_count) <= 0:
                    def_customer = Hierarchy.get_customer(DefaultCustomer)
                    res = Hierarchy.save_user_per_customer(user, def_customer)
                    if res:
                        result = True

            else:

                res = Hierarchy.save_user_per_customer(user, customer)
                if res:
                    result = True

        except Exception as e:

            logger.error(
                "Uable to toggle user `%s` from customer `%s`"
                % (user, customer)
            )
            logger.exception(e)

        return result
Exemple #3
0
    def toggle_group_from_customer(group=None, customer=None, both=False):
        """Toggles the group for the customer.

         If the group is part of customer then it's removed. If the group is
         not part of the customer then it's added. Changes are not saved
         to the DB.

         Args:

            group: A Group instance.

            customer: A Customer instance.

            both: Whether to toggle both Group and Customer instances or just
                customer.

        Returns:

            True if successfully toggled, False otherwise.
        """
        try:

            if (
                not group
                or not customer
            ):
                return result

            result = actions.db_get_by_secondary(
                collection=Collection.Groups,
                values=[
                    group.group_name,
                    customer.customer_name
                ],
                index=GroupKey.GroupNameAndCustomerId
            )

            if len(result) >= 1:

                result = actions.db_delete_by_secondary(
                    collection=Collection.Groups,
                    values=[
                        group.group_name,
                        customer.customer_name
                    ],
                    index=GroupKey.GroupNameAndCustomerId
                )

            else:

                res = Hierarchy.save_group_per_customer(group, customer)
                if res:
                    result = True

        except Exception as e:

            result = False

            logger.error(
                "Uable to toggle group `%s` from customer `%s`"
                % (group, customer)
            )
            logger.exception(e)

        return result
Exemple #4
0
    def toggle_group_of_user(group=None, user=None, customer=None):
        """Toggles the user for the group for a particular customer.

         If the user is part of group then it's removed. If the user is not
         part of the group then it's added.

         Args:

            user: A User instance.

            group: A Group instance.

            customer: A Customer instance.

        Returns:

            True if successfully toggled, False otherwise.
        """

        result = False

        try:

            if (
                not group
                or not user
                or not customer
            ):
                return result

            result = actions.db_get_by_secondary(
                collection=Collection.GroupsPerUser,
                values=[
                    group.group_name,
                    user.user_name,
                    customer.customer_name
                ],
                index=GroupsPerUserKey.GroupUserAndCustomerId
            )

            if len(result) >= 1:

                result = actions.db_delete_by_secondary(
                    collection=Collection.GroupsPerUser,
                    values=[
                        group.group_name,
                        user.user_name,
                        customer.customer_name
                    ],
                    index=GroupsPerUserKey.GroupUserAndCustomerId
                )

            else:

                res = Hierarchy.save_group_per_user(group, user, customer)
                if res:
                    result = True

            return result

        except Exception as e:

            logger.error(
                "Unable to toggle user `%s` from group `%s`."
                % (user, group)
            )
            logger.exception(e)

        return False