def authenticate(self, request): email = request.user_email validate_email(email) team_key = request.team_key user = User.find_user_by_email(email, team_key) if team_key: team_secret = request.team_secret validate_team_secret(team_secret) display_name = request.display_name image_url = request.user_image_url if not user: user = User.insert_user(email=email, username=display_name, account_type=team_key, image_url=image_url) community = Community.getCommunityFromTeamKey(team_key) UserRole.insert(user, community) elif (display_name != user.display_name) or (image_url != user.image_url): User.update_user(user=user, email=email, username=display_name, account_type=team_key, image_url=image_url) if not Community.authenticate(team_key, md5(team_secret)): raise endpoints.UnauthorizedException("Authentication failed. Team key and secret are not matched.") elif user.auth_source == AuthSourceType.ANNO: password = request.password validate_password(password) if not user: raise endpoints.NotFoundException("Authentication failed. User account " + email + " doesn't exist.") if not User.authenticate(email, md5(password)): raise endpoints.UnauthorizedException("Authentication failed. Email and password are not matched.") else: raise endpoints.ForbiddenException("Account for '%s' is Google or Facebook OAuth account." % email) return UserMessage(id=user.key.id(), display_name=user.display_name)
def dashboard_authenticate(self, request): email = request.user_email password = request.password team_key = request.team_key accounts = [] if email and password: users = User.get_all_user_by_email(email, md5(password), team_key=team_key) for user in users: if user: team_key = user.account_type team = Community.getCommunityFromTeamKey(team_key) if team: userTeamToken = get_user_team_token(email, password, team_key, team.team_secret, user.display_name, user.image_url) account = AccountAuthenticateMessage(display_name=user.display_name, image_url=user.image_url, team_name=team.name, team_key=team_key, team_hash=team.team_hash, user_team_token=json.dumps(userTeamToken), role=UserRole.getRole(user, team)) accounts.append(account) return AccountAuthenticateListMessage(authenticated=True if len(accounts) else False, account_info=accounts)
def create_teams(): team_key = "" app_name = "" community_name = "" admin_user_email = "" other_users_email = [] app = AppInfo.query().filter(AppInfo.lc_name == app_name.lower()).get() if not app: appinfo_message = AppInfoMessage() appinfo_message.name = app_name app = AppInfo.insert(appinfo_message) community = Community.getCommunityFromTeamKey(team_key=team_key) if not community: community_message = CommunityMessage(name=community_name, team_key=team_key, team_secret=md5(community_name.lower())) community_message.user = UserMessage(user_email=admin_user_email) Community.insert(community_message) community = Community.getCommunityFromTeamKey(team_key=team_key) if community and app: if not app.key in community.apps: community.apps.append(app.key) community.put() for user_email in other_users_email: user = User.find_user_by_email(email=user_email, team_key=team_key) if not user: user = User.insert_user(user_email, account_type=team_key, image_url="") userrole = UserRole.insert(user, community)
def update_user(self, request): action_auth_user = auth_user(self.request_state.headers) if not (action_auth_user.user_email == request.user_email): if not is_auth_user_admin(action_auth_user=action_auth_user): return ResponseMessage(success=False) user = get_user_from_request(user_id=request.user_id, user_email=request.user_email, team_key=request.team_key) if user: user.display_name = request.user_display_name or user.display_name user.password = md5(request.user_password) if request.user_password else user.password user.image_url = request.user_image_url or user.image_url or "" user.put() community = Community.getCommunityFromTeamKey(request.team_key) if request.team_key else Community.get_by_id(request.community_id) resp = None if user and community: circle = 0 for circle_value, circle_name in community.circles.iteritems(): if circle_name == request.circle: circle = int(circle_value) resp = UserRole.edit(user, community, request.role, circle) return ResponseMessage(success=True if resp else False)
def insert_user(self, request): if not is_auth_user_admin(headers=self.request_state.headers): return ResponseMessage(success=False) action_user = auth_user(self.request_state.headers) user = get_user_from_request(user_id=request.user_id, user_email=request.user_email, team_key=request.team_key) if not user: user = User.insert_user(request.user_email, username=request.user_display_name, account_type=request.team_key, auth_source=AuthSourceType.PLUGIN, password=md5(request.user_password), image_url=request.user_image_url or "") community = Community.getCommunityFromTeamKey(request.team_key) if request.team_key else Community.get_by_id(request.community_id) role = request.role if request.role else UserRoleType.MEMBER resp = None if user and community: circle = 0 for circle_value, circle_name in community.circles.iteritems(): if circle_name == request.circle: circle = int(circle_value) resp = UserRole.insert(user, community, role, circle) send_added_user_email(community.name, user.display_name, "added", action_user.display_name, community.team_hash) return ResponseMessage(success=True if resp else False)
def update_password(self, request): current_user = get_endpoints_current_user(raise_unauthorized=False) if current_user is not None: raise endpoints.BadRequestException("Google OAuth User can't update password.") user = auth_user(self.request_state.headers) user.password = md5(request.password) user.put() return message_types.VoidMessage()
def reset_team_secret(cls, team_key): team_secret = None community = cls.getCommunityFromTeamKey(team_key) if community: from helper.utils import md5 community.team_secret = md5(hex(random.randint(1000000, 9999999))[2:]) team_secret = community.team_secret community.put() return team_secret
def add_teamhash(cursor=None): community_list, cursor, more = Community.query().fetch_page(BATCH_SIZE, start_cursor=cursor) community_update_list = [] for community in community_list: if (not community.team_hash) and community.team_key: community.team_hash = md5(community.team_key)[-8:] community_update_list.append(community) if len(community_update_list): ndb.put_multi(community_update_list) if more: add_teamhash(cursor=cursor)
def register(self, request): email = request.user_email validate_email(email) password = request.password validate_password(password) user = User.find_user_by_email(email) if user is not None: raise endpoints.BadRequestException("Email(" + email + ") already exists.") display_name = request.display_name if display_name is None or display_name == '': raise endpoints.BadRequestException("Registration failed. Display name is missing.") user = User.find_user_by_display_name(display_name) if user is not None: raise endpoints.BadRequestException("Display name(" + display_name + ") already exists.") user = User.insert_user(email=email, username=display_name, password=md5(password)) return UserMessage(id=user.key.id())
def insert(cls, message, getCommunity=False): community, user = None, None try: from helper.utils import get_user_from_request if message.name is None: return "Community name is required" if not getCommunity else (community, user) if message.type: # community should be of type 'private' or 'public' if not message.type in [CommunityType.PRIVATE, CommunityType.PUBLIC]: return "Community should be of type 'private' or 'public'" if not getCommunity else (community, user) # only one public community is allowed elif message.type == CommunityType.PUBLIC: queryResultCount = Community.query(Community.type == message.type).count() if queryResultCount: return "Community not created. Can't create more than one public community." if not getCommunity else (community, user) else: message.type = CommunityType.PRIVATE community = cls.getCommunityFromTeamKey(team_key=message.team_key) if not community: from helper.utils import md5 team_hash = md5(message.team_key)[-8:] community = cls(name=message.name, description=message.description, welcome_msg=message.welcome_msg, type=message.type, team_key=message.team_key, team_secret=message.team_secret, team_hash=team_hash, plan=message.plan) community.circles = { CircleValue.CONTRIBUTOR : CircleType.CONTRIBUTOR, CircleValue.BETA_TESTER : CircleType.BETA_TESTER, CircleValue.ALPHA_TESTER : CircleType.ALPHA_TESTER, CircleValue.DEVELOPER : CircleType.DEVELOPER } community.put() respData = "Community created." user = get_user_from_request(user_id=message.user.id, user_email=message.user.user_email, team_key=message.team_key) userrole = None userrole_type = UserRoleType.ADMIN if message.team_key else UserRoleType.MANAGER if (not user) and message.team_key and message.user.user_email: from model.user import User from helper.utils import md5 user = User.insert_user(message.user.user_email, username=message.user.display_name, account_type=message.team_key, auth_source=AuthSourceType.PLUGIN, password=md5(message.user.password), image_url="") if user: from model.userrole import UserRole userrole = UserRole.insert(user, community, userrole_type, int(CircleValue.DEVELOPER)) if userrole is None: community.key.delete() respData = "Community is not created as user doesn't exist" except Exception as e: logging.exception("Exception while inserting community: %s" % e) respData = e return respData if not getCommunity else (community, user)
def create_sdk_team(cls, message, pricing_plan=False, stripe_token=None): team_key = message.team_key app_name = message.app.name community_name = message.community_name or app_name plan = message.plan or PlanType.BASIC app = AppInfo.query().filter(AppInfo.lc_name == app_name.lower()).get() if not app: appinfo_message = AppInfoMessage() appinfo_message.name = app_name appinfo_message.icon_url = message.app.icon_url appinfo_message.version = message.app.version app = AppInfo.insert(appinfo_message) from helper.utils import md5 community_message = CommunityMessage(name=community_name, team_key=team_key, team_secret=md5(community_name.lower()), plan=plan) community_message.user = UserMessage(user_email=message.admin_user.user_email, display_name=message.admin_user.display_name, password=message.admin_user.password) community, user = cls.insert(community_message, getCommunity=True) communities_message = [] if community and app: if not app.key in community.apps: community.apps.append(app.key) community.put() # response message community_message = CommunityAdminMasterMessage() community_message.community_name = community.name community_message.team_key = community.team_key community_message.team_secret = community.team_secret # community_message.team_hash = community.team_hash community_message.app_name = app.name community_message.app_icon = app.icon_url community_message.users = [] from model.userrole import UserRole user_message = UserAdminMasterMessage() user_message.display_name = user.display_name user_message.user_email = user.user_email user_message.password_present = True if user.password else False user_message.role = UserRole.getRole(user, community) user_message.image_url = user.image_url if community.circles: user_message.circle = community.circles.get(str(UserRole.getCircleLevel(user, community))) community_message.users.append(user_message) communities_message.append(community_message) registration_successful = True if pricing_plan: if stripe_token: payment_success = StripePayment.create_charge(stripe_token, community.key, plan) if not payment_success: registration_successful = False else: registration_successful = False if registration_successful: from helper.utils import send_created_team_email from helper.utils import send_access_team_email send_created_team_email(community.name, user.display_name) send_access_team_email(user.user_email, community.team_hash, community.name, community.team_key, community.team_secret) else: community.key.delete() communities_message = [] return communities_message