blob: 528d0b14d948803ec6b8cebc34ce6d81c2673a2f [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2018 The Chromium Authors
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
Copybara854996b2021-09-07 19:36:02 +00004
5from __future__ import print_function
6from __future__ import division
7from __future__ import absolute_import
8
9from api import converters
10from api import monorail_servicer
11from api import converters
12from api.api_proto import users_pb2
13from api.api_proto import users_prpc_pb2
14from api.api_proto import user_objects_pb2
15from businesslogic import work_env
16from framework import authdata
17from framework import framework_views
18from framework import permissions
19
20class UsersServicer(monorail_servicer.MonorailServicer):
21 """Handle API requests related to User objects.
22
23 Each API request is implemented with a method as defined in the
24 .proto file that does any request-specific validation, uses work_env
25 to safely operate on business objects, and returns a response proto.
26 """
27
28 DESCRIPTION = users_prpc_pb2.UsersServiceDescription
29
30 @monorail_servicer.PRPCMethod
31 def GetUser(self, mc, request):
32 """Return info about the specified user."""
33 with work_env.WorkEnv(mc, self.services) as we:
34 users, linked_user_ids = we.ListReferencedUsers(
35 [request.user_ref.display_name])
36 linked_user_views = framework_views.MakeAllUserViews(
37 mc.cnxn, self.services.user, linked_user_ids)
38
39 with mc.profiler.Phase('converting to response objects'):
40 response_users = converters.ConvertUsers(users, linked_user_views)
41
42 return response_users[0]
43
44 @monorail_servicer.PRPCMethod
45 def ListReferencedUsers(self, mc, request):
46 """Return the list of existing users in a response proto."""
47 emails = request.emails
48 if request.user_refs:
49 emails = [user_ref.display_name for user_ref in request.user_refs]
50 with work_env.WorkEnv(mc, self.services) as we:
51 users, linked_user_ids = we.ListReferencedUsers(emails)
52 linked_user_views = framework_views.MakeAllUserViews(
53 mc.cnxn, self.services.user, linked_user_ids)
54
55 with mc.profiler.Phase('converting to response objects'):
56 response_users = converters.ConvertUsers(users, linked_user_views)
57 response = users_pb2.ListReferencedUsersResponse(users=response_users)
58
59 return response
60
61 @monorail_servicer.PRPCMethod
62 def GetMemberships(self, mc, request):
63 """Return the user groups for the given user visible to the requester."""
64 user_id = converters.IngestUserRef(
65 mc.cnxn, request.user_ref, self.services.user)
66
67 with work_env.WorkEnv(mc, self.services) as we:
68 group_ids = we.GetMemberships(user_id)
69
70 with mc.profiler.Phase('converting to response objects'):
71 groups_by_id = framework_views.MakeAllUserViews(
72 mc.cnxn, self.services.user, group_ids)
73 group_refs = converters.ConvertUserRefs(
74 group_ids, [], groups_by_id, True)
75
76 return users_pb2.GetMembershipsResponse(group_refs=group_refs)
77
78 @monorail_servicer.PRPCMethod
79 def GetUserStarCount(self, mc, request):
80 """Return the star count for a given user."""
81 user_id = converters.IngestUserRef(
82 mc.cnxn, request.user_ref, self.services.user)
83
84 with work_env.WorkEnv(mc, self.services) as we:
85 star_count = we.GetUserStarCount(user_id)
86
87 result = users_pb2.GetUserStarCountResponse(star_count=star_count)
88 return result
89
90 @monorail_servicer.PRPCMethod
91 def StarUser(self, mc, request):
92 """Star a given user."""
93 user_id = converters.IngestUserRef(
94 mc.cnxn, request.user_ref, self.services.user)
95
96 with work_env.WorkEnv(mc, self.services) as we:
97 we.StarUser(user_id, request.starred)
98 star_count = we.GetUserStarCount(user_id)
99
100 result = users_pb2.StarUserResponse(star_count=star_count)
101 return result
102
103 @monorail_servicer.PRPCMethod
104 def SetExpandPermsPreference(self, mc, request):
105 """Set a users preference on whether to expand perms by default."""
106 with work_env.WorkEnv(mc, self.services) as we:
107 we.UpdateUserSettings(
108 mc.auth.user_pb, keep_people_perms_open=request.expand_perms)
109
110 result = users_pb2.SetExpandPermsPreferenceResponse()
111 return result
112
113 def _SignedInOrSpecifiedUser(self, mc, request):
114 """If request specifies a user, return it. Otherwise signed-in user."""
115 user_id = mc.auth.user_id
116 if request.HasField('user_ref'):
117 user_id = converters.IngestUserRef(
118 mc.cnxn, request.user_ref, self.services.user)
119 return user_id
120
121 @monorail_servicer.PRPCMethod
122 def GetSavedQueries(self, mc, request):
123 """Get a user's saved queries."""
124 user_id = self._SignedInOrSpecifiedUser(mc, request)
125
126 # Only site admins can view other user's saved queries.
127 if user_id != mc.auth.user_id and not mc.auth.user_pb.is_site_admin:
128 raise permissions.PermissionException(
129 'You are not allowed to view this user\'s saved queries')
130
131 saved_queries = self.services.features.GetSavedQueriesByUserID(
132 mc.cnxn, user_id)
133 return users_pb2.GetSavedQueriesResponse(
134 saved_queries=converters.IngestSavedQueries(mc.cnxn,
135 self.services.project, saved_queries))
136
137 @monorail_servicer.PRPCMethod
138 def GetUserPrefs(self, mc, request):
139 """Get a user's preferences."""
140 with work_env.WorkEnv(mc, self.services) as we:
141 userprefs = we.GetUserPrefs(self._SignedInOrSpecifiedUser(mc, request))
142
143 result = users_pb2.GetUserPrefsResponse(
144 prefs=converters.ConvertPrefValues(userprefs.prefs))
145 return result
146
147 @monorail_servicer.PRPCMethod
148 def SetUserPrefs(self, mc, request):
149 """Add to or set a users preferences."""
150 with work_env.WorkEnv(mc, self.services) as we:
151 pref_values = converters.IngestPrefValues(request.prefs)
152 we.SetUserPrefs(self._SignedInOrSpecifiedUser(mc, request), pref_values)
153
154 result = users_pb2.SetUserPrefsResponse()
155 return result
156
157 @monorail_servicer.PRPCMethod
158 def InviteLinkedParent(self, mc, request):
159 """Create a linked account invite."""
160 with work_env.WorkEnv(mc, self.services) as we:
161 we.InviteLinkedParent(request.email)
162
163 result = users_pb2.InviteLinkedParentResponse()
164 return result
165
166 @monorail_servicer.PRPCMethod
167 def AcceptLinkedChild(self, mc, request):
168 """Link a child account that has invited this account as parent."""
169 child_id = self.services.user.LookupUserID(mc.cnxn, request.email)
170 with work_env.WorkEnv(mc, self.services) as we:
171 we.AcceptLinkedChild(child_id)
172
173 result = users_pb2.AcceptLinkedChildResponse()
174 return result
175
176 @monorail_servicer.PRPCMethod
177 def UnlinkAccounts(self, mc, request):
178 """Unlink a specificed parent and child account."""
179 parent_id, child_id = converters.IngestUserRefs(
180 mc.cnxn, [request.parent, request.child], self.services.user)
181 with work_env.WorkEnv(mc, self.services) as we:
182 we.UnlinkAccounts(parent_id, child_id)
183
184 result = users_pb2.UnlinkAccountsResponse()
185 return result
186
187 @monorail_servicer.PRPCMethod
188 def GetUsersProjects(self, mc, request):
189 user_ids = converters.IngestUserRefs(
190 mc.cnxn, request.user_refs, self.services.user)
191 user_auths = [
192 authdata.AuthData.FromUserID(mc.cnxn, user_id, self.services)
193 for user_id in user_ids]
194
195 result = users_pb2.GetUsersProjectsResponse()
196 with work_env.WorkEnv(mc, self.services) as we:
197 for user_ref, auth in zip(request.user_refs, user_auths):
198 starred = we.ListStarredProjects(auth.user_id)
199 owner, _archived, member, contrib = we.GetUserProjects(
200 auth.effective_ids)
201 user_projects = result.users_projects.add()
202 user_projects.user_ref.CopyFrom(user_ref)
203 user_projects.owner_of.extend(p.project_name for p in owner)
204 user_projects.member_of.extend(p.project_name for p in member)
205 user_projects.contributor_to.extend(p.project_name for p in contrib)
206 user_projects.starred_projects.extend(p.project_name for p in starred)
207
208 return result
209
210 @monorail_servicer.PRPCMethod
211 def ExpungeUser(self, mc, request):
212 with work_env.WorkEnv(mc, self.services) as we:
213 we.ExpungeUsers([request.email])
214
215 response = users_pb2.ExpungeUserResponse()
216 return response