Beispiel #1
0
        public async Task <IActionResult> Delete(string id)
        {
            UniCoursesAppUser user = await userManager.FindByIdAsync(id);

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

                //Admins ne mozhat da se izbrishat
                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));
        }
Beispiel #2
0
        public async Task <IActionResult> Update(string id)
        {
            UniCoursesAppUser user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                return(View(user));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
Beispiel #3
0
        private async Task LoadAsync(UniCoursesAppUser user)
        {
            var userName = await _userManager.GetUserNameAsync(user);

            var phoneNumber = await _userManager.GetPhoneNumberAsync(user);

            Username = userName;

            Input = new InputModel
            {
                PhoneNumber = phoneNumber
            };
        }
Beispiel #4
0
        public async Task <IActionResult> Update(string id, string email, string password)
        {
            UniCoursesAppUser 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));
        }
Beispiel #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);
                UniCoursesAppUser appUser = new UniCoursesAppUser
                {
                    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));
        }
        public async Task <IActionResult> Index()
        {
            //return View();
            if (User.IsInRole("Admin"))
            {
                return(RedirectToAction("Index", "Courses"));
            }
            else if (User.IsInRole("Teacher"))
            {
                var userID             = userManager.GetUserId(User);
                UniCoursesAppUser user = await userManager.FindByIdAsync(userID);

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

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

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

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

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

            if (user == null)
            {
                var User = new UniCoursesAppUser();
                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");
                }
            }

            roleCheck = await RoleManager.RoleExistsAsync("Teacher");

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

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

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

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

            roleCheck = await RoleManager.RoleExistsAsync("Student");

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