public async Task <IActionResult> AccountsLogins() { var failures = new List <string>(); TimeZoneInfo pst = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"); DateTime longAgo = new DateTime(1800, 1, 1); foreach (var member in await legacyDb.Accounts.Include(f => f.Logins).ToListAsync()) { string firstLoginProvider = member.Logins.OrderBy(f => f.Created).Select(f => f.Provider).FirstOrDefault(); string nameSpacer = (string.IsNullOrEmpty(member.LastName) || string.IsNullOrEmpty(member.FirstName)) ? string.Empty : " "; var user = new ApplicationUser { Id = member.Id.ToString(), UserName = member.Username?.Replace(" ", ".") ?? $"@{member.Id}-{firstLoginProvider}", Email = member.Email, MemberId = member.MemberId.ToString(), PasswordHash = member.PasswordHash, Created = new DateTimeOffset(member.Created ?? longAgo, pst.GetUtcOffset(member.Created ?? longAgo)) }; var result = await users.CreateAsync(user); if (!result.Succeeded) { failures.AddRange(result.Errors.Select(f => f.Description)); continue; } List <Claim> claims = new List <Claim> { new Claim(JwtClaimTypes.Name, $"{member.FirstName}{nameSpacer}{member.LastName}") }; if (!string.IsNullOrEmpty(member.FirstName)) { claims.Add(new Claim(JwtClaimTypes.GivenName, member.FirstName)); } if (!string.IsNullOrEmpty(member.LastName)) { claims.Add(new Claim(JwtClaimTypes.FamilyName, member.LastName)); } if (!string.IsNullOrEmpty(member.Email)) { claims.Add(new Claim(JwtClaimTypes.Email, member.Email)); } await users.AddClaimsAsync(user, claims); foreach (var login in member.Logins) { await users.AddLoginAsync(user, new Microsoft.AspNetCore.Identity.UserLoginInfo(login.Provider, login.ProviderId, login.Provider)); } if (member.Locked.HasValue) { await users.SetLockoutEndDateAsync(user, DateTimeOffset.UtcNow.AddYears(200)); } } return(Content("Done\n\n" + string.Join("\n", failures))); }
public async Task InvokeAsync(HttpContext context, RequestDelegate next) { var adminRoleId = "79AC1299-E87C-446C-AD69-ECDFE1A5EEA1"; var devRoleId = "B6275249-B4F1-4738-963D-7C40B23AF8B7"; var userRoleId = "7AAFEC08-0A31-4588-A290-62B12F888920"; if (await _dbContext.Permissions.CountAsync() == 0) { await _dbContext.Permissions.AddRangeAsync(new List <Permission> { new Permission { Name = "View" }, new Permission { Name = "Create" }, new Permission { Name = "Update" }, new Permission { Name = "Delete" }, new Permission { Name = "Upload" }, new Permission { Name = "Publish" } }); await _dbContext.SaveChangesAsync(); } if (await _dbContext.MenuItems.CountAsync() == 0) { var setupItem = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Setup", ParentId = null, Icon = "wrench", Url = null }); await _dbContext.SaveChangesAsync(); await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Users", ParentId = setupItem.Entity.Id, Icon = "user", Url = "/Setup/Users" }, new MenuItem { Title = "Security", ParentId = setupItem.Entity.Id, Icon = "lock", Url = "/Setup/Security" }, new MenuItem { Title = "Menu Management", ParentId = setupItem.Entity.Id, Icon = "menu", Url = "/Setup/Menu" } }); await _dbContext.SaveChangesAsync(); } if (await _roleManager.Roles.CountAsync() == 0) { await _roleManager.CreateAsync(new ApplicationRole { Id = adminRoleId, Name = "Administrator" }); await _roleManager.CreateAsync(new ApplicationRole { Id = userRoleId, Name = "User" }); } if (await _dbContext.RoleMenus.CountAsync() == 0) { await _dbContext.RoleMenus.AddRangeAsync(new List <ApplicationRoleMenu> { // Admin new ApplicationRoleMenu { RoleId = adminRoleId, MenuId = 1 }, new ApplicationRoleMenu { RoleId = adminRoleId, MenuId = 2 }, new ApplicationRoleMenu { RoleId = adminRoleId, MenuId = 3 }, new ApplicationRoleMenu { RoleId = adminRoleId, MenuId = 4 }, // User new ApplicationRoleMenu { RoleId = userRoleId, MenuId = 1 }, new ApplicationRoleMenu { RoleId = userRoleId, MenuId = 2 }, }); await _dbContext.SaveChangesAsync(); } var samId = "C316633F-7DE3-48BE-A117-807AF8F7BE70"; var ralphId = "102EBBD8-EFDA-462C-B7C2-1F5D88C80456"; // Create Sam var sam = await _userManager.FindByEmailAsync("*****@*****.**"); if (sam == null) { sam = new ApplicationUser { Id = samId, Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, LockoutEnabled = false, NormalizedUserName = "******", NormalizedEmail = "*****@*****.**", SecurityStamp = Guid.NewGuid().ToString() }; await _userManager.CreateAsync(sam, "Password123!"); await _userManager.AddToRoleAsync(sam, "Administrator"); } // Create Ralph var ralph = await _userManager.FindByEmailAsync("*****@*****.**"); if (ralph == null) { ralph = new ApplicationUser { Id = ralphId, Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, NormalizedUserName = "******", NormalizedEmail = "*****@*****.**", LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; await _userManager.CreateAsync(ralph, "Password456!"); await _userManager.AddToRoleAsync(ralph, "User"); } // If you want this by user, add the claims for each user. var samClaims = await _userManager.GetClaimsAsync(sam); if (samClaims.Count(y => y.Value == "View") == 0) { await _userManager.AddClaimsAsync(sam, new List <Claim> { new Claim(1.ToString(), "View"), new Claim(2.ToString(), "View"), new Claim(3.ToString(), "View"), new Claim(4.ToString(), "View") }); } var ralphClaims = await _userManager.GetClaimsAsync(ralph); if (ralphClaims.Count(y => y.Value == "View") == 0) { await _userManager.AddClaimsAsync(ralph, new List <Claim> { new Claim(1.ToString(), "View"), new Claim(2.ToString(), "View") }); } // Call the next delegate/middleware in the pipeline await next(context); }
public async Task InitBasedata() { var adminRoleId = "79AC1299-E87C-446C-AD69-ECDFE1A5EEA1"; var devRoleId = "B6275249-B4F1-4738-963D-7C40B23AF8B7"; var userRoleId = "7AAFEC08-0A31-4588-A290-62B12F888920"; if (await _dbContext.Permissions.CountAsync() == 0) { await _dbContext.Permissions.AddRangeAsync(new List <Permission> { new Permission { Name = "View" }, new Permission { Name = "Create" }, new Permission { Name = "Update" }, new Permission { Name = "Delete" }, new Permission { Name = "Upload" }, new Permission { Name = "Publish" } }); } if (await _dbContext.MenuItems.CountAsync() == 0) { var mainMenu = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Menu", ParentId = null, Icon = "fa fa-list", Url = null }); await _dbContext.SaveChangesAsync(); #region first child menu var baocaokiemnghiem = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Báo cáo kiểm nghiệm", ParentId = mainMenu.Entity.Id, Icon = "", Url = null }); var baocaosxtieuthu = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Báo cáo sx-tiêu thụ điện", ParentId = mainMenu.Entity.Id, Icon = "", Url = null }); var baocaotieuthuhoi = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Báo cáo sx-tiêu thụ hơi", ParentId = mainMenu.Entity.Id, Icon = "", Url = null }); var phanvisinh = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Báo cáo phân vi sinh (view)", Icon = null, ParentId = mainMenu.Entity.Id, Url = null }); var traodoithongtin = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Trao đổi thông tin", ParentId = mainMenu.Entity.Id, Icon = "fa fa-exchange", Url = null }); var theodoi = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Theo dõi", ParentId = mainMenu.Entity.Id, Icon = "fa fa-retweet", Url = null }); var nhapslieu = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Nhập số liệu", ParentId = mainMenu.Entity.Id, Icon = "fa fa-retweet", Url = null }); var Quantrihethong = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Quản trị hệ thống", ParentId = mainMenu.Entity.Id, Icon = "fa fa-cog", Url = null }); await _dbContext.SaveChangesAsync(); await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Báo cáo phân vi sinh (view)", Icon = null, ParentId = phanvisinh.Entity.Id, Url = "bao-cao-phan-vi-sinh" }); #endregion #region Báo cáo kiểm nghiệm await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Báo cáo phân tích ngày", Icon = null, ParentId = baocaokiemnghiem.Entity.Id, Url = baocaokiemnghiem.Entity.Url + "/bao-cao-phan-tich-ngay" }, new MenuItem { Title = "Báo cáo sản xuât theo ngày", Icon = null, ParentId = baocaokiemnghiem.Entity.Id, Url = baocaokiemnghiem.Entity.Url + "/bao-cao-san-xuat-theo-ngay" }, new MenuItem { Title = "Báo cáo SX thời đoạn", Icon = null, ParentId = baocaokiemnghiem.Entity.Id, Url = baocaokiemnghiem.Entity.Url + "/bao-cao-san-xuat-thoi-doan" }, new MenuItem { Title = "Báo cáo phân tích từ ngày đến ngày", Icon = null, ParentId = baocaokiemnghiem.Entity.Id, Url = baocaokiemnghiem.Entity.Url + "/bao-cao-phan-tich-tu-ngay-den-ngay" }, new MenuItem { Title = "Vẽ biểu đồ SPC", Icon = null, ParentId = baocaokiemnghiem.Entity.Id, Url = baocaokiemnghiem.Entity.Url + "/ve-bieu-do-spc" }, new MenuItem { Title = "Biểu đồ theo dõi sản xuất", Icon = null, ParentId = baocaokiemnghiem.Entity.Id, Url = baocaokiemnghiem.Entity.Url + "/bieu-do-theo-doi-san-xuat" } }); await _dbContext.SaveChangesAsync(); #endregion #region Báo cáo sx-tiêu thụ điện await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Báo cáo điện tự phát", Icon = null, ParentId = baocaosxtieuthu.Entity.Id, Url = baocaosxtieuthu.Entity.Url + "/bao-cao-dien-tu-phat" }, new MenuItem { Title = "Báo cáo EVN", Icon = null, ParentId = baocaosxtieuthu.Entity.Id, Url = baocaosxtieuthu.Entity.Url + "/bao-cao-EVN" }, new MenuItem { Title = "Điện tiêu thụ từng khu vực", Icon = null, ParentId = baocaosxtieuthu.Entity.Id, Url = baocaosxtieuthu.Entity.Url + "/dien-tieu-thu-tung-khu-vuc" }, }); await _dbContext.SaveChangesAsync(); #endregion #region Báo cáo sx-tiêu thụ hơi await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Báo cáo sản lượng hơi", Icon = null, ParentId = baocaotieuthuhoi.Entity.Id, Url = baocaotieuthuhoi.Entity.Url + "/bao-cao-san-luong-hoi" }, }); await _dbContext.SaveChangesAsync(); #endregion #region Trao đổi thông tin await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Thông báo Tất cả phòng ban, cá nhân", Icon = null, ParentId = traodoithongtin.Entity.Id, Url = traodoithongtin.Entity.Url + "/thong-bao-phong-ban-ca-nhan" }, new MenuItem { Title = "Thông báo ", Icon = null, ParentId = traodoithongtin.Entity.Id, Url = traodoithongtin.Entity.Url + "/thong-bao-chung" }, }); await _dbContext.SaveChangesAsync(); #endregion #region Theo dõi await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Thông tin nhanh(view)", Icon = null, ParentId = theodoi.Entity.Id, Url = theodoi.Entity.Url + "/thong-tin-nhanh" }, new MenuItem { Title = "Báo cáo hóa chất vật tư", Icon = null, ParentId = theodoi.Entity.Id, Url = theodoi.Entity.Url + "/bao-cao-hoa-chat-vat-tu" }, new MenuItem { Title = "Kiểm tra cân", Icon = null, ParentId = theodoi.Entity.Id, Url = theodoi.Entity.Url + "/kiem-tra-can" }, new MenuItem { Title = "Sổ giao ca", Icon = null, ParentId = theodoi.Entity.Id, Url = theodoi.Entity.Url + "/so-giao-ca" }, new MenuItem { Title = "Ghi nhận sự cố", Icon = null, ParentId = theodoi.Entity.Id, Url = theodoi.Entity.Url + "/ghi-nhan-su-co" }, }); await _dbContext.SaveChangesAsync(); #endregion #region Nhập số liệu #region first child menu var solieubancan = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Số liệu từ bàn cân mía", ParentId = nhapslieu.Entity.Id, Icon = "fa fa-retweet", Url = nhapslieu.Entity.Url + "/so-lieu-ban-can-mia" }); var dulieuBCP = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Dữ liệu BCP", ParentId = nhapslieu.Entity.Id, Icon = "fa fa-retweet", Url = nhapslieu.Entity.Url + "/du-lieu-BCP" }); var duongsanxuat = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Đường sản xuất", ParentId = nhapslieu.Entity.Id, Icon = "fa fa-retweet", Url = nhapslieu.Entity.Url + "/duong-san-xuat" }); var xulynuocthai = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Xử lý nước thải", ParentId = nhapslieu.Entity.Id, Icon = "fa fa-retweet", Url = nhapslieu.Entity.Url + "/xu-ly-nuoc-thai" }); var phongkiemnghiem = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Phòng kiểm nghiệm", ParentId = nhapslieu.Entity.Id, Icon = "fa fa-retweet", Url = nhapslieu.Entity.Url + "/phong-kiem-nghiem" }); await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Vật tư hóa chất", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/vat-tu-hoa-chat" }, new MenuItem { Title = "Dữ liệu SX- Tiêu thụ điện", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/du-lieu-sx-tieu-thu-dien" }, new MenuItem { Title = "Dữ liệu SX- Tiêu thụ hơi", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/du-lieu-sx-tieu-thu-hoi" }, new MenuItem { Title = "Phân vi sinh", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/phan-vi-sinh" }, new MenuItem { Title = "Thực hiện mục tiêu trong tháng", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/thuc-hien-muc-tieu-trong-thang" }, new MenuItem { Title = "Thực hiên mục tiêu trong vụ", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/thuc-hien-muc-tieu-trong-vu" }, new MenuItem { Title = "Thực hiện mục tiêu báo cáo ngày", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/thuc-hien-muc-tieu-bao-cao-ngay" }, new MenuItem { Title = "Định mức vật tư hóa chất", Icon = null, ParentId = nhapslieu.Entity.Id, Url = nhapslieu.Entity.Url + "/dinh-muc-vat-tu-hoat-chat" }, }); await _dbContext.SaveChangesAsync(); #endregion #region second child menu var miahanggio = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Mía hàng giờ", ParentId = solieubancan.Entity.Id, Icon = "fa fa-retweet", Url = solieubancan.Entity.Url + "/mia-hang-gio" }); var kiemtracan = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Kiểm tra cân", ParentId = solieubancan.Entity.Id, Icon = "fa fa-retweet", Url = solieubancan.Entity.Url + "/kiem-tra-can" }); await _dbContext.SaveChangesAsync(); #endregion await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Tồn mía trên sân", Icon = null, ParentId = dulieuBCP.Entity.Id, Url = dulieuBCP.Entity.Url + "/ton-mia-tren-can" }, new MenuItem { Title = "Các bồn bán chế phẩm", Icon = null, ParentId = dulieuBCP.Entity.Id, Url = dulieuBCP.Entity.Url + "/cac-bon-ban-che-pham" }, new MenuItem { Title = "Đường sản xuất", Icon = null, ParentId = dulieuBCP.Entity.Id, Url = dulieuBCP.Entity.Url + "/duong-san-xuat" }, }); await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Đường nhập kho", Icon = null, ParentId = duongsanxuat.Entity.Id, Url = duongsanxuat.Entity.Url + "/duong-nhan-kho" }, new MenuItem { Title = "Đường xuất kho", Icon = null, ParentId = duongsanxuat.Entity.Id, Url = duongsanxuat.Entity.Url + "/duong-xuat-kho" }, }); await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Lượng nước thải", Icon = null, ParentId = xulynuocthai.Entity.Id, Url = xulynuocthai.Entity.Url + "/luong-nuoc-thai" }, new MenuItem { Title = "Cân đóng bao", Icon = null, ParentId = xulynuocthai.Entity.Id, Url = xulynuocthai.Entity.Url + "/can-dong-bao" }, }); await _dbContext.SaveChangesAsync(); #region child menu phòng kiểm nghiệm var nhaplieubaocao = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Nhập số liệu báo cáo", ParentId = phongkiemnghiem.Entity.Id, Icon = "fa fa-retweet", Url = phongkiemnghiem.Entity.Url + "/nhap-so-lieu-bao-cao" }); var nhaplieuphantich = await _dbContext.MenuItems.AddAsync(new MenuItem { Title = "Nhập số liệu phân tích", ParentId = phongkiemnghiem.Entity.Id, Icon = "fa fa-retweet", Url = phongkiemnghiem.Entity.Url + "/nhap-so-lieu-phan-tich" }); await _dbContext.SaveChangesAsync(); await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Kết quả phân tích các bồn chế phẩm", Icon = null, ParentId = nhaplieubaocao.Entity.Id, Url = nhaplieubaocao.Entity.Url + "/ket-qua-phan-tich-cac-bon-CP" }, new MenuItem { Title = "Nhập số liệu làm Báo cáo ngày", Icon = null, ParentId = nhaplieubaocao.Entity.Id, Url = nhaplieubaocao.Entity.Url + "/nhap-so-lieu-bao-cao-ngay" }, new MenuItem { Title = "Nhập liệu mùa vụ trước", Icon = null, ParentId = nhaplieubaocao.Entity.Id, Url = nhaplieubaocao.Entity.Url + "/nhap-lieu-mua-vu-truoc" }, new MenuItem { Title = " Cấu hình giá trị các bồn bán chế phẩm", Icon = null, ParentId = nhaplieubaocao.Entity.Id, Url = nhaplieubaocao.Entity.Url + "/cau-hinh-gia-tri-cac-bon-BCP" }, }); await _dbContext.SaveChangesAsync(); await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Nhập dữ liệu ca", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/nhap-du-lieu-ca" }, new MenuItem { Title = "Nhập mía nước mía", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/nhap-mia-nuoc-mia" }, new MenuItem { Title = "Đường non tho", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/duong-non-tho" }, new MenuItem { Title = "Hóa chế luyện", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/hoa-che-luyen" }, new MenuItem { Title = "Đường sau sấy", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/duong-sau-say" }, new MenuItem { Title = "Đường thành phẩm", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/duong-thanh-pham" }, new MenuItem { Title = "Bùn", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/bun" }, new MenuItem { Title = "Phân tích RS", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/phan-tich-rs" }, new MenuItem { Title = "Nước lò", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/nuoc-lo" }, new MenuItem { Title = "Nước thải", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/nuoc-thai" }, new MenuItem { Title = "Làm AFF", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/lam-aff" }, new MenuItem { Title = "Đường thô nhập chuyền", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/duong-tho-nhap-chuyen" }, new MenuItem { Title = "Đường non luyện", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/duong-non-luyen" }, new MenuItem { Title = "Đường thô nhập kho", Icon = null, ParentId = nhaplieuphantich.Entity.Id, Url = nhaplieuphantich.Entity.Url + "/duong-tho-nhap-kho" }, }); await _dbContext.SaveChangesAsync(); #endregion #endregion #region Quản trị hệ thống await _dbContext.MenuItems.AddRangeAsync(new List <MenuItem> { new MenuItem { Title = "Danh mục chức năng", Icon = null, ParentId = Quantrihethong.Entity.Id, Url = Quantrihethong.Entity.Url + "/danh-muc-chuc-nang" }, new MenuItem { Title = "Công ty thành viên", Icon = null, ParentId = Quantrihethong.Entity.Id, Url = Quantrihethong.Entity.Url + "/cong-ty-thanh-vien" }, new MenuItem { Title = "Quản lý nhóm", Icon = null, ParentId = Quantrihethong.Entity.Id, Url = Quantrihethong.Entity.Url + "/quan-ly-nhom" }, new MenuItem { Title = "Xem vết ngươi dùng", Icon = null, ParentId = Quantrihethong.Entity.Id, Url = Quantrihethong.Entity.Url + "/xem-vet-nguoi-dung" }, new MenuItem { Title = "Cấu hình chức năng", Icon = null, ParentId = Quantrihethong.Entity.Id, Url = Quantrihethong.Entity.Url + "/cau-hinh-chuc-nang" }, new MenuItem { Title = "Cấu hình báo cáo", Icon = null, ParentId = Quantrihethong.Entity.Id, Url = Quantrihethong.Entity.Url + "/cau-hinh-bao-cao" }, new MenuItem { Title = "Cấu hình mùa vụ", Icon = null, ParentId = Quantrihethong.Entity.Id, Url = Quantrihethong.Entity.Url + "/cau-hinh-mua-vu" }, }); await _dbContext.SaveChangesAsync(); #endregion } if (await _roleManager.Roles.CountAsync() == 0) { await _roleManager.CreateAsync(new ApplicationRole { Id = adminRoleId, Name = "Administrator" }); await _roleManager.CreateAsync(new ApplicationRole { Id = userRoleId, Name = "User" }); } if (await _dbContext.RoleMenus.CountAsync() == 0) { await _dbContext.RoleMenus.AddRangeAsync(new List <ApplicationRoleMenu> { // Admin new ApplicationRoleMenu { RoleId = adminRoleId, MenuId = 1 }, //new ApplicationRoleMenu {RoleId=adminRoleId, MenuId=2}, //new ApplicationRoleMenu {RoleId=adminRoleId, MenuId=3}, //new ApplicationRoleMenu {RoleId=adminRoleId, MenuId=4}, // User new ApplicationRoleMenu { RoleId = userRoleId, MenuId = 1 }, //new ApplicationRoleMenu {RoleId=userRoleId, MenuId=2}, }); await _dbContext.SaveChangesAsync(); } var samId = "C316633F-7DE3-48BE-A117-807AF8F7BE70"; var ralphId = "102EBBD8-EFDA-462C-B7C2-1F5D88C80456"; // Create Sam var sam = await _userManager.FindByEmailAsync("*****@*****.**"); if (sam == null) { sam = new ApplicationUser { Id = samId, Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, LockoutEnabled = false, NormalizedUserName = "******", NormalizedEmail = "*****@*****.**", SecurityStamp = Guid.NewGuid().ToString() }; await _userManager.CreateAsync(sam, "Password123!"); await _userManager.AddToRoleAsync(sam, "Administrator"); } // Create Ralph var ralph = await _userManager.FindByEmailAsync("*****@*****.**"); if (ralph == null) { ralph = new ApplicationUser { Id = ralphId, Email = "*****@*****.**", UserName = "******", EmailConfirmed = true, NormalizedUserName = "******", NormalizedEmail = "*****@*****.**", LockoutEnabled = false, SecurityStamp = Guid.NewGuid().ToString() }; await _userManager.CreateAsync(ralph, "Password456!"); await _userManager.AddToRoleAsync(ralph, "User"); } // If you want this by user, add the claims for each user. var samClaims = await _userManager.GetClaimsAsync(sam); if (samClaims.Count(y => y.Value == "View") == 0) { await _userManager.AddClaimsAsync(sam, new List <Claim> { new Claim(1.ToString(), "View"), new Claim(2.ToString(), "View"), new Claim(3.ToString(), "View"), new Claim(4.ToString(), "View") }); } var ralphClaims = await _userManager.GetClaimsAsync(ralph); if (ralphClaims.Count(y => y.Value == "View") == 0) { await _userManager.AddClaimsAsync(ralph, new List <Claim> { new Claim(1.ToString(), "View"), new Claim(2.ToString(), "View") }); } // Call the next delegate/middleware in the pipeline // await next(context); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } var user = await _userManager.GetUserAsync(User); if (user == null) { return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'.")); } var claimsToUpdate = new List <Claim>(); var firstName = await _userManager.GetFirstNameAsync(user); if (Input.FirstName != firstName) { var setFirstNameResult = await _userManager.SetFirstNameAsync(user, Input.FirstName); if (!setFirstNameResult.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting first name for user with ID '{userId}'."); } } if (Input.FirstName != User.FindFirst(IdentityModel.JwtClaimTypes.GivenName)?.Value) { claimsToUpdate.Add(new Claim(IdentityModel.JwtClaimTypes.GivenName, Input.FirstName)); } var lastName = await _userManager.GetLastNameAsync(user); if (Input.LastName != lastName) { var setLastNameResult = await _userManager.SetLastNameAsync(user, Input.LastName); if (!setLastNameResult.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting lst name for user with ID '{userId}'."); } } if (Input.LastName != User.FindFirst(IdentityModel.JwtClaimTypes.FamilyName)?.Value) { claimsToUpdate.Add(new Claim(IdentityModel.JwtClaimTypes.FamilyName, Input.LastName)); } var email = await _userManager.GetEmailAsync(user); if (Input.Email != email) { var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email); if (!setEmailResult.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting email for user with ID '{userId}'."); } } var phoneNumber = await _userManager.GetPhoneNumberAsync(user); if (Input.PhoneNumber != phoneNumber) { var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); if (!setPhoneResult.Succeeded) { var userId = await _userManager.GetUserIdAsync(user); throw new InvalidOperationException($"Unexpected error occurred setting phone number for user with ID '{userId}'."); } } if (claimsToUpdate.Any()) { await _userManager.RemoveClaimsAsync(user, claimsToUpdate); await _userManager.AddClaimsAsync(user, claimsToUpdate); } await _signInManager.RefreshSignInAsync(user); StatusMessage = "Your profile has been updated"; return(RedirectToPage()); }
public async Task <IActionResult> EditUser(EditUserViewModel model, string buttonSubmit) { if (buttonSubmit == "Submit") { if (ModelState.IsValid) { var userToUpdate = await _userManager.FindByIdAsync(model.Id); // img if (!IsNull(model.UserImage)) { userToUpdate.UserImage = await ImageToBitArray(model.UserImage); } // details userToUpdate.FirstName = model.FirstName ?? userToUpdate.FirstName; userToUpdate.LastName = model.LastName ?? userToUpdate.LastName; userToUpdate.IsLocked = model.IsLocked; // casting to hashset to check if the claims passed for editing are the same as the ones the user had before the update request. var previousClaimValues = new HashSet <string>(model.PreviousGroups ?? new List <string>()); // If there were no previous groups, we just add them in. if (previousClaimValues.Count == 0) { await _userManager.AddClaimsAsync(userToUpdate, CreateClaimList(model.SelectedGroups, "Group")); } else if (!previousClaimValues.SetEquals(model.SelectedGroups)) { // we have a change so must redo user group claims. IdentityResult result = await _userManager.RemoveClaimsByTypeAsync(userToUpdate, "Group"); if (result.Succeeded) { var newClaims = CreateClaimList(model.SelectedGroups, "Group"); await _userManager.AddClaimsAsync(userToUpdate, newClaims); } } Permission newRole = model.UserRole; Permission currentRole = await getCurrentPermissionAsync(userToUpdate); if (!(currentRole == newRole)) { // if we get here, the previous role is changed and the new role is updated. await _userManager.RemoveFromRoleAsync(userToUpdate, currentRole.ToString()); await _userManager.AddToRoleAsync(userToUpdate, newRole.ToString()); } await _userManager.UpdateAsync(userToUpdate); return(RedirectToAction("Index")); } // There were errors return(await EditUser(model.Id)); } // User pressed cancel else { return(RedirectToAction("Index")); } }