<commit_before>'''
users
=====

The following methods allow for interaction into the Tenable.io
:devportal:`users <users>` API endpoints.

Methods available on ``tio.users``:

.. rst-class:: hide-signature
.. autoclass:: UsersAPI

    .. automethod:: create
    .. automethod:: change_password
    .. automethod:: delete
    .. automethod:: details
    .. automethod:: edit
    .. automethod:: enabled
    .. automethod:: gen_api_keys
    .. automethod:: two_factor
    .. automethod:: enable_two_factor
    .. automethod:: verify_two_factor
    .. automethod:: impersonate
    .. automethod:: list
'''
from.base import TIOEndpoint
from tenable.errors import UnknownError, PasswordComplexityError
from tenable.utils import dict_merge

class UsersAPI(TIOEndpoint):
    def create(self, username, password, permissions,
            name=None, email=None, account_type=None):
        '''
        Create a new user.

        :devportal:`users: create <users-create>`

        Args:
            username (str): The username for the new user.
            password (str): <PASSWORD>.
            permissions (int):
                The permissions role for the user.  The permissions integer
                is derived based on the desired role of the user.  For details
                describing what permissions values mean what roles, please refer
                to the `User Roles <https://cloud.tenable.com/api#/authorization>`_
                table to see what permissions are accepted.
            name (str, optional): The human-readable name of the user.
            email (str, optional): The email address of the user.
            account_type (str, optional):
                The account type for the user.  The default is `local`.

        Returns:
            :obj:`dict`:
                The resorce record fo the new user.

        Examples:
            Create a standard user:

            >>> user = tio.users.create('<EMAIL>', '<PASSWORD>', 32)

            Create an admin user and add the email and name:

            >>> user = tio.create.users('<EMAIL>', 'password', 64,
            ...     name='<NAME>', email='<EMAIL>')

        '''
        payload = {
            'username': self._check('username', username, str),
            'password': self._check('password', password, str),
            'permissions': self._check('permissions', permissions, int),
            'type': self._check('account_type', account_type, str, default='local'),
        }

        if name:
            payload['name'] = self._check('name', name, str)
        if email:
            payload['email'] = self._check('email', email, str)

        return self._api.post('users', json=payload).json()

    def delete(self, id):
        '''
        Removes a user from Tenable.io.

        :devportal:`users: delete <users-delete>`

        Args:
            id (int): The unique identifier of the user.

        Returns:
            :obj:`None`:
                The user was successfully deleted.

        Examples:
            >>> tio.users.delete(1)
        '''
        self._api.delete('users/{}'.format(self._check('id', id, int)))

    def details(self, id):
        '''
        Retrieve the details of a user.

        :devportal:`users: details <users-details>`

        Args:
            id (int): THe unique identifier for the user.

        Returns:
            :obj:`dict`:
                The resource record for the user.

        Examples:
            >>> user = tio.users.details(1)
        '''
        return self._api.get('users/{}'.format(self._check('id', id, int))).json()

    def edit(self, id, permissions=None, name=None, email=None, enabled=None):
        '''
        Modify an existing user.

        :devportal:`users: edit <users-edit>`

        Args:
            id (int): The unique identifier for the user.
            permissions (int, optional):
                The permissions role for the user.  The permissions integer
                is derived based on the desired role of the user.  For details
                describing what permissions values mean what roles, please refer
                to the `User Roles <https://cloud.tenable.com/api#/authorization>`_
                table to see what permissions are accepted.
            name (str, optional): The human-readable name of the user.
            email (str, optional): The email address of the user.
            enabled (bool, optional): Is the user account enabled?

        Returns:
            :obj:`dict`:
                The modified user resource record.

        Examples:
            >>> user = tio.users.edit(1, name='<NAME>')
        '''
        payload = dict()

        if permissions:
            payload['permisions'] = self._check('permissions', permissions, int)
        if enabled is not None:
            payload['enabled'] = self._check('enabled', enabled, bool)
        if email:
            payload['email'] = self._check('email', email, str)
        if name:
            payload['name'] = self._check('name', name, str)

        # Merge the data that we build with the payload with the user details.
        user = self.details(self._check('id', id, int))
        payload = dict_merge({
            'permissions': user['permissions'],
            'enabled': user['enabled'],
            'email': user['email'],
            'name': user['name'],
        }, payload)<commit_msg>Spelling police! Setting the permissions during user updates would fail as a result of the misspelled 'permissions' key. This corrects the spelling and ensures the permissions are passed appropriately.<commit_after>'''
users
=====

The following methods allow for interaction into the Tenable.io
:devportal:`users <users>` API endpoints.

Methods available on ``tio.users``:

.. rst-class:: hide-signature
.. autoclass:: UsersAPI

    .. automethod:: create
    .. automethod:: change_password
    .. automethod:: delete
    .. automethod:: details
    .. automethod:: edit
    .. automethod:: enabled
    .. automethod:: gen_api_keys
    .. automethod:: two_factor
    .. automethod:: enable_two_factor
    .. automethod:: verify_two_factor
    .. automethod:: impersonate
    .. automethod:: list
'''
from.base import TIOEndpoint
from tenable.errors import UnknownError, PasswordComplexityError
from tenable.utils import dict_merge

class UsersAPI(TIOEndpoint):
    def create(self, username, password, permissions,
            name=None, email=None, account_type=None):
        '''
        Create a new user.

        :devportal:`users: create <users-create>`

        Args:
            username (str): The username for the new user.
            password (<PASSWORD>): <PASSWORD>.
            permissions (int):
                The permissions role for the user.  The permissions integer
                is derived based on the desired role of the user.  For details
                describing what permissions values mean what roles, please refer
                to the `User Roles <https://cloud.tenable.com/api#/authorization>`_
                table to see what permissions are accepted.
            name (str, optional): The human-readable name of the user.
            email (str, optional): The email address of the user.
            account_type (str, optional):
                The account type for the user.  The default is `local`.

        Returns:
            :obj:`dict`:
                The resorce record fo the new user.

        Examples:
            Create a standard user:

            >>> user = tio.users.create('<EMAIL>', '<PASSWORD>', 32)

            Create an admin user and add the email and name:

            >>> user = tio.create.users('<EMAIL>', 'password', 64,
            ...     name='<NAME>', email='<EMAIL>')

        '''
        payload = {
            'username': self._check('username', username, str),
            'password': self._check('password', password, str),
            'permissions': self._check('permissions', permissions, int),
            'type': self._check('account_type', account_type, str, default='local'),
        }

        if name:
            payload['name'] = self._check('name', name, str)
        if email:
            payload['email'] = self._check('email', email, str)

        return self._api.post('users', json=payload).json()

    def delete(self, id):
        '''
        Removes a user from Tenable.io.

        :devportal:`users: delete <users-delete>`

        Args:
            id (int): The unique identifier of the user.

        Returns:
            :obj:`None`:
                The user was successfully deleted.

        Examples:
            >>> tio.users.delete(1)
        '''
        self._api.delete('users/{}'.format(self._check('id', id, int)))

    def details(self, id):
        '''
        Retrieve the details of a user.

        :devportal:`users: details <users-details>`

        Args:
            id (int): THe unique identifier for the user.

        Returns:
            :obj:`dict`:
                The resource record for the user.

        Examples:
            >>> user = tio.users.details(1)
        '''
        return self._api.get('users/{}'.format(self._check('id', id, int))).json()

    def edit(self, id, permissions=None, name=None, email=None, enabled=None):
        '''
        Modify an existing user.

        :devportal:`users: edit <users-edit>`

        Args:
            id (int): The unique identifier for the user.
            permissions (int, optional):
                The permissions role for the user.  The permissions integer
                is derived based on the desired role of the user.  For details
                describing what permissions values mean what roles, please refer
                to the `User Roles <https://cloud.tenable.com/api#/authorization>`_
                table to see what permissions are accepted.
            name (str, optional): The human-readable name of the user.
            email (str, optional): The email address of the user.
            enabled (bool, optional): Is the user account enabled?

        Returns:
            :obj:`dict`:
                The modified user resource record.

        Examples:
            >>> user = tio.users.edit(1, name='<NAME>')
        '''
        payload = dict()

        if permissions:
            payload['permissions'] = self._check('permissions', permissions,
                                                 int)
        if enabled is not None:
            payload['enabled'] = self._check('enabled', enabled, bool)
        if email:
            payload['email'] = self._check('email', email, str)
        if name:
            payload['name'] = self._check('name', name, str)

        # Merge the data that we build with the payload with the user details.
        user = self.details(self._check('id', id, int))
        payload = dict_merge({
            'permissions': user['permissions'],
            'enabled': user['enabled'],
            'email': user['email'],
            'name': user['name'],
        }, payload)
{'fraction_non_alphanumeric': 0.11556967771221062, 'fraction_numerical': 0.0012709940989559692, 'mean_word_length': 1.9869848156182213, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 62, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4969154', 'n_tokens_mistral': 2874, 'n_tokens_neox': 2761, 'n_words': 947}