Code Duplication    Length = 93-97 lines in 2 locations

gvm/protocols/gmpv208/entities/users.py 1 location

@@ 208-304 (lines=97) @@
205
        cmd.set_attribute("user_id", user_id)
206
        return self._send_xml_command(cmd)
207
208
    def modify_user(
209
        self,
210
        user_id: str = None,
211
        name: str = None,
212
        *,
213
        new_name: Optional[str] = None,
214
        comment: Optional[str] = None,
215
        password: Optional[str] = None,
216
        auth_source: Optional[UserAuthType] = None,
217
        role_ids: Optional[List[str]] = None,
218
        hosts: Optional[List[str]] = None,
219
        hosts_allow: Optional[bool] = False,
220
        ifaces: Optional[List[str]] = None,
221
        ifaces_allow: Optional[bool] = False,
222
        group_ids: Optional[List[str]] = None,
223
    ) -> Any:
224
        """Modifies an existing user. Most of the fields need to be supplied
225
        for changing a single field even if no change is wanted for those.
226
        Else empty values are inserted for the missing fields instead.
227
        Arguments:
228
            user_id: UUID of the user to be modified. Overrides name element
229
                argument.
230
            name: The name of the user to be modified. Either user_id or name
231
                must be passed.
232
            new_name: The new name for the user.
233
            comment: Comment on the user.
234
            password: The password for the user.
235
            auth_source: Source allowed for authentication for this user.
236
            roles_id: List of roles UUIDs for the user.
237
            hosts: User access rules: List of hosts.
238
            hosts_allow: Defines how the hosts list is to be interpreted.
239
                If False (default) the list is treated as a deny list.
240
                All hosts are allowed by default except those provided by
241
                the hosts parameter. If True the list is treated as a
242
                allow list. All hosts are denied by default except those
243
                provided by the hosts parameter.
244
            ifaces: User access rules: List of ifaces.
245
            ifaces_allow: Defines how the ifaces list is to be interpreted.
246
                If False (default) the list is treated as a deny list.
247
                All ifaces are allowed by default except those provided by
248
                the ifaces parameter. If True the list is treated as a
249
                allow list. All ifaces are denied by default except those
250
                provided by the ifaces parameter.
251
            group_ids: List of group UUIDs for the user.
252
253
        Returns:
254
            The response. See :py:meth:`send_command` for details.
255
        """
256
        if not user_id and not name:
257
            raise RequiredArgument(
258
                function=self.modify_user.__name__, argument='user_id or name'
259
            )
260
261
        cmd = XmlCommand("modify_user")
262
263
        if user_id:
264
            cmd.set_attribute("user_id", user_id)
265
        else:
266
            cmd.add_element("name", name)
267
268
        if new_name:
269
            cmd.add_element("new_name", new_name)
270
271
        if role_ids:
272
            for role in role_ids:
273
                cmd.add_element("role", attrs={"id": role})
274
275
        if hosts:
276
            cmd.add_element(
277
                "hosts",
278
                to_comma_list(hosts),
279
                attrs={"allow": to_bool(hosts_allow)},
280
            )
281
282
        if ifaces:
283
            cmd.add_element(
284
                "ifaces",
285
                to_comma_list(ifaces),
286
                attrs={"allow": to_bool(ifaces_allow)},
287
            )
288
289
        if comment:
290
            cmd.add_element("comment", comment)
291
292
        if password:
293
            cmd.add_element("password", password)
294
295
        if auth_source:
296
            _xmlauthsrc = cmd.add_element("sources")
297
            _xmlauthsrc.add_element("source", auth_source.value)
298
299
        if group_ids:
300
            _xmlgroups = cmd.add_element("groups")
301
            for group_id in group_ids:
302
                _xmlgroups.add_element("group", attrs={"id": group_id})
303
304
        return self._send_xml_command(cmd)
305

gvm/protocols/gmpv214/entities/users.py 1 location

@@ 34-126 (lines=93) @@
31
32
33
class UsersMixin(Gmp208UsersMixin):
34
    def modify_user(
35
        self,
36
        user_id: str = None,
37
        *,
38
        name: Optional[str] = None,
39
        comment: Optional[str] = None,
40
        password: Optional[str] = None,
41
        auth_source: Optional[UserAuthType] = None,
42
        role_ids: Optional[List[str]] = None,
43
        hosts: Optional[List[str]] = None,
44
        hosts_allow: Optional[bool] = False,
45
        ifaces: Optional[List[str]] = None,
46
        ifaces_allow: Optional[bool] = False,
47
        group_ids: Optional[List[str]] = None,
48
    ) -> Any:
49
50
        """Modifies an existing user. Most of the fields need to be supplied
51
        for changing a single field even if no change is wanted for those.
52
        Else empty values are inserted for the missing fields instead.
53
54
        Arguments:
55
            user_id: UUID of the user to be modified.
56
            name: The new name for the user.
57
            comment: Comment on the user.
58
            password: The password for the user.
59
            auth_source: Source allowed for authentication for this user.
60
            roles_id: List of roles UUIDs for the user.
61
            hosts: User access rules: List of hosts.
62
            hosts_allow: Defines how the hosts list is to be interpreted.
63
                If False (default) the list is treated as a deny list.
64
                All hosts are allowed by default except those provided by
65
                the hosts parameter. If True the list is treated as a
66
                allow list. All hosts are denied by default except those
67
                provided by the hosts parameter.
68
            ifaces: User access rules: List of ifaces.
69
            ifaces_allow: Defines how the ifaces list is to be interpreted.
70
                If False (default) the list is treated as a deny list.
71
                All ifaces are allowed by default except those provided by
72
                the ifaces parameter. If True the list is treated as a
73
                allow list. All ifaces are denied by default except those
74
                provided by the ifaces parameter.
75
            group_ids: List of group UUIDs for the user.
76
77
        Returns:
78
            The response. See :py:meth:`send_command` for details.
79
        """
80
        if not user_id:
81
            raise RequiredArgument(
82
                function=self.modify_user.__name__, argument='user_id'
83
            )
84
85
        cmd = XmlCommand("modify_user")
86
87
        if user_id:
88
            cmd.set_attribute("user_id", user_id)
89
90
        if name:
91
            cmd.add_element("new_name", name)
92
93
        if role_ids:
94
            for role in role_ids:
95
                cmd.add_element("role", attrs={"id": role})
96
97
        if hosts:
98
            cmd.add_element(
99
                "hosts",
100
                to_comma_list(hosts),
101
                attrs={"allow": to_bool(hosts_allow)},
102
            )
103
104
        if ifaces:
105
            cmd.add_element(
106
                "ifaces",
107
                to_comma_list(ifaces),
108
                attrs={"allow": to_bool(ifaces_allow)},
109
            )
110
111
        if comment:
112
            cmd.add_element("comment", comment)
113
114
        if password:
115
            cmd.add_element("password", password)
116
117
        if auth_source:
118
            _xmlauthsrc = cmd.add_element("sources")
119
            _xmlauthsrc.add_element("source", auth_source.value)
120
121
        if group_ids:
122
            _xmlgroups = cmd.add_element("groups")
123
            for group_id in group_ids:
124
                _xmlgroups.add_element("group", attrs={"id": group_id})
125
126
        return self._send_xml_command(cmd)
127