Debian Codegraph @

adjutantclient/osc/v1/users.py

raw
1 # Copyright (c) 2016 Catalyst IT Ltd.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License"); you may
4 # not use this file except in compliance with the License. You may obtain
5 # a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 # License for the specific language governing permissions and limitations
13 # under the License.
14
15 import logging
16
17 from osc_lib.command import command
18 from osc_lib.i18n import _
19 from osc_lib import utils
20
21 from adjutantclient import client as adjutant_client
22 from adjutantclient import exc
23
24 LOG = logging.getLogger(__name__)
25
26
27 class UserList(command.Lister):
28 """Lists users in the currently scoped project. """
29
30 def take_action(self, parsed_args):
31 client = self.app.client_manager.admin_logic
32 project_users = client.users.list()
33 headers = [
34 'id', 'name', 'email', 'roles', 'cohort', 'status']
35 optional_headers = ['has_mfa']
36
37 rows = []
38 for user in project_users:
39 # Check the first user for optional headers.
40 for opt in optional_headers:
41 if hasattr(user, opt):
42 headers.append(opt)
43 optional_headers = []
44 rows.append([
45 getattr(user, header) for header in headers
46 ])
47
48 return headers, rows
49
50
51 class UserShow(command.ShowOne):
52 """Show details of one user."""
53
54 def get_parser(self, prog_name):
55 parser = super(UserShow, self).get_parser(prog_name)
56
57 parser.add_argument(
58 'user', metavar='<user>',
59 help=_("The user's ID or name."))
60 return parser
61
62 def take_action(self, parsed_args):
63 client = self.app.client_manager.admin_logic
64 # This ends up for names doing multiple requests, it may
65 # be better to do something slightly different here
66 user_id = utils.find_resource(client.users, parsed_args.user)
67 user = client.users.get(user_id)
68 return zip(*(user.to_dict()).items())
69
70
71 class UserInvite(command.Command):
72 """Invites a user to become a member of a project.
73
74 User does not need to have an existing openstack account.
75 """
76
77 def get_parser(self, prog_name):
78 parser = super(UserInvite, self).get_parser(prog_name)
79
80 parser.add_argument(
81 '--username', metavar='<username>',
82 default=None,
83 help=_('The username for the new user.'))
84 parser.add_argument(
85 'email', metavar='<email>',
86 help=_('Email address of user to invite'))
87 parser.add_argument(
88 'roles', metavar='<role>', nargs='+',
89 help=_('Roles to give to the user.'))
90 return parser
91
92 def take_action(self, parsed_args):
93 if not parsed_args.roles:
94 parsed_args.roles = ['Member']
95 client = self.app.client_manager.admin_logic
96 client.users.invite(
97 username=parsed_args.username, email=parsed_args.email,
98 role_list=parsed_args.roles)
99 print("User invited")
100
101
102 class UserInviteCancel(command.Command):
103 """Cancel invite(s) to a project."""
104 def get_parser(self, prog_name):
105 parser = super(UserInviteCancel, self).get_parser(prog_name)
106
107 parser.add_argument(
108 'user', metavar='<user>',
109 nargs='+',
110 help=_("The user's name or id."))
111 return parser
112
113 def take_action(self, parsed_args):
114 client = self.app.client_manager.admin_logic
115 for user in parsed_args.user:
116 try:
117 user_id = client.users.find(name=user).id
118 except exc.NotFound:
119 user_id = client.users.find(id=user).id
120 client.users.cancel(user_id=user_id)
121 print("Invite(s) Cancelled")
122
123
124 class UserRoleAdd(command.Command):
125 """Add a role to a user."""
126 def get_parser(self, prog_name):
127 parser = super(UserRoleAdd, self).get_parser(prog_name)
128
129 parser.add_argument(
130 'user', metavar='<user>',
131 help=_("The user's name or id.."))
132 parser.add_argument(
133 'role', metavar='<role>',
134 help=_("The role's name or id."))
135 return parser
136
137 def take_action(self, parsed_args):
138 client = self.app.client_manager.admin_logic
139
140 role = utils.find_resource(client.managed_roles, parsed_args.role)
141 user = utils.find_resource(client.users, parsed_args.user)
142 if client.user_roles.add(user.id, role=role.name):
143 print(_("Role added"))
144
145
146 class UserRoleRemove(command.Command):
147 """Remove a role from a user."""
148 def get_parser(self, prog_name):
149 parser = super(UserRoleRemove, self).get_parser(prog_name)
150
151 parser.add_argument(
152 'user', metavar='<user>',
153 help=_("The user's name or id.."))
154 parser.add_argument(
155 'role', metavar='<role>',
156 help=_("The role's name or id."))
157 return parser
158
159 def take_action(self, parsed_args):
160 client = self.app.client_manager.admin_logic
161 role = utils.find_resource(client.managed_roles, parsed_args.role)
162 user = utils.find_resource(client.users, parsed_args.user)
163
164 if client.user_roles.remove(user.id, role=role.name):
165 print(_("Role removed"))
166
167
168 class UserRoleList(command.Lister):
169 """Lists the roles a user has on a project"""
170 def get_parser(self, prog_name):
171 parser = super(UserRoleList, self).get_parser(prog_name)
172
173 parser.add_argument(
174 'user', metavar='<user>',
175 help=_("Name or ID of user."))
176 return parser
177
178 def take_action(self, parsed_args):
179 client = self.app.client_manager.admin_logic
180
181 user = utils.find_resource(client.users, parsed_args.user)
182
183 return ['name'], [[role] for role in user.roles]
184
185
186 class ManageableRolesList(command.Lister):
187 """Lists roles able to be managed by the current user """
188 def take_action(self, parsed_args):
189 client = self.app.client_manager.admin_logic
190 roles = client.managed_roles.list()
191
192 headers = ['id', 'name']
193 rows = [[role.id, role.name] for role in roles]
194 return headers, rows
195
196
197 class PasswordReset(command.Command):
198 """Force password reset for a user, admin only. """
199
200 def get_parser(self, prog_name):
201 parser = super(PasswordReset, self).get_parser(prog_name)
202
203 parser.add_argument(
204 'email', metavar='<email>',
205 help=_("Email address of the user."))
206 parser.add_argument(
207 '--username', metavar='<username>', default=None,
208 help=_('Username of the account to reset if the username '
209 'is different than the email'))
210 return parser
211
212 def take_action(self, parsed_args):
213 client = self.app.client_manager.admin_logic
214
215 data = {'email': parsed_args.email}
216 if parsed_args.username:
217 data['username'] = parsed_args.username
218
219 client.users.password_force_reset(data)
220 print("Task has been sucessfully submitted.")
221 print("If a user with that email exists, a reset "
222 "token will be issued.")
223
224
225 class PasswordForgot(command.Command):
226 """Links to user forgotten password endpoint, does not require auth."""
227 auth_required = False
228
229 def get_parser(self, prog_name):
230 parser = super(PasswordForgot, self).get_parser(prog_name)
231
232 parser.add_argument(
233 'email', metavar='<email>',
234 help=_("Email address of the user."))
235 parser.add_argument(
236 '--username', metavar='<username>', default=None,
237 help=_('Username of the account to reset if the username '
238 'is different than the email'))
239 parser.add_argument(
240 '--bypass-url', metavar='<bypass-url>', default=None,
241 help=_('Bypasss URL for unauthenticated access to the endpoint.'))
242 return parser
243
244 def take_action(self, parsed_args):
245 if not parsed_args.bypass_url:
246 self.app.client_manager._auth_required = True
247 self.app.client_manager.setup_auth()
248 client = self.app.client_manager.admin_logic
249 else:
250 client = adjutant_client.Client(1, parsed_args.bypass_url)
251
252 client.users.password_forgot(parsed_args.email, parsed_args.username)
253 print("Task has been sucessfully submitted.")
254 print("If a user with that email exists, a reset "
255 "token will be issued.")