/// <summary> /// Get user from users base by its idNumber. /// </summary> /// <param name="userId">User's idNumber</param> /// <returns>User if exist, null pointer otherwise</returns> public static User GetUserById(int userId) { DataBase db = DataBase.GetInstance(); db.SetConnections(Constants.PATH); db.LoadUsers(); return(db.GetUserById(userId)); }
/// <summary> /// Deletes a user from the database /// </summary> /// <param name="userId">id of the user to delete</param> /// <returns>false if there is no such user in the database</returns> public static bool DeleteUser(int userId) { bool success = false; DataBase db = DataBase.GetInstance(); db.SetConnections(Constants.PATH); db.LoadUsers(); success = db.Users.Remove(db.GetUserById(userId)); db.CommitUsers(); return(success); }
/// <summary> /// Method adds new user to database /// </summary> /// <param name="newName">new user's name</param> /// <param name="newPassword">new user's password</param> /// <param name="rolesIdes">List of Ides of user's roles</param> /// <returns></returns> public static bool AddNewUser(string newName, string newPassword, List <int> rolesIdes) { if (CheckNameExistence(newName) == true) { return(false); } DataBase db = DataBase.GetInstance(); db.SetConnections(Constants.PATH); db.LoadUsers(); db.Add(new User(db.Users.Last().Id + 1, newName, newPassword, rolesIdes)); db.CommitUsers(); return(true); }
/// <summary> /// Checks if the current name is in the database /// </summary> /// <param name="nameToCheck">the name to search for</param> /// <returns>True if the name is found</returns> private static bool CheckNameExistence(string nameToCheck) { DataBase db = DataBase.GetInstance(); db.SetConnections(Constants.PATH); db.LoadUsers(); foreach (User user in db.Users) { if (user.Login == nameToCheck) { return(true); } } return(false); }
/// <summary> /// Return a list of users by the role /// </summary> /// <param name="roleId">The role's id to check by</param> /// <returns>A list of users by the role</returns> public static System.Collections.Generic.List <User> GetUsersByRole(int roleId) { DataBase db = DataBase.GetInstance(); db.SetConnections(Constants.PATH); db.LoadUsers(); List <User> toReturn = null; foreach (User user in db.Users) { if (user.RolesId.Contains(roleId)) { toReturn.Add(user); } } return(toReturn); }
/// <summary> /// This method performs logging in. /// </summary> /// <param name="forLogin">Login that the current user has entered.</param> /// <param name="forPassword">Password that the current user has entered.</param> /// <returns>True if user is logged in.</returns> public static User Identify(string forLogin, string forPassword) { User toReturn = null; DataBase db = DataBase.GetInstance(); db.SetConnections(Constants.PATH); db.LoadUsers(); foreach (User user in db.Users) { if (user.Login == forLogin && user.Password == forPassword) { toReturn = user; break; } } return(toReturn); }
/// <summary> /// Method removes user's role /// </summary> /// <param name="userId">User's idNumber</param> /// <param name="roleId">The role's id to remove</param> /// <returns>True if role was successfully removed </returns> public static bool RemoveUsersRole(int userId, int roleId) { DataBase db = DataBase.GetInstance(); db.SetConnections(Constants.PATH); db.LoadUsers(); foreach (User user in db.Users) { if (user.Id == userId) { user.RolesId.Remove(roleId); return(true); } } db.CommitUsers(); return(false); }