UserAttr.get - Python API

Returns data and metadata of an existing user attribute, by default decrypting its value on the fly.

There are two versions of the call - .get is used with a single attribute and .delete_many is used with multiple attributes. Performance-wise, it is more efficient to use .get_many if more than one attribute should be returned.

The object returned is of zato.sso.attr.AttrEntity type, with the properties as below:

NameDescription
nameAttribute's name
valueAttribute's value
creation_timeWhen was the attribute created
last_modifiedWhen was the attribute last updated
expiration_timeWhen will the attribute expire
is_encryptedIs the attribute's value stored encrypted in the database (if True) or in clear-text (if False). Note that it does indicate if the attribute's value is returned encrypted or decrypted by current function call, instead it pertains to what is kept in the database.

get

user.attr.get(self, name, decrypt=True, serialize_dt=False)

  • name: Name of the attribute to return
  • decrypt: If the attribute's value is stored encrypted in the database, should it be decrypted before it's returned
  • serialize_dt: Should date and time be returned as string (if True) or as Python datetime objects (if False)
  • Returns: A zato.sso.attr.AttrEntity object
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class UserAttrGet(Service):
    def handle(self):

        # Current user's data
        username = 'admin1'
        password = 'abxqDJpXMVXYEO8NOGx9nVZvv4xSew9'
        current_app = 'CRM'
        remote_addr = '127.0.0.1'
        user_agent = 'Firefox 139.0'

        # Log in current user
        session = self.sso.user.login(
            self.cid, username, password, current_app, remote_addr, user_agent)

        # Get UST
        ust = session.ust

        # Get user object
        user = self.sso.user.get_user_by_id(
            self.cid, session.user_id, ust, current_app, remote_addr)

        # Information about the attribute to be created
        name = 'my-attribute'
        value = 'my-value'

        # Create a new attribute
        user.attr.create(name, value)

        # Get the attribute
        attr = user.attr.get(name)

        self.logger.info(attr.name)
        self.logger.info(attr.value)
        self.logger.info(attr.creation_time)
        self.logger.info(attr.last_modified)
        self.logger.info(attr.expiration_time)
        self.logger.info(attr.is_encrypted)
INFO - my-attribute
INFO - my-value
INFO - 2023-03-27 11:33:16
INFO - 2023-03-27 11:33:16
INFO - 9999-12-31 00:00:00
INFO - False

get_many

user.attr.get_many(self, data, decrypt=True, serialize_dt=False)

Returns all attributes from the input list. If any of input attributes does not exist, the output dictionary will return None for the corresponding key.

  • data: A list of names of attributes to return
  • Returns: A dictionary of attribute names pointing to zato.sso.attr.AttrEntity objects
# -*- coding: utf-8 -*-

# Zato
from zato.server.service import Service

class UserAttrGetMany(Service):
    def handle(self):

        # Current user's data
        username = 'admin1'
        password = 'abxqDJpXMVXYEO8NOGx9nVZvv4xSew9'
        current_app = 'CRM'
        remote_addr = '127.0.0.1'
        user_agent = 'Firefox 139.0'

        # Log in current user
        session = self.sso.user.login(
            self.cid, username, password, current_app, remote_addr, user_agent)

        # Get UST
        ust = session.ust

        # Get user object
        user = self.sso.user.get_user_by_id(
            self.cid, session.user_id, ust, current_app, remote_addr)

        # Prepare a list of dictionaries with attributes to create
        data = [
            {'name':'my-attr1-zxc-11', 'value':'11'},
            {'name':'my-attr2-zxc-22', 'value':'22'},
            {'name':'my-attr3-zxc-33', 'value':'33'},
        ]

        # Create new attributes
        user.attr.create_many(data)

        # Get all the attributes just created
        data = user.attr.get_many(data.keys())

        attrs = ['my-attr1-zxc-11', 'my-attr2-zxc-22', 'my-attr3-zxc-33']
        for name, attr_info in sorted(attrs):
            self.logger.info('Name -> %s', name)
            self.logger.info('Data -> %s', attr_info.to_dict())
INFO - Name -> my-attr1-zxc-11
INFO - Data -> {'name': 'my-attr1-zxc-11',
                'creation_time': datetime.datetime(2023, 3, 27, 12, 37, 26),
                'is_encrypted': False,
                'value': '11',
                'last_modified': datetime.datetime(2023, 3, 27, 12, 39, 18),
                'expiration_time': datetime.datetime(9999, 12, 31, 0, 0)}

INFO - Name -> my-attr2-zxc-22
INFO - Data -> {'name': 'my-attr2-zxc-22',
                'creation_time': datetime.datetime(2023, 3, 27, 12, 37, 26),
                'is_encrypted': False,
                'value': '22',
                'last_modified': datetime.datetime(2023, 3, 27, 12, 39, 18),
                'expiration_time': datetime.datetime(9999, 12, 31, 0, 0)}

INFO - Name -> my-attr3-zxc-33
INFO - Data -> {'name': 'my-attr3-zxc-33',
                'creation_time': datetime.datetime(2023, 3, 27, 12, 37, 26),
                'is_encrypted': False,
                'value': '33',
                'last_modified': datetime.datetime(2023, 3, 27, 12, 39, 18),
                'expiration_time': datetime.datetime(9999, 12, 31, 0, 0)}