Esempio n. 1
0
    def create_customer(name=None):
        """Create a new Customer and save it.

        Args:

            name: Name of the customer.

        Returns:

            The newly created Customer if added successfully, None otherwise.
        """

        if not name:

            return False

        name = name.strip()

        customer = Customer(name)

        _id = _db.save_customer(customer)

        if _id == '':

            customer.id = customer.name
            return customer

        return None
Esempio n. 2
0
    def create_customer(name=None):
        """Create a new Customer and save it.

        Args:

            name: Name of the customer.

        Returns:

            The newly created Customer if added successfully, None otherwise.
        """

        if not name:

            return False

        name = name.strip()

        customer = Customer(name)

        _id = _db.save_customer(customer)

        if _id == '':

            customer.id = customer.name
            return customer

        return None
Esempio n. 3
0
    def edit_customer(name, mod_data=None):
        """Edit customer properties.

        Args:

            name: Name of the customer.

            mod_data: A dic of GroupKeys as the key with the new values.

        Returns:

            True if successful, False otherwise.
        """

        if not name and not mod_data:

            return False

        customer = Hierarchy.get_customer(name)

        net_throttle = mod_data.get(CustomerKey.NetThrottle)
        if net_throttle:

            customer.net_throttle = net_throttle

        cpu_throttle = mod_data.get(CustomerKey.CpuThrottle)
        if cpu_throttle:

            customer.cpu_throttle = cpu_throttle

        groups = mod_data.get(CustomerKey.Groups)
        if groups:

            for group in groups:

                g = Hierarchy.get_group(group)

                if g:

                    g, customer = Hierarchy.toggle_group_from_customer(
                        g, customer, both=True)

                    _db.save_group(g)

        users = mod_data.get(CustomerKey.Users)
        if users:

            for user in users:

                u = Hierarchy.get_user(user)

                if u:

                    u, customer = Hierarchy.toggle_user_from_customer(
                        u, customer, both=True)

                    _db.save_user(u)

        return _db.save_customer(customer)
Esempio n. 4
0
    def save_customer(customer=None):

        if customer:

            return _db.save_customer(customer)
Esempio n. 5
0
    def delete_user(name=None, current_customer=None):
        """Delete a User for good.

         Args:

            name: Name of the user to delete.

        Returns:

            True if user was deleted, False otherwise.
        """

        if name == 'admin':
            return False

        user = Hierarchy.get_user(name)

        if not name:

            return False

        # Build users and groups list before deleting user.
        user_groups = user.get_groups()
        found_groups = []

        for group in user_groups:

            g = Hierarchy.get_group({GroupKey.Id: group.id})

            if g:

                found_groups.append(g)

        user_customers = user.get_customers()
        found_customers = []

        for customer in user_customers:

            c = Hierarchy.get_customer(customer.name)

            if c:

                found_customers.append(c)

        deleted = _db._db_delete(collection_name=UserCollection, _id=name)

        if deleted:

            for group in found_groups:

                __, group = Hierarchy.toggle_user_from_group(user, group)

                _db.save_group(group)

            for customer in found_customers:

                __, customer = Hierarchy.toggle_user_from_customer(user,
                                                                   customer)

                _db.save_customer(customer)

        return deleted
Esempio n. 6
0
    def edit_customer(name, mod_data=None):
        """Edit customer properties.

        Args:

            name: Name of the customer.

            mod_data: A dic of GroupKeys as the key with the new values.

        Returns:

            True if successful, False otherwise.
        """

        if not name and not mod_data:

            return False

        customer = Hierarchy.get_customer(name)

        net_throttle = mod_data.get(CustomerKey.NetThrottle)
        if net_throttle:

            customer.net_throttle = net_throttle

        cpu_throttle = mod_data.get(CustomerKey.CpuThrottle)
        if cpu_throttle:

            customer.cpu_throttle = cpu_throttle

        groups = mod_data.get(CustomerKey.Groups)
        if groups:

            for group in groups:

                g = Hierarchy.get_group(group)

                if g:

                    g, customer = Hierarchy.toggle_group_from_customer(
                        g,
                        customer, both=True)

                    _db.save_group(g)

        users = mod_data.get(CustomerKey.Users)
        if users:

            for user in users:

                u = Hierarchy.get_user(user)

                if u:

                    u, customer = Hierarchy.toggle_user_from_customer(
                        u,
                        customer, both=True
                    )

                    _db.save_user(u)

        return _db.save_customer(customer)
Esempio n. 7
0
    def edit_user(user=None, mod_data=None):
        """Edit user properties.

        Args:

            user: Name of the user.

            mod_data: A dic of UserKeys as the key with the new values.

        Returns:

            True if successful, False otherwise.
        """

        if not user and not mod_data:

            return False

        user = Hierarchy.get_user(user)

        password = mod_data.get(UserKey.Password)
        if password:

            user.password = Crypto.hash_bcrypt(password)

        full_name = mod_data.get(UserKey.FullName)
        if full_name:

            user.full_name = full_name

        email = mod_data.get(UserKey.Email)
        if email:

            user.email = email

        current_customer = mod_data.get(UserKey.CurrentCustomer)
        if current_customer:

            customer = Hierarchy.get_customer(current_customer)

            if customer:

                customer_name = ''
                current_customer = user.get_current_customer()
                if current_customer:
                    customer_name = current_customer.name

                if not customer.name == customer_name:
                    user.set_current_customer(customer)

        default_customer = mod_data.get(UserKey.DefaultCustomer)
        if default_customer:

            customer = Hierarchy.get_customer(default_customer)

            if customer:

                user.set_current_customer(customer)

        customers = mod_data.get(UserKey.Customers)
        if customers:

            for customer in customers:

                c = Hierarchy.get_customer(customer)

                if c:

                    user, c = Hierarchy.toggle_user_from_customer(
                        user,
                        c,
                        both=True
                    )

                    _db.save_customer(c)

        groups = mod_data.get(UserKey.Groups)

        if groups:

            for group in groups:

                g = Hierarchy.get_group(group)

                if g:

                    user, g = Hierarchy.toggle_user_from_group(user, g,
                                                               both=True)

                    _db.save_group(g)

        if _db.save_user(user):

            return True

        return False
Esempio n. 8
0
    def create_user(name=None, full_name=None, email=None, password=None,
                    groups=None, customers=None, default_customer=None):
        """Create a new User and save it.

        All parameters are required *except* groups and customers.

        Args:

            name: Name of the user.

            full_name: Full name of the user (ie First and last name).

            email: User's email address.

            password: User's plain text password.

            groups: A list of dicts consisting of either an id key or name key
                describing the group.

            customers: Customers this user should be added to. List of customer
                names.

            default_customer: The default customer for this user. Will be the
                first data available to the user.

        Returns:

            The newly created User if added successfully, None otherwise.
        """
        if (
            not name
            or not password
        ):
            return False

        # Get the Group instances that will be added to this user.
        if groups:

            groups_list = []

            for group in groups:

                g = Hierarchy.get_group(group)

                if g:

                    groups_list.append(g)

            groups = groups_list

        else:

            groups = []

            g = Hierarchy.get_group({GroupKey.Name: 'Read Only'})

            if g:

                groups.append(g)

        # Get the Customer instances that will be added to this user.
        if customers:

            customers_list = []

            for customer in customers:

                c = Hierarchy.get_customer(customer)

                if c:

                    customers_list.append(c)

            if customers_list:
                customers = customers_list

            else:
                customers = [Hierarchy.get_customer(DefaultCustomer)]

        else:

            customers = [Hierarchy.get_customer(DefaultCustomer)]

        if default_customer:

            default_customer = Hierarchy.get_customer(default_customer)

        else:

            default_customer = customers[0]

        name = name.strip()
        full_name = full_name.strip()

        password = Crypto.hash_bcrypt(password)

        user = User(name, full_name, email, password, groups, customers,
                    default_customer=default_customer,
                    current_customer=default_customer)

        _id = _db.save_user(user)

        if _id == '':

            user.id = user.name

            for g in groups:

                _, mod_group = Hierarchy.toggle_user_from_group(user, g)

                _db.save_group(mod_group)

            for c in customers:

                _, mod_customer = Hierarchy.toggle_user_from_customer(user, c)
                _db.save_customer(mod_customer)

            return user

        return None
Esempio n. 9
0
    def save_customer(customer=None):

        if customer:

            return _db.save_customer(customer)
Esempio n. 10
0
    def delete_user(name=None, current_customer=None):
        """Delete a User for good.

         Args:

            name: Name of the user to delete.

        Returns:

            True if user was deleted, False otherwise.
        """

        if name == 'admin':
            return False

        user = Hierarchy.get_user(name)

        if not name:

            return False

        # Build users and groups list before deleting user.
        user_groups = user.get_groups()
        found_groups = []

        for group in user_groups:

            g = Hierarchy.get_group({GroupKey.Id: group.id})

            if g:

                found_groups.append(g)

        user_customers = user.get_customers()
        found_customers = []

        for customer in user_customers:

            c = Hierarchy.get_customer(customer.name)

            if c:

                found_customers.append(c)

        deleted = _db._db_delete(collection_name=UserCollection, _id=name)

        if deleted:

            for group in found_groups:

                __, group = Hierarchy.toggle_user_from_group(user, group)

                _db.save_group(group)

            for customer in found_customers:

                __, customer = Hierarchy.toggle_user_from_customer(
                    user, customer)

                _db.save_customer(customer)

        return deleted
Esempio n. 11
0
    def edit_user(user=None, mod_data=None):
        """Edit user properties.

        Args:

            user: Name of the user.

            mod_data: A dic of UserKeys as the key with the new values.

        Returns:

            True if successful, False otherwise.
        """

        if not user and not mod_data:

            return False

        user = Hierarchy.get_user(user)

        password = mod_data.get(UserKey.Password)
        if password:

            user.password = Crypto.hash_bcrypt(password)

        full_name = mod_data.get(UserKey.FullName)
        if full_name:

            user.full_name = full_name

        email = mod_data.get(UserKey.Email)
        if email:

            user.email = email

        current_customer = mod_data.get(UserKey.CurrentCustomer)
        if current_customer:

            customer = Hierarchy.get_customer(current_customer)

            if customer:

                customer_name = ''
                current_customer = user.get_current_customer()
                if current_customer:
                    customer_name = current_customer.name

                if not customer.name == customer_name:
                    user.set_current_customer(customer)

        default_customer = mod_data.get(UserKey.DefaultCustomer)
        if default_customer:

            customer = Hierarchy.get_customer(default_customer)

            if customer:

                user.set_current_customer(customer)

        customers = mod_data.get(UserKey.Customers)
        if customers:

            for customer in customers:

                c = Hierarchy.get_customer(customer)

                if c:

                    user, c = Hierarchy.toggle_user_from_customer(user,
                                                                  c,
                                                                  both=True)

                    _db.save_customer(c)

        groups = mod_data.get(UserKey.Groups)

        if groups:

            for group in groups:

                g = Hierarchy.get_group(group)

                if g:

                    user, g = Hierarchy.toggle_user_from_group(user,
                                                               g,
                                                               both=True)

                    _db.save_group(g)

        if _db.save_user(user):

            return True

        return False
Esempio n. 12
0
    def create_user(name=None,
                    full_name=None,
                    email=None,
                    password=None,
                    groups=None,
                    customers=None,
                    default_customer=None):
        """Create a new User and save it.

        All parameters are required *except* groups and customers.

        Args:

            name: Name of the user.

            full_name: Full name of the user (ie First and last name).

            email: User's email address.

            password: User's plain text password.

            groups: A list of dicts consisting of either an id key or name key
                describing the group.

            customers: Customers this user should be added to. List of customer
                names.

            default_customer: The default customer for this user. Will be the
                first data available to the user.

        Returns:

            The newly created User if added successfully, None otherwise.
        """
        if (not name or not password):
            return False

        # Get the Group instances that will be added to this user.
        if groups:

            groups_list = []

            for group in groups:

                g = Hierarchy.get_group(group)

                if g:

                    groups_list.append(g)

            groups = groups_list

        else:

            groups = []

            g = Hierarchy.get_group({GroupKey.Name: 'Read Only'})

            if g:

                groups.append(g)

        # Get the Customer instances that will be added to this user.
        if customers:

            customers_list = []

            for customer in customers:

                c = Hierarchy.get_customer(customer)

                if c:

                    customers_list.append(c)

            if customers_list:
                customers = customers_list

            else:
                customers = [Hierarchy.get_customer(DefaultCustomer)]

        else:

            customers = [Hierarchy.get_customer(DefaultCustomer)]

        if default_customer:

            default_customer = Hierarchy.get_customer(default_customer)

        else:

            default_customer = customers[0]

        name = name.strip()
        full_name = full_name.strip()

        password = Crypto.hash_bcrypt(password)

        user = User(name,
                    full_name,
                    email,
                    password,
                    groups,
                    customers,
                    default_customer=default_customer,
                    current_customer=default_customer)

        _id = _db.save_user(user)

        if _id == '':

            user.id = user.name

            for g in groups:

                _, mod_group = Hierarchy.toggle_user_from_group(user, g)

                _db.save_group(mod_group)

            for c in customers:

                _, mod_customer = Hierarchy.toggle_user_from_customer(user, c)
                _db.save_customer(mod_customer)

            return user

        return None