def get(self): """ Handles the /logout endpoint. Logs out users. """ session = SessionHandler() session.logout() clear_cookie(self, name="_ut_") success = "You have logged out successfully!" success_message(self, success) self.redirect("/login")
def post(self): """ Handles the /register/verify/send endpoint. Resends email verification. """ if self.POST("email"): email = self.POST("email").lower().strip() query = User.query() query = query.filter(User.current_email == email) user = query.get() if user: if user.status == "PENDING": content = { "token": user.confirmation_token, "uid": str(user.key.id()), "receiver_name": user.first_name, "receiver_email": user.current_email, "subject": "Email Verfication", "email_type": "verify" } taskqueue.add( url="/tasks/email/send", params=content, method="POST") success = "The verification email has been sent to " success += self.POST("email") + ". Please open the " success += "email and verify your account " success += "to complete the registration." success_message(self, success) self.redirect("/register/verify/send") else: error = "Account is already verified." error_message(self, error) self.redirect("/register/verify/send") else: error = "Sorry, " + self.POST("email") error += " does not belong to an existing account." error_message(self, error) self.redirect("/register/verify/send") else: error = "Email is required." error_message(self, error) self.redirect("/register/verify/send")
def post(self): """ Handles the /register endpoint. ODTF registration. """ json_data = {} for arg in self.request.arguments(): json_data[arg] = self.POST(arg) if self.POST("first_name") and self.POST("last_name") \ and self.POST("email") and self.POST("street_address") \ and self.POST("province") and self.POST("city") \ and self.POST("password") and self.POST("confirm_password"): user_exist = User.check_user(email=self.POST("email")) if user_exist: message = "Sorry, it looks like " message += self.POST("email") message += " belongs to an existing account. If this is yours, please login using your account." error_message(self, message) data = base64.b64encode(json.dumps(json_data)) set_cookie(self, name="_rm_", value=data) else: user = User.create_new_user( first_name=self.POST("first_name"), middle_name=self.POST("middle_name"), last_name=self.POST("last_name"), street_address=self.POST("street_address"), province=self.POST("province"), city=self.POST("city"), password=self.POST("password"), mobile=self.POST("mobile_number"), email=self.POST("email"), office_order_number=self.POST('office_order_number'), redirect=self.POST("redirect")) query = UserGroup.query() query = query.filter(UserGroup.invited_users == user.current_email) user_groups = query.fetch() if user_groups: for group in user_groups: if user.key not in group.users: group.users.append(user.key) if user.current_email in group.invited_users: group.invited_users.remove(user.current_email) group.put() if group.key in user.user_groups: user.user_groups.append(str(group.key.id())) user.put() success = "Thank you for your registration. " success += "We sent you a verification email, " success += "please open the email and verify your account " success += "to complete the registration." success_message(self, success) else: message = "We were unable to create your account. " message += "Please fill in all required fields." error_message(self, message) data = base64.b64encode(json.dumps(json_data)) set_cookie(self, name="_rm_", value=data) url = "/register" if self.POST("redirect"): url += "?redirect=" url += self.POST("redirect") self.redirect(url)
def post(self, data_id=None): response = { "code": 200, "type": "List of users", "method": "POST", "response": "OK", "data": [] } if data_id: user = User.get_by_id(int(data_id)) if self.POST("action") == "disable": response["type"] = "Disable user account." if user: user.status = "DISABLED" user.put() response["data"] = user.to_api_object( user_role=self.user.role, level=self.user.level) else: response["code"] = 400 response["response"] = "ERROR" wrap_response(self, response) return if self.POST("action") == "approve": response["type"] = "Enable user account." if user: user.status = "APPROVED" user.approved_by_key = self.user.key user.approved_by_name = self.user.name user.approved_on = datetime.datetime.utcnow() user.put() response["data"] = user.to_api_object( user_role=self.user.role, level=self.user.level) else: response["code"] = 400 response["response"] = "ERROR" wrap_response(self, response) return if self.POST("action") == "enable": response["type"] = "Enable user account." if user: user.status = "APPROVED" user.put() response["data"] = user.to_api_object( user_role=self.user.role, level=self.user.level) else: response["code"] = 400 response["response"] = "ERROR" wrap_response(self, response) return elif self.POST("action") == "update": if user: if self.POST("first_name"): user.first_name = self.POST("first_name") if self.POST("middle_name"): user.middle_name = self.POST("middle_name") if self.POST("last_name"): user.last_name = self.POST("last_name") if self.POST("role"): user.level = int(self.POST("role")) if user.level == 2: user.role = 'CLUSTERDIRECTOR' elif user.level == 3: user.role = 'AGENCYADMIN' elif user.level == 4: user.role = 'GEOSTOREADMIN' else: user.role = 'USER' if self.POST("email"): user.current_email = self.POST("email").strip().lower() user.email_list.append(user.current_email) if self.POST("access_key"): access_key = ["PUBLIC"] for key in self.POST("access_key").split(","): if key: access_key.append(key.upper().strip()) user.access_key = uniquify(access_key) user.put() msg = "User has been updated." success_message(self, msg) self.redirect("/dashboard")
def get(self, group_id=None): if self.user.role != 'CLUSTERDIRECTOR': self.redirect('/environment') return self.tv['page_user_groups'] = True if group_id: group = UserGroup.get_by_id(int(group_id)) if group: if self.GET("ta") == "join": if self.user.current_email in group.invited_users: if self.user.key in group.users: msg = "You are already a member of the " msg += group.title msg += " user group." error_message(self, msg) else: self.user.user_groups.append(str(group.key.id())) self.user.put() group.users.append(self.user.key) group.invited_users.remove(self.user.current_email) group.put() if group.environments: for environment in group.environments: environment = environment.get() if environment: if self.user.key not in environment.users: environment.users.append( self.user.key) environment.put() msg = "You have successfully joined the " msg += group.title msg += " user group." success_message(self, msg) else: msg = "Cannot find user group." error_message(self, msg) self.redirect("/groups") else: if self.user.role == "CLUSTERDIRECTOR": wrap_response(self, group.to_object()) else: self.redirect("/groups") else: if self.user.role == "CLUSTERDIRECTOR": wrap_response(self, {"error": "cannot find user group"}) else: self.redirect("/groups") else: query = UserGroup.query() if self.user.role == "CLUSTERDIRECTOR": query = query.filter(UserGroup.owner == self.user.key) else: query = query.filter( ndb.OR(UserGroup.users == self.user.key, UserGroup.invited_users == self.user.current_email)) groups = query.fetch() self.tv["user_groups"] = [] if groups: for g in groups: if self.GET('fetch'): if self.GET('environmentid'): environment = Environment.get_by_id( int(self.GET('environmentid'))) if self.GET('addusergroup'): if g.key not in environment.user_groups: self.tv["user_groups"].append( g.to_object()) elif self.GET('removeusergroup'): if g.key in environment.user_groups: self.tv["user_groups"].append( g.to_object()) else: self.tv["user_groups"].append(g.to_object()) if self.GET('fetch'): wrap_response(self, self.tv['user_groups']) return self.tv["breadcrumb"] = [{ "name": "User Groups", "link": "/groups" }] if self.user.role == "CLUSTERDIRECTOR": self.tv["show_new_group"] = True self.render("groups.html") else: self.render("groups-user.html")
def post(self, group_id=None): if group_id: response = {} response["code"] = 200 response["data"] = [] response["description"] = "" response["success"] = True group = UserGroup.get_by_id(int(group_id)) if group: if self.POST("action"): if self.POST("action") == "delete_invited_user": if self.POST("email").strip().lower( ) in group.invited_users: group.invited_users.remove( self.POST("email").strip().lower()) group.put() response[ 'description'] = 'Invitation to ' + self.POST( 'email').strip().lower( ) + ' has been cancelled.' response["data"] = group.to_object() elif self.POST("action") == "remove_member": user_key = ndb.Key('User', int(self.POST('user_id'))) if user_key in group.users: if group.environments: for environment in group.environments: environment = environment.get() if environment: if user_key in environment.users: environment.users.remove(user_key) environment.put() group.users.remove(user_key) group.put() response["data"] = group.to_object() response['description'] = 'User has been removed.' else: response['success'] = False response[ 'description'] = 'User is not a member of the user group.' elif self.POST("action") == "invite_users": if self.POST("email"): for email in self.POST("email").strip().split(","): email = email.strip().lower() query = User.query() query = query.filter( User.current_email == email) user = query.get() if user: user.user_groups.append(str( group.key.id())) user.put() group.users.append(user.key) else: group.invited_users.append(email) group.put() response["data"] = group.to_object() elif self.POST("action") == "leave_group": if self.user.key in group.users: group.users.remove(self.user.key) group.put() response["data"] = group.to_object() response[ "description"] = "You have successfully left the " + group.title.upper( ) + " user group." else: response["success"] = False response[ "description"] = "User is not part of the team." elif self.POST("action") == "update_group": if self.POST("group_name"): query = Teams.query() query = query.filter(Teams.team_name == self.POST( "group_name").strip().upper()) group2 = query.get() logging.info(group2) logging.info(group) if group2: if str(group2.key.id()) != str(group.key.id()): response["success"] = False response[ "description"] = "User group already exists." wrap_response(self, response) return group.title = self.POST( "group_name").strip().upper() if self.POST("group_description"): group.description = self.POST("group_description") group.put() response["data"] = group.to_object() wrap_response(self, response) else: if self.POST("group_name") \ and self.POST("group_description") \ and self.POST("group_member_emails"): # Create Environment # Only CLUSTERDIRECTOR role can create an environment if self.user.role != "CLUSTERDIRECTOR": msg = "You have insufficient rights to access this application." error_message(self, msg) self.redirect("/groups") return query = UserGroup.query() query = query.filter( UserGroup.title == self.POST("group_name").strip().upper()) group = query.get() if group: msg = "Could not create the user group. " msg += self.POST("group_name").strip() msg += " already exists." error_message(self, msg) else: group = UserGroup() group.title = self.POST("group_name").strip().upper() group.description = self.POST("group_description").strip() group.owner = self.user.key for email in self.POST("group_member_emails").split(","): email = email.strip().lower() query = User.query() query = query.filter(User.current_email == email) user = query.get() if user: group.users.append(user.key) else: group.invited_users.append(email) group.put() self.user.user_groups.append(str(group.key.id())) self.user.put() msg = "User group has been saved." success_message(self, msg) self.redirect("/groups")
def get(self): """ Handles the /register/verify endpoint. Verifies user registration. """ if self.user: self.redirect("/dashboard") else: if self.GET("token") and self.GET("uid"): user = User.get_by_id(int(self.GET("uid"))) logging.debug(user) if user: if user.status == "PENDING": if user.confirmation_token == self.GET("token"): user.status = "VERIFIED" user.put() # find teams and add it teams = Teams.query(Teams.invited_users == user.current_email).fetch(10) for team in teams: user.access_key.append(str(team.key.id())) user.teams.append(str(team.key.id())) user.put() team.members.append(str(user.key.id())) team.invited_users.remove(user.current_email) team.put() content = { "receiver_name": user.first_name, "receiver_email": user.current_email, "subject": "Account Verified", "email_type": "after_verify" } taskqueue.add(url="/tasks/email/send", params=content, method="POST") success = "Your account has been verified and pending approval. " success += "You will receive an email once your account is approved." success_message(self, success) if self.GET("r"): url = "/login/authorize?r=" url += urllib.quote(self.GET("r")) self.redirect(url) else: self.redirect("/login") else: msg = "You might have clicked a broken or expired link." error_message(msg) self.redirect("/register") elif user.status == "INVITE" and user.role == "OPENDATAADMIN": self.tv["token"] = self.GET("token") self.tv["uid"] = self.GET("uid") self.tv["email"] = user.current_email self.render("register-opendataadmin.html") elif user.status == "VERIFIED": success = "Your account is already verified and pending approval. " success += "You will receive an email once your account is approved." success_message(self, success) self.redirect("/login") else: error = "You may have clicked an expired link " error += "or mistyped the address." error_message(self, error) if self.GET("r"): url = "/login/authorize?r=" url += urllib.quote(self.GET("r")) self.redirect(url) else: self.redirect("/login") else: error = "Sorry, we couldn't process your request. " error += "Please try again." error_message(self, error) self.redirect("/register") else: self.redirect("/register")
def post(self): """ Handles the /password/reset endpoint. Resets password of the user. """ if self.POST("email"): email = self.POST("email").lower().strip() query = User.query() query = query.filter(User.current_email == email) user = query.get() if user: user.password_token = generate_token() user.put() content = { "token": user.password_token, "uid": str(user.key.id()), "receiver_name": user.first_name, "receiver_email": user.current_email, "subject": "Reset Password", "email_type": "password_reset" } taskqueue.add(url="/tasks/email/send", params=content, method="POST") success = "We sent an email to " success += self.POST("email") + ". Please open the " success += "email and click on the password reset link " success += "to reset your password." success_message(self, success) self.redirect("/password/reset") else: error = "Sorry, " + self.POST("email") error += " does not belong to an existing account." error_message(self, error) self.redirect("/password/reset") elif self.POST("new_password") and self.POST("confirm_password") \ and self.GET("uid") and self.GET("password_token"): if self.POST("new_password") == self.POST("confirm_password"): user = User.get_by_id(int(self.GET("uid"))) if user: if user.password_token == self.GET("password_token"): password = user.hash_password( self.POST("new_password")) user.password_token = generate_token() user.previous_passwords.append(password) user.password_update = datetime.datetime.now() user.hashed_password = password user.put() session = SessionHandler(user) session.login() code = session.generate_login_code() if self.POST("redirect"): self.redirect( urllib.unquote(str(self.POST("redirect")))) else: self.redirect("/dashboard") return else: error = "Sorry, your password reset request has expired." error += " Please create a new request." error_message(self, error) self.redirect("/password/reset") else: error = "Sorry, we couldn't process your request. " error += "Please try again." error_message(self, error) self.redirect("/password/reset") else: error = "Passwords do not match." error_message(self, error) url = "/password/reset?password_token=" + self.POST( "password_token") url += "&uid=" + self.POST("uid") self.redirect(url) else: error = "Please fill all required fields." error_message(self, error) self.redirect("/password/reset")