def set_password(cls, user_name, passwd): pass_hash = sha256_crypt.encrypt(passwd) try: User.objects(username=user_name).update_one(set__password=pass_hash) Console.info("User password updated.") except: Console.error("Oops! Something went wrong while trying to set user password")
def set_user_status(cls, user_name, status): if user_name: try: User.objects(username=user_name).update_one(set__status=status) except: Console.error("Oops! Something went wrong while trying to amend user status") else: Console.error("Please specify the user to be amended")
def set_role(cls, user_name, role): try: if role in ROLE: User.objects(username=user_name).update_one(push__roles=role) Console.info("Role {0} added to user {1}".format(role, user_name)) else: Console.error("Please specify a valid role. Role {0} is invalid.".format(role)) except: Console.error("Oops! Something went wrong while trying to set user role.")
def get_unique_username(cls, proposal): """ Gets a unique username from a proposal. This is achieved while appending a number at the end. :param proposal: the proposed username :type proposal: String """ new_proposal = proposal.lower() num = 1 username = User.objects(username=new_proposal) while username.count() > 0: new_proposal = proposal + str(num) username = User.objects(username=new_proposal) num += 1 return new_proposal
def run(self): banner("Install Cloudmesh Management") obj = Mongo() obj.check_mongo() get_mongo_db("manage", DBConnFactory.TYPE_MONGOENGINE) install.run(self) banner("Adding Super User") users = Users() found = User.objects(username='******') if not found: data = User( status="approved", title="None", firstname="Super", lastname="User", email="*****@*****.**", username="******", active=True, password=sha256_crypt.encrypt("MyPassword"), phone="555-555-5555", department="IT", institution="IU", institutionrole="Other", address="IU", country="United States(US)", citizenship="United States(US)", bio="Manage Project Committee", url="http://cloudmesh.github.io/cloudmesh.html", advisor="None", confirm=sha256_crypt.encrypt("MyPassword"), projects=[], ) users.add(data)
def find(cls, email=None): """ Returns the users based on the given query. If no email is specified all users are returned. If the email is specified we search for the user with the given e-mail. :param email: email :type email: email address """ if email is None: return User.objects() else: found = User.objects(email=email) if found.count() > 0: return User.objects()[0] else: return None
def validate_email(cls, email): """ Verifies if the email of the user is not already in the users. :param email: email id of the user :return: true or false """ user = User.objects(email=email) valid = user.count() == 0 return valid
def __init__(self): # config = ConfigDict(filename=config_file("/cloudmesh_server.yaml")) # port = config['cloudmesh']['server']['mongo']['port'] # db = connect('manage', port=port) self.users = User.objects() db_name = get_mongo_dbname_from_collection("manage") if db_name: meta = {'db_alias': db_name}
def list_users(cls, display_fmt=None, username=None, status=None): # req_fields = ["username", "title", "firstname", "lastname", # "email", "phone", "url", "citizenship", # "institution", "institutionrole", "department", # "advisor", "address", "status", "projects"] req_fields = ["username", "firstname", "lastname", "email", "phone", "institution", "institutionrole", "advisor", "address", "status", "projects", "roles"] try: if username is None: if status: if status not in STATUS: Console.info("Invalid status requested.. Displaying all users..") user_json = User.objects.only(*req_fields).to_json() else: user_json = User.objects(status=status).only(*req_fields).to_json() else: user_json = User.objects.only(*req_fields).to_json() user_dict = json.loads(user_json) if user_dict: if display_fmt != 'json': cls.display(user_dict, username) else: cls.display_json(user_dict, username) else: Console.info("No users in the database.") else: user_json = User.objects(username=username).to_json() users_list = json.loads(user_json) for item in users_list: users_dict = item if users_dict: if display_fmt != 'json': cls.display_two_columns(users_dict) else: cls.display_json(users_dict, username) else: Console.error("User not in the database.") except: Console.error("Oops.. Something went wrong in the list users method " + sys.exc_info()[0])
def remove_project_reviewer(cls, project_id, username): if project_id: try: found = User.objects(username=username) if found.count() > 0: Project.objects(project_id=project_id).update_one(pull__reviewers=username) Console.info("User `{0}` removed as Reviewer.".format(username)) else: Console.error("Please specify a valid user") except: Console.error("Oops! Something went wrong while trying to remove project reviewer") else: Console.error("Please specify the project to be amended")
def get_user_status(cls, user_name): if user_name: try: user = User.objects(username=user_name).only('status') if user: for entry in user: return entry.status else: return except: Console.error("Oops! Something went wrong while trying to get user status") else: Console.error("Please specify the user get status")
def list_users_json(cls): # req_fields = ["username", "title", "firstname", "lastname", # "email", "phone", "url", "citizenship", # "institution", "institutionrole", "department", # "advisor", "address", "status", "projects"] req_fields = ["username", "firstname", "lastname", "email", "phone", "institution", "institutionrole", "advisor", "address", "status", "projects"] try: user_json = User.objects().to_json() return user_json except: Console.error("Oops.. Something went wrong in the list users method " + sys.exc_info()[0])
def delete_user(cls, user_name=None): if user_name: try: user = User.objects(username=user_name) if user: user.delete() if user_name != "super": Console.info("User " + user_name + " removed from the database.") else: Console.error("User with the name '{0}' does not exist.".format(user_name)) except: Console.error("Oops! Something went wrong while trying to remove a user") else: Console.error("Please specify the user to be removed")
def add_user(cls, user_name, project_id, role): """ Adds a member to the project. :param role: the role of the user :type role: String :param user_name: the username :type user_name: String :param project_id: the project id """ if role not in ROLES_LIST: Console.error("Invalid role `{0}`".format(role)) return """adds members to a particular project""" user = User.objects(username=user_name).first() project = Project.objects(project_id=project_id).first() if project: if user and role != 'alumni': if role == "member": Project.objects(project_id=project_id).update_one(push__members=user) User.objects(username=user_name).update_one(push__projects=project) Console.info("User `{0}` added as Project member.".format(user_name)) elif role == "lead": Project.objects(project_id=project_id).update_one(push__lead=user) Console.info("User `{0}` set as Lead.".format(user_name)) else: Console.error("Role `{0}` cannot be amended".format(role)) elif role == "alumni": Project.objects(project_id=project_id).update_one(push__alumnis=user_name) Console.info("User `{0}` added as Alumni.".format(user_name)) else: Console.error("The user `{0}` has not registered with Future Systems".format(user_name)) else: Console.error("The project `{0}` is not registered with Future Systems".format(project_id))
def remove_user(cls, user_name, project_id, role): if role not in ROLES_LIST: Console.error("Invalid role `{0}`".format(role)) return user = User.objects(username=user_name).first() if user and role != "alumni": if role == "member": Project.objects(project_id=project_id).update_one(pull__members=user) Console.info("User `{0}` removed as Project member.".format(user_name)) elif role == "lead": Project.objects(project_id=project_id).update_one(pull__lead=user) Console.info("User `{0}` removed as Project lead.".format(user_name)) elif role == "alumni": Project.objects(project_id=project_id).update_one(pull__alumnis=user) Console.info("User `{0}` removed as alumni.".format(user_name)) else: Console.error("The user `{0}` has not registered with Future Systems".format(user_name))
def get_current_user_role(): filename = path_expand("~/.cloudmesh/{0}/{1}".format("accounts", ".config")) with open(filename, 'r') as yamlfile: cfg = yaml.load(yamlfile) username = cfg['user'] db_name = get_mongo_dbname_from_collection("manage") if db_name: meta = {'db_alias': db_name} ## ## obj = Mongo() obj.check_mongo() ## ## get_mongo_db("manage", DBConnFactory.TYPE_MONGOENGINE) found = User.objects(username=username).only('roles').first() return found.roles pass
def __init__(self): obj = Mongo() obj.check_mongo() get_mongo_db("manage", DBConnFactory.TYPE_MONGOENGINE) self.projects = Project.objects() self.users = User.objects()
def check_exists(cls, user_name): return len(User.objects(username=user_name)) > 0