Skip to content

User

User(name, groups, friendly_name=None, password=None, user_type=None, enabled=None)

Bases: TM1Object

Abstraction of a TM1 User

Source code in TM1py/Objects/User.py
def __init__(
    self,
    name: str,
    groups: Iterable[str],
    friendly_name: Optional[str] = None,
    password: Optional[str] = None,
    user_type: Union[UserType, str] = None,
    enabled: bool = None,
):
    self._name = name
    self._groups = CaseAndSpaceInsensitiveSet(*groups)
    self._friendly_name = friendly_name
    self._password = password
    self._enabled = enabled
    self._user_type = user_type
    # determine user_type
    if user_type is None:
        if str(UserType.Admin) in self._groups:
            self.user_type = UserType.Admin
        elif str(UserType.SecurityAdmin) in self._groups:
            self.user_type = UserType.SecurityAdmin
        elif str(UserType.DataAdmin) in self._groups:
            self.user_type = UserType.DataAdmin
        elif str(UserType.OperationsAdmin) in self._groups:
            self.user_type = UserType.OperationsAdmin
        else:
            self.user_type = UserType.User
    else:
        self.user_type = user_type

body property

enabled property writable

friendly_name property writable

groups property

is_admin property

is_data_admin property

is_ops_admin property

is_security_admin property

name property writable

password property writable

user_type property writable

add_group(group_name)

Source code in TM1py/Objects/User.py
def add_group(self, group_name: str):
    self._groups.add(group_name)

construct_body()

construct body (json) from the class attributes

Returns:

Type Description
str

String, TM1 JSON representation of a user

Source code in TM1py/Objects/User.py
def construct_body(self) -> str:
    """
    construct body (json) from the class attributes
    :return: String, TM1 JSON representation of a user
    """
    body_as_dict = collections.OrderedDict()
    body_as_dict["Name"] = self.name
    body_as_dict["FriendlyName"] = self.friendly_name or self.name
    body_as_dict["Enabled"] = self._enabled
    body_as_dict["Type"] = str(self._user_type)
    if self.password:
        body_as_dict["Password"] = self._password
    body_as_dict["Groups@odata.bind"] = [format_url("Groups('{}')", group) for group in self.groups]
    return json.dumps(body_as_dict, ensure_ascii=False)

from_dict(user_as_dict) classmethod

Alternative constructor

Parameters:

Name Type Description Default
user_as_dict Dict

user as dict

required

Returns:

Type Description
User

user, an instance of this class

Source code in TM1py/Objects/User.py
@classmethod
def from_dict(cls, user_as_dict: Dict) -> "User":
    """Alternative constructor

    :param user_as_dict: user as dict
    :return: user, an instance of this class
    """
    return cls(
        name=user_as_dict["Name"],
        friendly_name=user_as_dict["FriendlyName"],
        enabled=user_as_dict.get("Enabled", None),
        user_type=user_as_dict["Type"],
        groups=[group["Name"] for group in user_as_dict["Groups"]],
    )

from_json(user_as_json) classmethod

Alternative constructor

Parameters:

Name Type Description Default
user_as_json str

user as JSON string

required

Returns:

Type Description

user, an instance of this class

Source code in TM1py/Objects/User.py
@classmethod
def from_json(cls, user_as_json: str):
    """Alternative constructor

    :param user_as_json: user as JSON string
    :return: user, an instance of this class
    """
    user_as_dict = json.loads(user_as_json)
    return cls.from_dict(user_as_dict)

remove_group(group_name)

Source code in TM1py/Objects/User.py
def remove_group(self, group_name: str):
    self._groups.discard(group_name)

UserType

Bases: Enum

Admin = 3 class-attribute instance-attribute

DataAdmin = 2 class-attribute instance-attribute

OperationsAdmin = 4 class-attribute instance-attribute

SecurityAdmin = 1 class-attribute instance-attribute

User = 0 class-attribute instance-attribute

__str__()

Source code in TM1py/Objects/User.py
def __str__(self):
    return self.name