Passed
Pull Request — master (#457)
by Jaspar
02:22
created

GmpV214Mixin.__init__()   A

Complexity

Conditions 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nop 4
dl 0
loc 10
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
# Copyright (C) 2020-2021 Greenbone Networks GmbH
3
#
4
# SPDX-License-Identifier: GPL-3.0-or-later
5
#
6
# This program is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
19
# pylint: disable=arguments-differ, redefined-builtin, too-many-lines
20
21
"""
22
Module for communication with gvmd in
23
`Greenbone Management Protocol version 21.04`_
24
25
.. _Greenbone Management Protocol version 21.04:
26
    https://docs.greenbone.net/API/GMP/gmp-21.04.html
27
"""
28
29
from typing import Any, List, Optional, Callable
30
31
from gvm.utils import to_comma_list, to_bool
32
from gvm.xml import XmlCommand
33
34
from gvm.connections import GvmConnection
35
from gvm.errors import RequiredArgument
36
37
from gvm.protocols.base import GvmProtocol
38
39
from . import types
40
from .types import *  # pylint: disable=unused-wildcard-import, wildcard-import
41
42
_EMPTY_POLICY_ID = '085569ce-73ed-11df-83c3-002264764cea'
43
44
PROTOCOL_VERSION = (21, 4)
45
46
47
class GmpV214Mixin(GvmProtocol):
48
    types = types
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable types does not seem to be defined.
Loading history...
49
50
    def __init__(
51
        self,
52
        connection: GvmConnection,
53
        *,
54
        transform: Optional[Callable[[str], Any]] = None,
55
    ):
56
        super().__init__(connection, transform=transform)
57
58
        # Is authenticated on gvmd
59
        self._authenticated = False
60
61 View Code Duplication
    def modify_user(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
62
        self,
63
        user_id: str = None,
64
        *,
65
        name: Optional[str] = None,
66
        comment: Optional[str] = None,
67
        password: Optional[str] = None,
68
        auth_source: Optional[UserAuthType] = None,
69
        role_ids: Optional[List[str]] = None,
70
        hosts: Optional[List[str]] = None,
71
        hosts_allow: Optional[bool] = False,
72
        ifaces: Optional[List[str]] = None,
73
        ifaces_allow: Optional[bool] = False,
74
        group_ids: Optional[List[str]] = None,
75
    ) -> Any:
76
77
        """Modifies an existing user. Most of the fields need to be supplied
78
        for changing a single field even if no change is wanted for those.
79
        Else empty values are inserted for the missing fields instead.
80
81
        Arguments:
82
            user_id: UUID of the user to be modified.
83
            name: The new name for the user.
84
            comment: Comment on the user.
85
            password: The password for the user.
86
            auth_source: Source allowed for authentication for this user.
87
            roles_id: List of roles UUIDs for the user.
88
            hosts: User access rules: List of hosts.
89
            hosts_allow: Defines how the hosts list is to be interpreted.
90
                If False (default) the list is treated as a deny list.
91
                All hosts are allowed by default except those provided by
92
                the hosts parameter. If True the list is treated as a
93
                allow list. All hosts are denied by default except those
94
                provided by the hosts parameter.
95
            ifaces: User access rules: List of ifaces.
96
            ifaces_allow: Defines how the ifaces list is to be interpreted.
97
                If False (default) the list is treated as a deny list.
98
                All ifaces are allowed by default except those provided by
99
                the ifaces parameter. If True the list is treated as a
100
                allow list. All ifaces are denied by default except those
101
                provided by the ifaces parameter.
102
            group_ids: List of group UUIDs for the user.
103
104
        Returns:
105
            The response. See :py:meth:`send_command` for details.
106
        """
107
        if not user_id:
108
            raise RequiredArgument(
109
                function=self.modify_user.__name__, argument='user_id'
110
            )
111
112
        cmd = XmlCommand("modify_user")
113
114
        if user_id:
115
            cmd.set_attribute("user_id", user_id)
116
117
        if name:
118
            cmd.add_element("new_name", name)
119
120
        if role_ids:
121
            for role in role_ids:
122
                cmd.add_element("role", attrs={"id": role})
123
124
        if hosts:
125
            cmd.add_element(
126
                "hosts",
127
                to_comma_list(hosts),
128
                attrs={"allow": to_bool(hosts_allow)},
129
            )
130
131
        if ifaces:
132
            cmd.add_element(
133
                "ifaces",
134
                to_comma_list(ifaces),
135
                attrs={"allow": to_bool(ifaces_allow)},
136
            )
137
138
        if comment:
139
            cmd.add_element("comment", comment)
140
141
        if password:
142
            cmd.add_element("password", password)
143
144
        if auth_source:
145
            _xmlauthsrc = cmd.add_element("sources")
146
            _xmlauthsrc.add_element("source", auth_source.value)
147
148
        if group_ids:
149
            _xmlgroups = cmd.add_element("groups")
150
            for group_id in group_ids:
151
                _xmlgroups.add_element("group", attrs={"id": group_id})
152
153
        return self._send_xml_command(cmd)
154