public UserDateView Invoke(int projectId, int userId) { var user = userRepository.GetById(userId); if (user == null) { throw new ArgumentNullException($"Could not find user with id: {projectId}."); } var userPolicy = user.UserPolicies.FirstOrDefault( m => (m.ProjectPolicies.ProjectId == projectId) && (m.ProjectPolicies.Policy == Policy.AccountExpirationDate)); if (userPolicy != null) { var result = (UserAccountExpirationDateConfiguration) jsonConvertService.DeserializeUser(Policy.AccountExpirationDate, userPolicy.SerializedUserPolicySetting); return(new UserDateView { UserId = userId, ProjectId = projectId, DateTime = result.ExpirationDate.ToString("dd-MM-yyyy") }); } return(new UserDateView { UserId = userId, ProjectId = projectId }); }
public void Invoke(int projectId, int userId, Policy policy, DateTime userExpDate) { var project = projectRepository.GetByIdWithPolicies(projectId); if (project == null) { throw new ArgumentNullException($"Cannot find user with id:{projectId}"); } var userPolicy = project.ProjectPolicies.FirstOrDefault(m => m.Policy == policy) .UserPolicies.FirstOrDefault(m => m.UserId == userId); if (userPolicy != null) { project.ProjectPolicies.FirstOrDefault(m => m.Policy == policy).UserPolicies.Remove(userPolicy); if (userExpDate == default(DateTime)) { projectRepository.Edit(project); return; } var result = (UserAccountExpirationDateConfiguration) jsonConvertService.DeserializeUser(policy, userPolicy.SerializedUserPolicySetting); result.ExpirationDate = userExpDate; userPolicy.SerializedUserPolicySetting = jsonConvertService.Serialize(result); project.ProjectPolicies.FirstOrDefault(m => m.Policy == policy).UserPolicies.Add(userPolicy); } else { if (userExpDate == default(DateTime)) { return; } userPolicy = new UserPolicies { UserId = userId, SerializedUserPolicySetting = jsonConvertService.Serialize(new UserAccountExpirationDateConfiguration { ExpirationDate = userExpDate }) }; project.ProjectPolicies.FirstOrDefault(m => m.Policy == policy).UserPolicies.Add(userPolicy); } projectRepository.Edit(project); }
public PolicyConfiguration Invoke(int userId, Policy policy, int projectId) { var user = userRepository.GetById(userId); if (user == null) { throw new ArgumentNullException($"Could not find user with id: {projectId}."); } var userPolicy = user.UserPolicies .FirstOrDefault( m => (m.ProjectPolicies.Policy == policy) && (m.ProjectPolicies.ProjectId == projectId)); if (userPolicy != null) { try { return(jsonConvertService.DeserializeUser(policy, userPolicy.SerializedUserPolicySetting)); } catch (Exception e) { throw new Exception( $"There was a problem with deserializing policy configurations of project with id: {projectId}, " + $"policy: {policy}, exception message: {e.Message}."); } } switch (policy) { case Policy.AccountExpirationDate: return(new UserAccountExpirationDateConfiguration { ExpirationDate = default(DateTime) }); default: throw new Exception( $"Wrong policy ({policy}) given to GetUserPolicyConfigurationOrDefaultFromProject."); } }