def users_get(self, request): """Get a user.""" owner_project_only = request.ownerProjectsOnly mar = self.mar_factory(request) (visible_ownership, visible_deleted, visible_membership, visible_contrib) = sitewide_helpers.GetUserProjects( mar.cnxn, self._services, mar.auth.user_pb, mar.auth.effective_ids, mar.viewed_user_auth.effective_ids) project_list = [] for proj in (visible_ownership + visible_deleted): config = self._services.config.GetProjectConfig( mar.cnxn, proj.project_id) proj_result = api_pb2_v1_helpers.convert_project( proj, config, api_pb2_v1.Role.owner) project_list.append(proj_result) if not owner_project_only: for proj in visible_membership: config = self._services.config.GetProjectConfig( mar.cnxn, proj.project_id) proj_result = api_pb2_v1_helpers.convert_project( proj, config, api_pb2_v1.Role.member) project_list.append(proj_result) for proj in visible_contrib: config = self._services.config.GetProjectConfig( mar.cnxn, proj.project_id) proj_result = api_pb2_v1_helpers.convert_project( proj, config, api_pb2_v1.Role.contributor) project_list.append(proj_result) return api_pb2_v1.UsersGetResponse( id=str(mar.viewed_user_auth.user_id), kind='monorail#user', projects=project_list, )
def testGetUserProjects_AdminSeesAll(self): (owned, archived, membered, _contrib) = sitewide_helpers.GetUserProjects(self.cnxn, self.services, self.admin_user, {ADMIN_USER_ID}, {OTHER_USER_ID}) self.assertProjectsAnyOrder(owned, self.other_owner_live) self.assertProjectsAnyOrder(archived, self.other_owner_archived) self.assertProjectsAnyOrder(membered, self.other_committer_live)
def testGetUserProjects_UserSeesOwnArchived(self): (owned, archived, membered, _contrib) = sitewide_helpers.GetUserProjects(self.cnxn, self.services, self.regular_user, {REGULAR_USER_ID}, {REGULAR_USER_ID}) self.assertProjectsAnyOrder(owned, self.regular_owner_live, self.members_only_project) self.assertProjectsAnyOrder(archived, self.regular_owner_archived) self.assertProjectsAnyOrder(membered, self.regular_committer_live)
def testGetUserProjects_OnlyLiveOfOtherUsers(self): """Regular users should only see live projects of other users.""" (owned, archived, membered, _contrib) = sitewide_helpers.GetUserProjects(self.cnxn, self.services, self.regular_user, {REGULAR_USER_ID}, {OTHER_USER_ID}) self.assertProjectsAnyOrder(owned, self.other_owner_live) self.assertEqual([], archived) self.assertProjectsAnyOrder(membered, self.other_committer_live)
def testGetUserProjects_UserSeesGroupArchived(self): # Now imagine that "regular user" is part of a user group, and the "other # user" is the group. Users and user groups are treated # the same: there's no separate way to represent a user group. (owned, archived, membered, _contrib) = sitewide_helpers.GetUserProjects( self.cnxn, self.services, self.regular_user, {REGULAR_USER_ID, OTHER_USER_ID}, {REGULAR_USER_ID, OTHER_USER_ID}) self.assertProjectsAnyOrder(owned, self.regular_owner_live, self.members_only_project, self.other_owner_live) self.assertProjectsAnyOrder(archived, self.regular_owner_archived, self.other_owner_archived) self.assertProjectsAnyOrder(membered, self.regular_committer_live, self.other_committer_live)
def _GatherProjects(self, mr): """Return a dict of project names the current user is involved in.""" with self.profiler.Phase('GetUserProjects'): project_lists = sitewide_helpers.GetUserProjects( mr.cnxn, self.services, mr.auth.user_pb, mr.auth.effective_ids, mr.auth.effective_ids) (visible_ownership, _visible_deleted, visible_membership, visible_contrib) = project_lists with self.profiler.Phase('GetStarredProjects'): starred_projects = sitewide_helpers.GetViewableStarredProjects( mr.cnxn, self.services, mr.auth.user_id, mr.auth.effective_ids, mr.auth.user_pb) projects_dict = { 'memberof': [p.project_name for p in visible_membership], 'ownerof': [p.project_name for p in visible_ownership], 'contributorto': [p.project_name for p in visible_contrib], 'starred_projects': [p.project_name for p in starred_projects], } return projects_dict
def GatherPageData(self, mr): """Build up a dictionary of data values to use when rendering the page. Args: mr: commonly used info parsed from the request. Returns: Dict of values used by EZT for rendering the page. """ can_create_project = permissions.CanCreateProject(mr.perms) # Kick off the search pipeline, it has its own promises for parallelism. pipeline = projectsearch.ProjectSearchPipeline(mr, self.services, self.profiler) # Meanwhile, determine which projects the signed-in user has starred. starred_project_ids = set() # A dict of project id to the user's membership status. project_memberships = {} if mr.auth.user_id: starred_projects = sitewide_helpers.GetViewableStarredProjects( mr.cnxn, self.services, mr.auth.user_id, mr.auth.effective_ids, mr.auth.user_pb) starred_project_ids = {p.project_id for p in starred_projects} owned, _archive_owned, member_of, contrib_of = ( sitewide_helpers.GetUserProjects(mr.cnxn, self.services, mr.auth.user_pb, mr.auth.effective_ids, mr.auth.effective_ids)) project_memberships.update( {proj.project_id: 'Owner' for proj in owned}) project_memberships.update( {proj.project_id: 'Member' for proj in member_of}) project_memberships.update( {proj.project_id: 'Contributor' for proj in contrib_of}) # Finish the project search pipeline. pipeline.SearchForIDs() pipeline.GetProjectsAndPaginate(mr.cnxn, urls.HOSTING_HOME) project_ids = [p.project_id for p in pipeline.visible_results] star_count_dict = self.services.project_star.CountItemsStars( mr.cnxn, project_ids) # Make ProjectView objects project_view_list = [ project_views.ProjectView( p, starred=p.project_id in starred_project_ids, num_stars=star_count_dict.get(p.project_id), membership_desc=project_memberships.get(p.project_id)) for p in pipeline.visible_results ] return { 'can_create_project': ezt.boolean(can_create_project), 'learn_more_link': settings.learn_more_link, 'projects': project_view_list, 'pagination': pipeline.pagination, }
def GatherPageData(self, mr): """Build up a dictionary of data values to use when rendering the page.""" viewed_user = mr.viewed_user_auth.user_pb if self.services.usergroup.GetGroupSettings( mr.cnxn, mr.viewed_user_auth.user_id): url = framework_helpers.FormatAbsoluteURL(mr, '/g/%s/' % viewed_user.email, include_project=False) self.redirect(url, abort=True) # Show group page instead. with self.profiler.Phase('GetUserProjects'): project_lists = sitewide_helpers.GetUserProjects( mr.cnxn, self.services, mr.auth.user_pb, mr.auth.effective_ids, mr.viewed_user_auth.effective_ids) (visible_ownership, visible_archived, visible_membership, visible_contrib) = project_lists viewed_user_display_name = framework_views.GetViewedUserDisplayName(mr) with self.profiler.Phase('GetStarredProjects'): starred_projects = sitewide_helpers.GetViewableStarredProjects( mr.cnxn, self.services, mr.viewed_user_auth.user_id, mr.auth.effective_ids, mr.auth.user_pb) logged_in_starred_pids = [] if mr.auth.user_id: logged_in_starred_pids = self.services.project_star.LookupStarredItemIDs( mr.cnxn, mr.auth.user_id) starred_user_ids = self.services.user_star.LookupStarredItemIDs( mr.cnxn, mr.viewed_user_auth.user_id) starred_user_dict = framework_views.MakeAllUserViews( mr.cnxn, self.services.user, starred_user_ids) starred_users = starred_user_dict.values() is_user_starred = self._IsUserStarred(mr.cnxn, mr.auth.user_id, mr.viewed_user_auth.user_id) if viewed_user.last_visit_timestamp: last_visit_str = timestr.FormatRelativeDate( viewed_user.last_visit_timestamp, days_only=True) last_visit_str = last_visit_str or 'Less than 2 days ago' else: last_visit_str = 'Never' if viewed_user.email_bounce_timestamp: last_bounce_str = timestr.FormatRelativeDate( viewed_user.email_bounce_timestamp, days_only=True) last_bounce_str = last_bounce_str or 'Less than 2 days ago' else: last_bounce_str = None can_ban = permissions.CanBan(mr, self.services) viewed_user_is_spammer = viewed_user.banned.lower() == 'spam' viewed_user_may_be_spammer = not viewed_user_is_spammer all_projects = self.services.project.GetAllProjects(mr.cnxn) for project_id in all_projects: project = all_projects[project_id] viewed_user_perms = permissions.GetPermissions( viewed_user, mr.viewed_user_auth.effective_ids, project) if (viewed_user_perms != permissions.EMPTY_PERMISSIONSET and viewed_user_perms != permissions.USER_PERMISSIONSET): viewed_user_may_be_spammer = False ban_token = None ban_spammer_token = None if mr.auth.user_id and can_ban: form_token_path = mr.request.path + 'ban.do' ban_token = xsrf.GenerateToken(mr.auth.user_id, form_token_path) form_token_path = mr.request.path + 'banSpammer.do' ban_spammer_token = xsrf.GenerateToken(mr.auth.user_id, form_token_path) page_data = { 'user_tab_mode': 'st2', 'viewed_user_display_name': viewed_user_display_name, 'viewed_user_may_be_spammer': ezt.boolean(viewed_user_may_be_spammer), 'viewed_user_is_spammer': ezt.boolean(viewed_user_is_spammer), 'viewed_user_is_banned': ezt.boolean(viewed_user.banned), 'viewed_user_ignore_action_limits': (ezt.boolean(viewed_user.ignore_action_limits)), 'owner_of_projects': [ project_views.ProjectView(p, starred=p.project_id in logged_in_starred_pids) for p in visible_ownership ], 'committer_of_projects': [ project_views.ProjectView(p, starred=p.project_id in logged_in_starred_pids) for p in visible_membership ], 'contributor_to_projects': [ project_views.ProjectView(p, starred=p.project_id in logged_in_starred_pids) for p in visible_contrib ], 'owner_of_archived_projects': [project_views.ProjectView(p) for p in visible_archived], 'starred_projects': [ project_views.ProjectView(p, starred=p.project_id in logged_in_starred_pids) for p in starred_projects ], 'starred_users': starred_users, 'is_user_starred': ezt.boolean(is_user_starred), 'viewing_user_page': ezt.boolean(True), 'last_visit_str': last_visit_str, 'last_bounce_str': last_bounce_str, 'vacation_message': viewed_user.vacation_message, 'can_ban': ezt.boolean(can_ban), 'ban_token': ban_token, 'ban_spammer_token': ban_spammer_token } settings = framework_helpers.UserSettings.GatherUnifiedSettingsPageData( mr.auth.user_id, mr.viewed_user_auth.user_view, viewed_user) page_data.update(settings) return page_data