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 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 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 auth_user(headers): current_user = get_endpoints_current_user(raise_unauthorized=False) user = None if current_user is None: credential_pair = get_credential(headers) signinMethod = SignInMethod.ANNO team_key = None team_secret = None display_name = None image_url = None if len(credential_pair) == 2: email, password = credential_pair elif len(credential_pair) == 5: signinMethod, email, password, team_key, team_secret = credential_pair else: signinMethod, email, password, team_key, team_secret, display_name, image_url = credential_pair validate_email(email) user = User.find_user_by_email(email, team_key) if signinMethod == SignInMethod.ANNO: User.authenticate(email, md5(password)) elif signinMethod == SignInMethod.PLUGIN: if not Community.authenticate(team_key, team_secret): raise endpoints.UnauthorizedException("Incorrect team key or secret") display_name = unicode(display_name, "utf-8", "ignore") 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 and display_name != user.display_name) or (image_url and image_url != user.image_url): User.update_user(user=user, email=email, username=display_name, account_type=team_key, image_url=image_url) else: user = User.find_user_by_email(current_user.email()) if user is None: raise endpoints.UnauthorizedException("Oops, something went wrong. Please try later.") return user
def accept(cls, message): resp = None msg = "" invitation = cls.query(cls.invite_hash == message.invite_hash).get() if invitation: community = invitation.community.get() if community: if (invitation.email == message.user_email) or message.force: user = User.find_user_by_email(message.user_email) resp = UserRole.insert(user, community, invitation.role) if resp: invitation.key.delete() msg = "Invitation accepted" else: msg = "Error while adding user to community" else: msg = "User Mismatch" else: msg = "Community no longer exist" else: msg = "Invitation no longer exist" return (resp, msg)
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)