Ejemplo n.º 1
0
        public async Task <IActionResult> Delete(string id)
        {
            MVCFacultyUSER user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                IList <String> list = await userManager.GetRolesAsync(user);

                if (!list.Contains("Admin"))
                {
                    IdentityResult result = await userManager.DeleteAsync(user);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        Errors(result);
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }
            return(View("Index", userManager.Users));
        }
Ejemplo n.º 2
0
        private async Task LoadAsync(MVCFacultyUSER user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            Username = userName;

            Input = new InputModel
            {
                PhoneNumber = phoneNumber
            };
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Update(string id)
        {
            MVCFacultyUSER user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                return(View(user));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> Update(string id, string email, string password)
        {
            MVCFacultyUSER user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                if (!string.IsNullOrEmpty(email))
                {
                    user.Email = email;
                }
                else
                {
                    ModelState.AddModelError("", "Email cannot be empty");
                }

                if (!string.IsNullOrEmpty(password))
                {
                    user.PasswordHash = passwordHasher.HashPassword(user, password);
                }
                else
                {
                    ModelState.AddModelError("", "Password cannot be empty");
                }

                if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password))
                {
                    IdentityResult result = await userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        Errors(result);
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }
            return(View(user));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Create(User user)
        {
            if (ModelState.IsValid)
            {
                ViewData["Teachers"] = new SelectList(_context.Set <Teacher>(), "Id", "FullName", user.TeacherId);
                ViewData["Students"] = new SelectList(_context.Set <Teacher>(), "Id", "FullName", user.StudentId);
                MVCFacultyUSER appUser = new MVCFacultyUSER
                {
                    UserName  = user.UserName,
                    Email     = user.Email,
                    StudentId = user.StudentId,
                    TeacherId = user.TeacherId
                };

                IdentityResult result = await userManager.CreateAsync(appUser, user.Password);

                if (result.Succeeded)
                {
                    if (user.TeacherId != null)
                    {
                        var result1 = await userManager.AddToRoleAsync(appUser, "Teacher");
                    }
                    else if (user.StudentId != null)
                    {
                        var result2 = await userManager.AddToRoleAsync(appUser, "Student");
                    }
                    return(RedirectToAction("Index"));
                }

                else
                {
                    foreach (IdentityError error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }

            return(View(user));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> Index()
        {
            if (User.IsInRole("Admin"))
            {
                return(RedirectToAction("Index", "Courses"));
            }
            else if (User.IsInRole("Teacher"))
            {
                //Get TeacherId
                var            userID = userManager.GetUserId(User);
                MVCFacultyUSER user   = await userManager.FindByIdAsync(userID);

                return(RedirectToAction("CoursesByTeacher", "Courses", new { id = user.TeacherId }));
            }
            else if (User.IsInRole("Student"))
            {
                var            userID = userManager.GetUserId(User);
                MVCFacultyUSER user   = await userManager.FindByIdAsync(userID);

                return(RedirectToAction("MyEnrollments", "Enrollments", new { id = user.StudentId }));
            }
            return(View());
        }
Ejemplo n.º 7
0
        public static async Task CreateUserRoles(IServiceProvider serviceProvider)
        {
            var            RoleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();
            var            UserManager = serviceProvider.GetRequiredService <UserManager <MVCFacultyUSER> >();
            IdentityResult roleResult;

            //Add Admin Role
            var roleCheck = await RoleManager.RoleExistsAsync("Admin");

            if (!roleCheck)
            {
                roleResult = await RoleManager.CreateAsync(new IdentityRole("Admin"));
            }

            //Add Admin User
            MVCFacultyUSER user = await UserManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                var User = new MVCFacultyUSER();
                User.Email    = "*****@*****.**";
                User.UserName = "******";
                string         userPWD = "Admin123";
                IdentityResult chkUser = await UserManager.CreateAsync(User, userPWD);

                //Add default User to Role Admin
                if (chkUser.Succeeded)
                {
                    var result1 = await UserManager.AddToRoleAsync(User, "Admin");
                }
            }

            //Add Teacher Role
            roleCheck = await RoleManager.RoleExistsAsync("Teacher");

            if (!roleCheck)
            {
                roleResult = await RoleManager.CreateAsync(new IdentityRole("Teacher"));
            }

            user = await UserManager.FindByEmailAsync("*****@*****.**");

            if (user == null)
            {
                var User = new MVCFacultyUSER();
                User.Email     = "*****@*****.**";
                User.UserName  = "******";
                User.TeacherId = 1;
                string         userPWD = "Edna123";
                IdentityResult chkUser = await UserManager.CreateAsync(User, userPWD);

                if (chkUser.Succeeded)
                {
                    var result1 = await UserManager.AddToRoleAsync(User, "Teacher");
                }
            }

            //Add Student Role
            roleCheck = await RoleManager.RoleExistsAsync("Student");

            if (!roleCheck)
            {
                roleResult = await RoleManager.CreateAsync(new IdentityRole("Student"));
            }
        }