Example #1
0
 public async Task<dynamic> GetUserCTDataAsync(ApplicationUser user) {
     var userBooksDto = await _db.GetUserBooksDtoAsync(user.Id, null);
     dynamic books = userBooksDto.Select(ub => new {
         id = ub.book,
         userBookId = ub.id,
         userId = ub.user,
         authorId = ub.BookDto.user,
         name = ub.BookDto.name,
         language = ub.BookDto.language,
         learnLevels = ub.learnLevels,
         learnDates = ub.learnDates,
         examDates = ub.examDates,
         promoteDates = ub.promoteDates,
         translations = ub.translations
     }).ToList();
     return new {
         isAuthenticated = true,
         languages = SupportedLanguages.AllDto,
         user = new {
             id = user.Id,
             name = user.DisplayName,
             language = ((LanguageType)user.NativeLanguage).ToString(),
             books = books
         }
     };
 }
Example #2
0
        //public static Tuple<int, int> GetBookData(int bookId) {
        //    var fields = new RedisValue[] { "size", "completed" };
        //    RedisValue[] values = Db.HashGet("book:" + bookId, fields);
        //    int bookSize;
        //    int bookCompleted;
        //    if (values[0].IsNull || values[1].IsNull ||
        //        !values[0].TryParse(out bookSize) || !values[1].TryParse(out bookCompleted)) {
        //        return null;
        //    }
        //    return new Tuple<int, int>(bookSize, bookCompleted);
        //}

        internal static List<UserBookDto> GetUserBooks(ApplicationUser user) {
            return null;
        }
 private async Task SignInAsync(ApplicationUser user, bool isPersistent) {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
 public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl, string loginProvider)
 {
     //if (User.Identity.IsAuthenticated)
     //{
     //    return RedirectToAction("User", "Home");
     //}
     LanguageType nativeLanguage;
     if (!Enum.TryParse(model.NativeLanguage, out nativeLanguage)) {
         ModelState.AddModelError("", "Invalid native language");
     }
     if (ModelState.IsValid)
     {
         // Получение сведений о пользователе от внешнего поставщика входа
         var info = await AuthenticationManager.GetExternalLoginInfoAsync();
         if (info == null)
         {
             return View("ExternalLoginFailure");
         }
         var user = await UserManager.FindByNameAsync(model.Email);
         if (user != null)
             ModelState.AddModelError("", string.Format("User with email {0} already exists.", model.Email));
         else
         {
             user = new ApplicationUser() { 
                 UserName = model.Email, 
                 Email = model.Email, 
                 DisplayName = model.DisplayName,
                 NativeLanguage = (int)nativeLanguage};
             var result = await UserManager.CreateAsync(user);
             if (result.Succeeded)
             {
                 result = await UserManager.AddLoginAsync(user.Id, info.Login);
                 if (result.Succeeded) {
                     await SignInAsync(user, true);
                     //await db.CreateFirstBookAsync(user.Id, nativeLanguage);
                     if (string.IsNullOrEmpty(returnUrl))
                         return RedirectToAction("Vocabulary", "Home");
                     else
                         return Redirect(returnUrl);
                 }
             }
             AddErrors(result);
         }
     }
     ViewBag.SupportedLanguages = SupportedLanguages.AllDto;
     ViewBag.loginProvider = loginProvider;
     ViewBag.ReturnUrl = returnUrl;
     return View(model);
 }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            LanguageType nativeLanguage;
            if (!Enum.TryParse(model.NativeLanguage, out nativeLanguage)) {
                ModelState.AddModelError("", "Invalid native language");
            }

            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { 
                    UserName = model.Email,
                    Email = model.Email,
                    DisplayName = model.DisplayName,
                    NativeLanguage = (int)nativeLanguage
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded) {
                    await SignInAsync(user, true);
                    //await db.CreateFirstBookAsync(user.Id, nativeLanguage);
                    return RedirectToAction("Vocabulary", "Home");
                } else {
                    AddErrors(result);
                }
            }

            // Появление этого сообщения означает наличие ошибки; повторное отображение формы
            ViewBag.SupportedLanguages = SupportedLanguages.AllDto;
            return View(model);
        }