Example #1
0
        public async Task <IActionResult> Delete(string userId = "")
        {
            if (string.IsNullOrEmpty(userId))
            {
                throw new HttpBadRequestError();
            }

            var deletedAccount = (await AccountRepository.Records()).FirstOrDefault(item => item.DisplayName == "Deleted Account");

            if (deletedAccount is null)
            {
                deletedAccount = new DataModels.ApplicationUser {
                    DisplayName = "Deleted Account",
                    UserName    = Guid.NewGuid().ToString(),
                    Email       = Guid.NewGuid().ToString(),
                    AvatarPath  = string.Empty,
                    Birthday    = new DateTime(2000, 1, 1),
                    Registered  = new DateTime(2000, 1, 1),
                };

                DbContext.Users.Add(deletedAccount);
                DbContext.SaveChanges();
            }

            return(View("Delete", userId));
        }
Example #2
0
        public async Task <IActionResult> ConfirmDelete(string userId)
        {
            if (UserContext.ApplicationUser.Id != userId && !UserContext.IsAdmin)
            {
                throw new HttpForbiddenError();
            }

            var deletedAccount = AccountRepository.FirstOrDefault(item => item.DisplayName == "Deleted Account");

            if (deletedAccount is null)
            {
                deletedAccount = new DataModels.ApplicationUser {
                    DisplayName = "Deleted Account",
                    UserName    = Guid.NewGuid().ToString(),
                    Email       = Guid.NewGuid().ToString(),
                    AvatarPath  = string.Empty,
                    Birthday    = new DateTime(2000, 1, 1),
                    Registered  = new DateTime(2000, 1, 1),
                };

                DbContext.Users.Add(deletedAccount);
                DbContext.SaveChanges();
            }

            await AccountRepository.MergeAccounts(userId, deletedAccount.Id, true);

            return(RedirectToAction(nameof(Home.FrontPage), nameof(Home)));
        }
Example #3
0
        public async Task <IActionResult> Index()
        {
            var viewModel = new ViewModels.Roles.IndexPage();

            var roles = await RoleManager.Roles.OrderBy(r => r.Name).ToListAsync();

            foreach (var role in roles)
            {
                DataModels.ApplicationUser createdBy  = null;
                DataModels.ApplicationUser modifiedBy = null;

                if (role.CreatedById != null)
                {
                    createdBy = await UserManager.FindByIdAsync(role.CreatedById);
                }

                if (role.ModifiedById != null)
                {
                    modifiedBy = await UserManager.FindByIdAsync(role.ModifiedById);
                }

                IList <DataModels.ApplicationUser> usersInRole = null;

                try {
                    usersInRole = await UserManager.GetUsersInRoleAsync(role.Name);
                }
                catch (OperationCanceledException) {
                    continue;
                }

                viewModel.Roles.Add(new ViewModels.Roles.IndexRole {
                    Id            = role.Id,
                    Description   = role.Description,
                    Name          = role.Name,
                    CreatedBy     = createdBy?.DecoratedName,
                    Created       = role.CreatedDate.ToPassedTimeString(),
                    ModifiedBy    = modifiedBy?.DecoratedName,
                    Modified      = role.ModifiedDate.ToPassedTimeString(),
                    NumberOfUsers = usersInRole.Count()
                });
            }

            return(await ForumViewResult.ViewResult(this, viewModel));
        }
Example #4
0
        public ViewModels.Roles.EditPage GetEditPageModel(string id)
        {
            var role = RoleManager.FindByIdAsync(id).Result;

            if (role is null)
            {
                throw new HttpNotFoundError();
            }

            DataModels.ApplicationUser createdBy  = null;
            DataModels.ApplicationUser modifiedBy = null;

            if (role.CreatedById != null)
            {
                createdBy = UserManager.FindByIdAsync(role.CreatedById).Result;
            }

            if (role.ModifiedById != null)
            {
                modifiedBy = UserManager.FindByIdAsync(role.ModifiedById).Result;
            }

            var usersInRole = UserManager.GetUsersInRoleAsync(role.Name).Result;

            var viewModel = new ViewModels.Roles.EditPage {
                Id            = role.Id,
                Description   = role.Description,
                Name          = role.Name,
                CreatedBy     = createdBy?.DecoratedName,
                Created       = role.CreatedDate,
                ModifiedBy    = modifiedBy?.DecoratedName,
                Modified      = role.ModifiedDate,
                NumberOfUsers = usersInRole.Count()
            };

            return(viewModel);
        }
Example #5
0
        public async Task <PageViewModels.EditPage> GetEditPageModel(string id)
        {
            var role = await RoleManager.FindByIdAsync(id);

            if (role is null)
            {
                throw new HttpNotFoundError();
            }

            DataModels.ApplicationUser createdBy  = null;
            DataModels.ApplicationUser modifiedBy = null;

            if (role.CreatedById != null)
            {
                createdBy = await UserManager.FindByIdAsync(role.CreatedById);
            }

            if (role.ModifiedById != null)
            {
                modifiedBy = await UserManager.FindByIdAsync(role.ModifiedById);
            }

            var usersInRole = await UserManager.GetUsersInRoleAsync(role.Name);

            var viewModel = new PageViewModels.EditPage {
                Id            = role.Id,
                Description   = role.Description,
                Name          = role.Name,
                CreatedBy     = createdBy?.DisplayName,
                Created       = role.CreatedDate.ToPassedTimeString(),
                ModifiedBy    = modifiedBy?.DisplayName,
                Modified      = role.ModifiedDate.ToPassedTimeString(),
                NumberOfUsers = usersInRole.Count()
            };

            return(viewModel);
        }
Example #6
0
        public static void Initialize(IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.CreateScope())
            {
                // instantiating the DbContext (vs 'using' statement newing up var context - in v1.0)
                var context = serviceScope.ServiceProvider.GetService <ApplicationDbContext>();
                context.Database.EnsureCreated();

                var _userManager =
                    serviceScope.ServiceProvider.GetService <UserManager <ApplicationUser> >();
                var _roleManager =
                    serviceScope.ServiceProvider.GetService <RoleManager <IdentityRole> >();

                // If the admin, instructor, and student role is not yet created, create the roles.
                if (!_roleManager.RoleExistsAsync("Administrator").Result)
                {
                    var role = _roleManager.CreateAsync(
                        new IdentityRole {
                        Name = "Administrator"
                    }).Result;
                }

                if (!_roleManager.RoleExistsAsync("Instructor").Result)
                {
                    var role = _roleManager.CreateAsync(
                        new IdentityRole {
                        Name = "Instructor"
                    }).Result;
                }

                if (!_roleManager.RoleExistsAsync("Student").Result)
                {
                    var role = _roleManager.CreateAsync(
                        new IdentityRole {
                        Name = "Student"
                    }).Result;
                }


                // check for Administrator Account, if null, seed ApplicationUsers with single admin entry
                var adminUser = context.ApplicationUsers
                                .Where(a => a.Name == "Admin").FirstOrDefault();
                if (adminUser == null)
                {
                    adminUser = new DataModels.ApplicationUser()
                    {
                        Name           = "Admin",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        UserName       = "******",
                        Courses        = null,
                    };
                    var userResult = _userManager.CreateAsync(adminUser, "Test1!").Result;
                    _userManager.AddToRoleAsync(adminUser, "Administrator");
                    context.SaveChanges();
                }

                //Same, except with demo Instructor entry
                var demoInstructor = context.ApplicationUsers
                                     .Where(a => a.Name == "Instructor").FirstOrDefault();
                if (demoInstructor == null)
                {
                    demoInstructor = new DataModels.ApplicationUser()
                    {
                        Name           = "Instructor",
                        Email          = "*****@*****.**",
                        EmailConfirmed = true,
                        UserName       = "******",
                        Courses        = null,
                    };
                    var userResult = _userManager.CreateAsync(demoInstructor, "Test1!").Result;
                    _userManager.AddToRoleAsync(demoInstructor, "Instructor");
                    context.SaveChanges();
                }



                // demo students
                var student1 = new Student()
                {
                    Name = "Student 1", ImgUrl = "#", GPA = 2.5, SatisfactoryPerformance = false
                };
                var student2 = new Student()
                {
                    Name = "Student 2", ImgUrl = "#", GPA = 3.5, SatisfactoryPerformance = true
                };

                // if context has no students, add demos
                if (!context.Students.Any())
                {
                    context.Students.Add(student1);
                    context.Students.Add(student2);
                    context.SaveChanges();
                }

                // If context has no courses, add demo.

                var computerScience = new Course()
                {
                    CourseName = "Computer Science", Instructor = demoInstructor, Students = new List <Student>()
                    {
                        student1, student2
                    }
                };
                if (!context.Courses.Any())
                {
                    context.Courses.Add(computerScience);
                    context.SaveChanges();
                }
            }
        }