/// <summary> /// Adds one user to the users table /// </summary> /// <param name="Head">Authorization header with current user information</param> /// <param name="User">User entity with the data to add</param> /// <exception cref="Chronos.Exceptions.ChronosException">Thrown when an unknown error occurs.</exception> /// <exception cref="Chronos.Exceptions.DataAlreadyExistsException">Thrown when the user already exists.</exception> /// <exception cref="Chronos.Exceptions.NotAuthorizedException">Throws this exception when the user is not allowed to use this function.</exception> public void AddUser(AuthHead Head, UserEntity User) { Head.CommandID = "CHRONOS.MODULES.CORE.SECURITY.USERSERVICE"; Head.CommandOptionID = "ADDUSER"; if (CanDoIt(Head)) { if ((from user in dataConn.Users where user.UserID == User.UserID select user).Count() == 0) { Users newUser = new Users(); newUser.UserID = User.UserID; newUser.Password = User.Password; newUser.Name = User.Name; newUser.EMail = User.EMail; dataConn.Users.Add(newUser); if (dataConn.SaveChanges() <= 0) { throw new ChronosException("It wasn't possible to add the new user."); } } else { throw new DataAlreadyExistsException(); } } else { throw new NotAuthorizedException(); } }
/// <summary> /// Obtains all available users in the system /// </summary> /// <param name="Head">Authorization header with current user information</param> /// <returns>UserEntity for the supplied ID</returns> /// <exception cref="Chronos.Exceptions.NotAuthorizedException">Throws this exception when the user is not allowed to use this function.</exception> public List<UserEntity> GetAllUsers(AuthHead Head) { Head.CommandID = "CHRONOS.MODULES.CORE.SECURITY.USERSERVICE"; Head.CommandOptionID = "GETALLUSERS"; if (CanDoIt(Head)) { List<UserEntity> lstReturn = new List<UserEntity>(); foreach (var varUser in dataConn.Users) { UserEntity userEntity = new UserEntity(); userEntity.UserID = varUser.UserID; userEntity.Name = varUser.Name; userEntity.EMail = varUser.EMail; userEntity.Password = varUser.Password; lstReturn.Add(userEntity); } return lstReturn; } else { throw new NotAuthorizedException(); } }
/// <summary> /// Obtains an user entity for the provided user ID /// </summary> /// <param name="UserID">The ID of the user to get</param> /// <returns>UserEntity for the supplied ID</returns> public UserEntity GetUser(string UserID) { var varUser = (from user in dataConn.Users where user.UserID == UserID select user).FirstOrDefault(); if (varUser == null) { return null; } else { UserEntity userReturn = new UserEntity(); userReturn.UserID = varUser.UserID; userReturn.Name = varUser.Name; userReturn.EMail = varUser.EMail; return userReturn; } }
/// <summary> /// Edits the provided user /// </summary> /// <param name="Head">Authorization header with current user information</param> /// <param name="User">User entity with the data to edit</param> /// <exception cref="Chronos.Exceptions.ChronosException">Thrown when an unknown error occurs.</exception> /// <exception cref="Chronos.Exceptions.NotAuthorizedException">Throws this exception when the user is not allowed to use this function.</exception> public void EditUser(AuthHead Head, UserEntity User) { Head.CommandID = "CHRONOS.MODULES.CORE.SECURITY.USERSERVICE"; Head.CommandOptionID = "EDITUSER"; if (CanDoIt(Head)) { var varUser = (from user in dataConn.Users where user.UserID == User.UserID select user).FirstOrDefault(); if (varUser != null) { varUser.Name = User.Name; varUser.Password = User.Password; varUser.EMail = User.EMail; if (dataConn.SaveChanges() <= 0) { throw new ChronosException("It wasn't possible to save the changes in the user."); } } else { throw new DataNotFoundException("The specified user do not exist."); } } else { throw new NotAuthorizedException(); } }