public void DeleteMessage(int messageId) { using (var context = new PhobosCoreContext()) { var msg = context.UserMessages.First(x => x.Id == messageId); context.UserMessages.Remove(msg); context.SaveChanges(); } }
public void AddConfiguration(string key, string value) { using (var context = new PhobosCoreContext()) { var config = context.Configurations.FirstOrDefault(x => x.Key == key); if (config == default(Configuration)) { context.Configurations.Add(new Configuration() { Key = key, Value = value }); context.SaveChanges(); } } }
public UserRole CreateRole(string username) { using (var context = new PhobosCoreContext()) { var newUserRole = new UserRole(); newUserRole.Name = username; context.Roles.Add(newUserRole); context.SaveChanges(); return newUserRole; } }
public void DeleteFolder(string userName, int id) { using (var context = new PhobosCoreContext()) { var folder = context.UserMessageFolders.FirstOrDefault(x => x.Id == id && x.User.Username == userName); if (folder != null) { context.UserMessageFolders.Remove(folder); context.SaveChanges(); } } }
public UserMessageFolder CreateDefaultFolder(string userName) { using (var context = new PhobosCoreContext()) { var folder = new UserMessageFolder() { User = context.Users.Include(x => x.Roles).First(x => x.Username == userName), Name = InboxFolderName, IsInboxFolder = true }; context.UserMessageFolders.Add(folder); context.SaveChanges(); return folder; } }
public bool AddFailedLoginAttempt(string userName) { using (var context = new PhobosCoreContext()) { var selectedUser = context.Users.FirstOrDefault(x => x.Username == userName); if (selectedUser != default(UserAccount)) { selectedUser.FailedAttempts++; if (selectedUser.FailedAttempts > 3) { selectedUser.LockedDate = DateTime.Now; selectedUser.IsLocked = true; } context.SaveChanges(); return true; } return false; } }
public UserMessage SaveMessage(UserMessage sentMessage) { using (var context = new PhobosCoreContext()) { sentMessage.Owner = context.Users.Include(x => x.Roles).First(x => x.Username == sentMessage.Owner.Username); sentMessage.Sender = context.Users.Include(x => x.Roles).First(x => x.Username == sentMessage.Sender.Username); sentMessage.Receiver = context.Users.Include(x => x.Roles).First(x => x.Username == sentMessage.Receiver.Username); if (sentMessage.Folder == null && sentMessage.IsDraft) { sentMessage.Folder = context.UserMessageFolders.FirstOrDefault(x => x.User.Username == sentMessage.Owner.Username && x.Name == DraftFolderName); sentMessage.IsDraft = sentMessage.Folder.Name == DraftFolderName; } else if (sentMessage.Folder == null && !sentMessage.IsDraft) { sentMessage.Folder = context.UserMessageFolders.FirstOrDefault(x => x.User.Username == sentMessage.Receiver.Username && x.Name == InboxFolderName); } else if (sentMessage.Folder != null) { sentMessage.Folder = context.UserMessageFolders.FirstOrDefault(x => x.Id == sentMessage.Folder.Id); } if (sentMessage.Id == 0) { context.UserMessages.Add(sentMessage); } else { context.UserMessages.First(x => x.Id == sentMessage.Id).Message = sentMessage.Message; context.UserMessages.First(x => x.Id == sentMessage.Id).Title = sentMessage.Title; context.UserMessages.First(x => x.Id == sentMessage.Id).Receiver = sentMessage.Receiver; context.UserMessages.First(x => x.Id == sentMessage.Id).Sent = sentMessage.Sent; context.UserMessages.First(x => x.Id == sentMessage.Id).IsFavorite = sentMessage.IsFavorite; } context.SaveChanges(); return sentMessage; } }
public UserMessageFolder SaveFolder(UserMessageFolder model) { using (var context = new PhobosCoreContext()) { model.User = context.Users.Include(x => x.Roles).First(x => x.Username == model.User.Username); if (model.Id == 0 || !context.UserMessageFolders.Any(x => x.Id == model.Id)) { context.UserMessageFolders.Add(model); } else { context.UserMessageFolders.First(x => x.Id == model.Id).Icon = model.Icon; context.UserMessageFolders.First(x => x.Id == model.Id).IconColor = model.IconColor; context.UserMessageFolders.First(x => x.Id == model.Id).Name = model.Name; } context.SaveChanges(); return model; } }
public void MoveMessageToFolder(string userName, int msgId, int newFolderId) { using (var context = new PhobosCoreContext()) { var msg = context.UserMessages.First(x => x.Id == msgId && x.Owner.Username == userName); var newFolder = context.UserMessageFolders.Where(x => x.Id == newFolderId && x.User.Username == userName).First(); msg.Folder = newFolder; context.SaveChanges(); } }
public bool UnlockUserAccount(string userName) { using (var context = new PhobosCoreContext()) { var selectedUser = context.Users.FirstOrDefault(x => x.Username == userName); if (selectedUser != default(UserAccount)) { selectedUser.LockedDate = null; selectedUser.FailedAttempts = 0; selectedUser.IsLocked = false; context.SaveChanges(); return true; } return false; } }
public bool UpdateRoleUsers(string name, List<string> usersInRole) { using (var context = new PhobosCoreContext()) { var selectedRole = context.Roles.FirstOrDefault(x => x.Name == name); if (selectedRole != default(UserRole)) { selectedRole.UserAccounts.RemoveAll(x => !usersInRole.Any(z => z == x.Username)); foreach (var userName in usersInRole.Where(x => !selectedRole.UserAccounts.Any(z => z.Username == x))) { selectedRole.UserAccounts.Add(context.Users.FirstOrDefault(x => x.Username == userName)); } return context.SaveChanges() > 0; } return false; } }
public bool UpdateRole(UserRole role, string name) { using (var context = new PhobosCoreContext()) { var selectedRole = context.Roles.FirstOrDefault(x => x.Name == name); if (selectedRole != default(UserRole)) { selectedRole.Name = role.Name; context.SaveChanges(); return true; } return false; } }
public bool UpdateLastLoginDate(string userName, DateTime lastDate) { using (var context = new PhobosCoreContext()) { var selectedUser = context.Users.FirstOrDefault(x => x.Username == userName); if (selectedUser != default(UserAccount)) { selectedUser.LastLoginDate = lastDate; selectedUser.FailedAttempts = 0; selectedUser.IsLocked = false; selectedUser.CurrentStatus = Models.Enums.UserStatusEnum.Online; context.SaveChanges(); return true; } return false; } }
public bool UpdateAccount(UserAccount userAccount) { using (var context = new PhobosCoreContext()) { var selectedUser = context.Users.FirstOrDefault(x => x.Username == userAccount.Username); if (selectedUser != default(UserAccount)) { selectedUser.BirthDate = userAccount.BirthDate; selectedUser.CurrentStatus = userAccount.CurrentStatus; selectedUser.FirstName = userAccount.FirstName; selectedUser.LastName = userAccount.LastName; selectedUser.MemberSinceDate = userAccount.MemberSinceDate; selectedUser.Position = userAccount.Position; selectedUser.IsLocked = userAccount.IsLocked; selectedUser.LockedDate = userAccount.LockedDate; selectedUser.LastLoginDate = userAccount.LastLoginDate; context.SaveChanges(); return true; } return false; } }
public UserAccount CreateUser(string name, string userName, string password) { using (var context = new PhobosCoreContext()) { Configuration salt = CoreRepository.GetConfiguration("PasswordSalt"); var newUser = new UserAccount(); newUser.FirstName = name; newUser.Username = userName; newUser.Password = password; newUser.MemberSinceDate = DateTime.Now; newUser.Roles.AddRange(this.GetRolesForUser(context)); context.Users.Add(newUser); context.SaveChanges(); return newUser; } }
public UserMessageFolder GetDraftFolder(string userName) { using (var context = new PhobosCoreContext()) { var folder = context.UserMessageFolders .Include(x => x.User) .Include(x => x.User.Roles) .Where(x => x.User.Username == userName && x.Name == DraftFolderName) .FirstOrDefault(); if (folder == default(UserMessageFolder)) { folder = new UserMessageFolder() { User = context.Users.First(x => x.Username == userName), Name = DraftFolderName, IsDraftFolder = true }; context.UserMessageFolders.Add(folder); context.SaveChanges(); } return folder; } }
public bool DeleteRole(string name) { using (var context = new PhobosCoreContext()) { var selectedRole = context.Roles.FirstOrDefault(x => x.Name == name); context.Roles.Remove(selectedRole); return context.SaveChanges() > 0; } }