def get_nav_items(self): items = [('Home', '/'), ('Competitions', [ ('Nationals', '/nationals/2018'), ('Regional Championships', '/regional'), ]), ('Competitors', [ ('State Rankings', '/state_rankings'), ]), ('Organizers', [ ('CubingUSA Supported Competitions', '/supported'), ]), ('About', [ ('About CubingUSA', '/about'), ('Who we are', '/about/who'), ('Donations', '/about/donations'), ('Contact Us', '/about/contact'), ('Logo', '/about/logo'), ('Public Documents', '/about/documents'), ]), ] if self.user and self.user.HasAnyRole(Roles.AdminRoles()): admin_list = [('Edit Users', '/admin/edit_users')] items.append(('Admin', admin_list)) return items
def EditableRoles(user, editor): if not editor: return [] if editor.HasAnyRole([Roles.GLOBAL_ADMIN]): return Roles.AllRoles() elif editor.HasAnyRole([Roles.WEBMASTER, Roles.DIRECTOR]): return [Roles.WEBMASTER, Roles.DIRECTOR] else: return []
def TemplateDict(self, user): return { 'c': common.Common(self), 'user': user, 'all_roles': Roles.AllRoles(), 'editing_location_enabled': auth.CanEditLocation(user=user, editor=self.user), 'can_view_roles': auth.CanViewRoles(user=user, viewer=self.user), 'editable_roles': auth.EditableRoles(user=user, editor=self.user), }
def CanEditLocation(user, editor): if not editor: return False if editor.HasAnyRole(Roles.AdminRoles()): return True if user == editor: last_update = (UserLocationUpdate.query( UserLocationUpdate.user == user.key).order( -UserLocationUpdate.update_time).get()) if last_update and datetime.datetime.now( ) - last_update.update_time < datetime.timedelta(days=365): return False return True else: return False
def get(self, user_id, role): user = (User.get_by_id(user_id) or User.query(User.wca_person == ndb.Key(Person, user_id))).get() if not user: self.response.write('error: unrecognized user %s' % user_id) self.response.set_status(400) return if role not in Roles.AllRoles(): self.response.write('error: unrecognized role %s' % role) self.response.set_status(400) return if user.HasAnyRole(role): self.response.write('user already had role %s' % role) return user.roles.append(role) user.put() self.response.write('ok')
from src.handlers.admin.update_championships import UpdateChampionshipsHandler from src.handlers.admin.update_states import UpdateStatesHandler from src.handlers.admin.upload_users import UploadUsersHandler from src.handlers.admin.get_wca_export import GetExportHandler from src.handlers.admin.app_settings import AppSettingsHandler from src.handlers.basic import BasicHandler from src.handlers.login import LoginHandler from src.handlers.login import LoginCallbackHandler from src.handlers.login import LogoutHandler from src.handlers.oauth import AuthenticateHandler from src.models.user import Roles app = webapp2.WSGIApplication([ webapp2.Route('/', handler=BasicHandler('admin/index.html', permitted_roles=Roles.AdminRoles())), webapp2.Route('/authenticate', handler=AuthenticateHandler), webapp2.Route('/login', handler=LoginHandler, name='login'), webapp2.Route('/login_callback', handler=LoginCallbackHandler, name='login_callback'), webapp2.Route('/logout', handler=LogoutHandler, name='logout'), webapp2.Route('/post_import_mutations', handler=PostImportMutationsHandler), webapp2.Route('/update_championships', handler=UpdateChampionshipsHandler), webapp2.Route('/update_states', handler=UpdateStatesHandler), webapp2.Route( '/upload_users', handler=UploadUsersHandler, name='upload_users'), webapp2.Route('/wca/get_export', handler=GetExportHandler), webapp2.Route('/assign_role/<user_id:.*>/<role:.*>', handler=AssignRoleHandler),
def PermittedRoles(self): return Roles.AdminRoles()
def CanViewRoles(user, viewer): if not viewer: return False return (viewer.HasAnyRole(Roles.DelegateRoles()) or viewer.HasAnyRole(Roles.AdminRoles()))
webapp2.Route( '/async/champions_by_year/<event_id:.*>/<championship_type:.*>/<championship_region:.*>', handler=ChampionsTableHandler), webapp2.Route( '/async/champions_by_region/<event_id:.*>/<championship_type:.*>/<year:\d*>', handler=ChampionsTableHandler), webapp2.Route( '/async/state_rankings/<event_id:.*>/<state_id:.*>/<use_average:\d>', handler=StateRankingsHandler), webapp2.Route( '/async/championship_psych/<championship_id:.*>/<event_id:.*>', handler=ChampionshipPsychAsyncHandler), # Admin webapp2.Route('/admin/edit_users', handler=BasicHandler('admin/edit_users.html', permitted_roles=Roles.AllRoles()), name='admin_edit_users'), webapp2.Route('/admin/upload_document', handler=UploadDocumentHandler, name='upload_document'), webapp2.Route('/admin/delete_document/<document_id:.*>', handler=DeleteDocumentHandler, name='delete_document'), webapp2.Route('/admin/restore_document/<document_id:.*>', handler=RestoreDocumentHandler, name='restore_document'), webapp2.Route('/admin/permanently_delete_documents', handler=PermanentlyDeleteDocumentsHandler), webapp2.Route('/admin/edit_championships', handler=EditChampionshipsHandler, name='edit_championships'),
def PermittedRoles(self): return Roles.AdminRoles() + Roles.DelegateRoles()
def CanEditLocation(user, editor): if not editor: return False if editor.HasAnyRole(Roles.AdminRoles()): return True return user == editor
def get(self): OAuthBaseHandler.get(self) if not self.auth_token: return response = self.GetWcaApi('/api/v0/me') if response.status != 200: self.response.set_status(response.status) logging.error('Error from WCA: ' + self.response.read()) return # Save the account information we need. wca_info = json.loads(response.read())['me'] self.session['wca_account_number'] = str(wca_info['id']) self.session['login_time'] = ( datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() user = User.get_by_id(str( wca_info['id'])) or User(id=str(wca_info['id'])) if 'wca_id' in wca_info: user.wca_person = ndb.Key(Person, wca_info['wca_id']) else: del user.wca_person if 'name' in wca_info: user.name = wca_info['name'] else: del user.name if 'email' in wca_info: user.email = wca_info['email'] else: del user.email user.roles = [ role for role in user.roles if role not in Roles.DelegateRoles() ] if 'delegate_status' in wca_info: if wca_info['delegate_status'] == 'senior_delegate': user.roles.append(Roles.SENIOR_DELEGATE) elif wca_info['delegate_status'] == 'delegate': user.roles.append(Roles.DELEGATE) elif wca_info['delegate_status'] == 'candidate_delegate': user.roles.append(Roles.CANDIDATE_DELEGATE) wca_id_user = User.get_by_id(wca_info['wca_id']) if wca_id_user: if wca_id_user.city and not user.city: user.city = wca_id_user.city if wca_id_user.state and not user.state: user.state = wca_id_user.state if wca_id_user.latitude and not user.latitude: user.latitude = wca_id_user.latitude if wca_id_user.longitude and not user.longitude: user.longitude = wca_id_user.longitude wca_id_user.key.delete() user.last_login = datetime.datetime.now() user.put() self.redirect(str(self.handler_data))
def get(self): OAuthBaseHandler.GetTokenFromCode(self) if not self.auth_token: return response = self.GetWcaApi('/api/v0/me') if response.status != 200: self.response.set_status(response.status) logging.error('Error from WCA: ' + self.response.read()) return # Save the account information we need. wca_info = json.loads(response.read())['me'] self.session['wca_account_number'] = str(wca_info['id']) self.session['login_time'] = ( datetime.datetime.now() - datetime.datetime.utcfromtimestamp(0)).total_seconds() user = User.get_by_id(str( wca_info['id'])) or User(id=str(wca_info['id'])) if 'wca_id' in wca_info and wca_info['wca_id']: user.wca_person = ndb.Key(Person, wca_info['wca_id']) # If the user has a state on their account, we should update this on the # Person and Ranks as wel. if user.state: person = user.wca_person.get() person.state = user.state person.put() for rank_class in (RankSingle, RankAverage): ndb.put_multi( rank_class.query( rank_class.person == person.key).fetch()) else: del user.wca_person if 'name' in wca_info: user.name = wca_info['name'] else: del user.name if 'email' in wca_info: user.email = wca_info['email'] else: del user.email user.roles = [ role for role in user.roles if role not in Roles.DelegateRoles() ] if 'delegate_status' in wca_info: if wca_info['delegate_status'] == 'senior_delegate': user.roles.append(Roles.SENIOR_DELEGATE) elif wca_info['delegate_status'] == 'delegate': user.roles.append(Roles.DELEGATE) elif wca_info['delegate_status'] == 'candidate_delegate': user.roles.append(Roles.CANDIDATE_DELEGATE) if wca_info['wca_id']: wca_id_user = User.get_by_id(wca_info['wca_id']) else: wca_id_user = None if wca_id_user: if wca_id_user.city and not user.city: user.city = wca_id_user.city if wca_id_user.state and not user.state: user.state = wca_id_user.state if wca_id_user.latitude and not user.latitude: user.latitude = wca_id_user.latitude if wca_id_user.longitude and not user.longitude: user.longitude = wca_id_user.longitude wca_id_user.key.delete() user.last_login = datetime.datetime.now() user.put() self.redirect(str(self.handler_data))