Exemple #1
0
        public async Task SetPreference(string userId, UserPreferenceType type, string value)
        {
            var existing = await _entities.UserPreferences.SingleOrDefaultAsync(x => x.UserId == userId && x.Key == type);

            var existed = existing != null;

            if (String.IsNullOrEmpty(value))
            {
                if (existed)
                {
                    _entities.UserPreferences.Remove(existing);
                    await _entities.SaveChangesSafe();
                }
            }
            else
            {
                existing = existing ?? new UserPreference {
                    UserId = userId, Key = type
                };
                existing.Value = value;
                if (!existed)
                {
                    _entities.UserPreferences.Add(existing);
                }
                await _entities.SaveChangesSafe();
            }
        }
Exemple #2
0
        protected virtual async Task <object> CreateOrUpdate(TEditorModel model, TEntity entity = null)
        {
            model = await PreProcess(model);

            var newItem = entity == null;

            if (newItem)
            {
                entity = Create(model);
                DbContext.Set <TEntity>().Add(entity);
            }
            else
            {
                entity = Update(model, entity);
            }
            entity = await PostProcess(entity);

            await DbContext.SaveChangesSafe();

            return(new IdResponse <TEntityKey> {
                Id = entity.Id
            });
        }
Exemple #3
0
        public async Task <IActionResult> Delete(int?id = null)
        {
            var userId = User.GetId();

            if (String.IsNullOrWhiteSpace(User.GetId()))
            {
                return(Unauthorized());
            }
            var notifications = DbContext.Notifications
                                .Where(x => x.UserId == userId);

            if (id != null)
            {
                notifications = notifications.Where(x => x.Id == id);
            }
            DbContext.Notifications.RemoveRange(notifications);
            await DbContext.SaveChangesSafe();

            return(Ok(1));
        }
Exemple #4
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View(nameof(ExternalLoginFailure)));
                }
                var user = new ApplicationUser {
                    UserName = model.UserName, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);

#if DEBUG
                        await _userManager.AddToRolesAsync(user, new[]
                        {
                            RoleConstants.Administrator,
                            RoleConstants.Contributor,
                            RoleConstants.BetaTester,
                            RoleConstants.Moderator,
                            RoleConstants.BoxUser,
                            RoleConstants.Donor,
                            RoleConstants.GCRAdmin,
                            RoleConstants.Contributor,
                            RoleConstants.BoxUser
                        });
#endif
#if RELEASE
                        await _userManager.AddToRolesAsync(user, new[]
                        {
                            RoleConstants.Contributor,
                            RoleConstants.BoxUser
                        });
#endif
                        _entities.UserProfiles.Add(new UserProfile
                        {
                            Id       = user.Id,
                            UserName = user.UserName
                        });
                        await _entities.SaveChangesSafe();

                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(model));
        }