// po kliknuti na startstudy ze z uzivatele stane student
        public async Task <IActionResult> IsStudent(int?id)
        {
            ApplicationUser user = await GetCurrentUserAsync();

            await _userExtensions.CreateRoleAddToUser("StudentCourse" + id, user); //student kurzu

            await _userExtensions.CreateRoleAddToUser("Student", user);            // pokud studuje alespon jeden kurz

            // seznam kurzu studenta
            var currentCourseId = await _context.Course.Where(x => x.Id == id).Select(x => x.Id).FirstOrDefaultAsync();

            var        userid = user.Id;
            CourseUser model  = new CourseUser();

            model.UserId   = userid;
            model.CourseId = currentCourseId;

            var courseUserList = _context.CourseUser.Add(model);

            _context.Update(user);
            await _context.SaveChangesAsync();

            await _signInManager.RefreshSignInAsync(user);

            await _context.SaveChangesAsync();

            return(RedirectToAction("Details", new { Id = id }));
        }
        public async Task <IActionResult> CompleteLesson(int id)
        {
            var user = await GetCurrentUserAsync();

            string userId   = user.Id;
            int    courseid = _context.Lesson.Where(x => x.Id == id).Select(x => x.Course.Id).FirstOrDefault();

            // pridani poctu hotovych lekci do courseuser
            CourseUser originalCourse = await _context.CourseUser.Where(x => x.CourseId == courseid && x.UserId == userId).FirstOrDefaultAsync();

            originalCourse.NumberOfFinishedLessons = originalCourse.NumberOfFinishedLessons + 1;

            //pridani hotovych lekci do FinishedUserLesson
            var lessonId = _context.Lesson.Where(x => x.Id == id).Select(x => x.Id).FirstOrDefault();

            //pridani role FinishedLesson
            await _userExtensions.CreateRoleAddToUser("FinishedLesson" + id, user);

            FinishedUserLesson polozkaLessonUser = new FinishedUserLesson();

            polozkaLessonUser.LessonId = lessonId;
            polozkaLessonUser.UserId   = userId;
            polozkaLessonUser.CourseId = originalCourse.CourseId;

            _context.Update(originalCourse);
            _context.Add(polozkaLessonUser);
            await _context.SaveChangesAsync();

            return(RedirectToAction("DetailsFinishedLesson", "Lessons", new { id = lessonId }));
        }
Exemple #3
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                string userName = user.UserName;
                if (userName == "*****@*****.**")
                {
                    //tento uzivatel dostane roli admin
                    await _userExtensions.CreateRoleAddToUser("Admin", user);
                }

                //pridani uzivateli role User
                await _userExtensions.CreateRoleAddToUser("User", user);

                //posila se email
                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
                    await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);

                    //await _signInManager.SignInAsync(user, isPersistent: false);
                    _logger.LogInformation("User created a new account with password.");

                    //return RedirectToLocal(returnUrl);
                    return(View("VerificationEmailSent"));
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }