public OperationResult<OperationResults, object> UpdateUser(AdminUser user) { var output = new OperationResult<OperationResults, object>() { Result = OperationResults.Success }; using (var context = this.CreateMainContext()) { if (context.Connection.State != System.Data.ConnectionState.Open) context.Connection.Open(); try { context.Transaction = context.Connection.BeginTransaction(); var member = context.AdminUserMembershipRecords.Where(x => x.ID == user.ID.Value).SingleOrDefault(); if (member == null) return new OperationResult<OperationResults, object>() { Result = OperationResults.NotFound }; member.IsActive = user.IsActive; member.Login = user.Login; context.SubmitChanges(); var profile = context.AdminUserProfileRecords.Where(x => x.ID == member.ProfileID).Single(); profile.Email = user.Email; profile.FirstName = user.FirstName; profile.LastName = user.LastName; profile.MiddleName = user.MiddleName; context.SubmitChanges(); foreach (var item in member.AdminUserToRoleRecords) context.AdminUserToRoleRecords.DeleteOnSubmit(item); context.SubmitChanges(); context.AdminUserToRoleRecords.InsertOnSubmit(new AdminUserToRoleRecord() { MemberID = user.ID.Value, RoleID = (int)user.Roles }); context.SubmitChanges(); context.Transaction.Commit(); } catch (Exception ex) { output.Result = OperationResults.Failure; this.Logger.WriteError(ex); if (context.Transaction != null) context.Transaction.Rollback(); } return output; } }
/// <summary> /// Gets admin user info by his memebership identifier /// </summary> /// <param name="id"></param> /// <returns></returns> public OperationResult<OperationResults, AdminUser> GetUser(int id) { var result = new OperationResult<OperationResults, AdminUser>() { Result = OperationResults.Success }; using (var context = this.CreateMainContext()) { var query = from m in context.AdminUserMembershipRecords join p in context.AdminUserProfileRecords on m.ProfileID equals p.ID join m2r in context.AdminUserToRoleRecords on m.ID equals m2r.MemberID into roles where m.ID == id select new { member = m, profile = p, roles = roles }; var record = query.SingleOrDefault(); if (record == null) { result.Result = OperationResults.NotFound; return result; } var user = new AdminUser() { FirstName = record.profile.FirstName, Email = record.profile.Email, ID = id, IsActive = record.member.IsActive, LastName = record.profile.LastName, Login = record.member.Login, MiddleName = record.profile.MiddleName, Password = null }; foreach (var item in record.roles) user.Roles = user.Roles.HasValue ? (user.Roles.Value | (AdminUserRoles)item.RoleID) : (AdminUserRoles)item.RoleID; result.Output = user; } return result; }
/// <summary> /// /// </summary> /// <returns></returns> public OperationResult<OperationResults, List<AdminUser>> GetUsers() { using (var context = this.CreateMainContext()) { var query = from m in context.AdminUserMembershipRecords join p in context.AdminUserProfileRecords on m.ProfileID equals p.ID join m2r in context.AdminUserToRoleRecords on m.ID equals m2r.MemberID into roles select new { member = m, profile = p, roles = roles }; var users = new List<AdminUser>(); foreach (var item in query) { var user = new AdminUser() { ID = item.member.ID, FirstName = item.profile.FirstName, MiddleName = item.profile.MiddleName, LastName = item.profile.LastName, IsActive = item.member.IsActive, Email = item.profile.Email, Login = item.member.Login }; if (item.roles != null) { foreach (var irole in item.roles) user.Roles = user.Roles.HasValue ? (user.Roles.Value | (AdminUserRoles)irole.RoleID) : (AdminUserRoles)irole.RoleID; } users.Add(user); } return new OperationResult<OperationResults, List<AdminUser>>() { Result = OperationResults.Success, Output = users }; } }
public OperationResult<OperationResults, int?> CreateUser(int? creatorID, AdminUser user) { var output = new OperationResult<OperationResults, int?>() { Result = OperationResults.Success }; using (var context = this.CreateMainContext()) { if(context.Connection.State != System.Data.ConnectionState.Open) context.Connection.Open(); try { if (!this.CheckLogin(context, user.Login)) return new OperationResult<OperationResults, int?>() { Result = OperationResults.Failure }; context.Transaction = context.Connection.BeginTransaction(); var profile = new AdminUserProfileRecord() { FirstName = user.FirstName, MiddleName = user.MiddleName, LastName = user.LastName, Email = user.Email }; context.AdminUserProfileRecords.InsertOnSubmit(profile); context.SubmitChanges(); var member = new AdminUserMembershipRecord() { Login = user.Login, Password = this.PasswordUtility.HashPassword(user.Password, this.PasswordHashKey), ProfileID = profile.ID, IsActive = user.IsActive }; context.AdminUserMembershipRecords.InsertOnSubmit(member); context.SubmitChanges(); context.AdminUserToRoleRecords.InsertOnSubmit(new AdminUserToRoleRecord() { RoleID = (int)user.Roles.Value, MemberID = member.ID }); context.SubmitChanges(); context.Transaction.Commit(); output.Output = member.ID; } catch(Exception ex) { output.Result = OperationResults.Failure; this.Logger.WriteError(ex); if (context.Transaction != null) context.Transaction.Rollback(); } return output; } }