Exemple #1
0
        public async Task <ActionResult> Create(PageEditorVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(await ViewEditorFormAsync(vm));
            }

            try
            {
                var page = await _pages.CreateAsync(vm, User);

                await _db.SaveChangesAsync();

                await _search.AddPageAsync(page);

                _alarm.FireTreeLayoutRegenerationRequired();

                return(RedirectToSuccess("Страница создана"));
            }
            catch (ValidationException ex)
            {
                SetModelState(ex);
                return(await ViewEditorFormAsync(vm));
            }
        }
Exemple #2
0
        public async Task <ActionResult> Update(UserEditorVM vm)
        {
            if (!ModelState.IsValid)
            {
                return(await ViewUpdateFormAsync(vm));
            }

            try
            {
                if (vm.CreatePersonalPage && await _users.CanCreatePersonalPageAsync(vm))
                {
                    var page = await _pages.CreateDefaultUserPageAsync(vm, User);

                    vm.PersonalPageId     = page.Id;
                    vm.CreatePersonalPage = false;

                    await _search.AddPageAsync(page);
                }

                await _users.UpdateAsync(vm, User);

                await _db.SaveChangesAsync();

                return(RedirectToSuccess("Пользователь обновлен"));
            }
            catch (ValidationException ex)
            {
                SetModelState(ex);
                return(await ViewUpdateFormAsync(vm));
            }
        }
Exemple #3
0
        public async Task <ActionResult> Register(RegisterUserVM vm)
        {
            if (!await CanRegisterAsync())
            {
                return(View("RegisterDisabled"));
            }

            var info = Session.Get <RegistrationInfo>();

            if (info == null && !(await _auth.IsFirstUserAsync()))
            {
                return(RedirectToAction("Login"));
            }

            if (!ModelState.IsValid)
            {
                return(await ViewRegisterFormAsync(vm, usesPasswordAuth : info == null));
            }

            try
            {
                var result = await _auth.RegisterAsync(vm, info?.Login);

                if (!result.IsValidated)
                {
                    return(RedirectToAction("RegisterSuccess", "Auth"));
                }

                if (vm.CreatePersonalPage)
                {
                    _db.Entry(result.User).State = EntityState.Unchanged;
                    result.User.Page             = await _pages.CreateDefaultUserPageAsync(vm, result.Principal);

                    await _db.SaveChangesAsync();

                    await _search.AddPageAsync(result.User.Page);
                }

                return(RedirectToAction("Index", "Home"));
            }
            catch (ValidationException ex)
            {
                SetModelState(ex);

                return(await ViewRegisterFormAsync(vm, usesPasswordAuth : info == null));
            }
        }
Exemple #4
0
        /// <summary>
        /// Initializes the search index with test data.
        /// </summary>
        public async Task InitializeAsync()
        {
            var rootPath = Path.GetFullPath(Directory.GetCurrentDirectory());
            var wwwPath  = Path.Combine(rootPath, "wwwroot");

            if (Directory.Exists(wwwPath))
            {
                Directory.Delete(wwwPath, true);
            }
            await Search.ClearDataAsync();

            var seedPath = Path.Combine(rootPath, "..", "..", "..", "..", "Bonsai", "Data", "Utils", "Seed");
            await SeedData.EnsureSampleDataSeededAsync(Db, seedPath);

            await foreach (var page in Db.Pages)
            {
                await Search.AddPageAsync(page);
            }
        }
Exemple #5
0
        /// <summary>
        /// Reverts a change.
        /// </summary>
        public async Task RevertChangeAsync(Guid id, ClaimsPrincipal user)
        {
            var chg = await _db.Changes
                      .AsNoTracking()
                      .GetAsync(x => x.Id == id, "Правка не найдена");

            var isRemoving = string.IsNullOrEmpty(chg.OriginalState);

            switch (chg.Type)
            {
            case ChangesetEntityType.Media:
            {
                if (isRemoving)
                {
                    await _media.RemoveAsync(new RemoveMediaRequestVM { Id = chg.EditedMediaId.Value, RemoveFile = false }, user);
                }
                else
                {
                    var vm = JsonConvert.DeserializeObject <MediaEditorVM>(chg.OriginalState);
                    await _media.UpdateAsync(vm, user, id);
                }

                return;
            }

            case ChangesetEntityType.Page:
            {
                if (isRemoving)
                {
                    var page = await _pages.RemoveAsync(chg.EditedPageId.Value, user);

                    await _search.RemovePageAsync(page);
                }
                else
                {
                    var vm   = JsonConvert.DeserializeObject <PageEditorVM>(chg.OriginalState);
                    var page = await _pages.UpdateAsync(vm, user, id);

                    await _search.AddPageAsync(page);
                }
                return;
            }

            case ChangesetEntityType.Relation:
            {
                if (isRemoving)
                {
                    await _rels.RemoveAsync(chg.EditedRelationId.Value, user);
                }
                else
                {
                    var vm = JsonConvert.DeserializeObject <RelationEditorVM>(chg.OriginalState);
                    await _rels.UpdateAsync(vm, user, id);
                }

                return;
            }

            default:
                throw new ArgumentException($"Неизвестный тип сущности: {chg.Type}!");
            }
        }
Exemple #6
0
        /// <summary>
        /// Reverts a change.
        /// </summary>
        public async Task RevertChangeAsync(Guid id, ClaimsPrincipal user)
        {
            var(chg, prev) = await GetChangesetPairAsync(id, q => q.AsNoTracking());

            var isRemoving = string.IsNullOrEmpty(prev?.UpdatedState);

            switch (chg.EntityType)
            {
            case ChangesetEntityType.Media:
            {
                if (isRemoving)
                {
                    await _media.RemoveAsync(chg.EditedMediaId.Value, user);
                }
                else
                {
                    var vm = JsonConvert.DeserializeObject <MediaEditorVM>(prev.UpdatedState);
                    await _media.UpdateAsync(vm, user, id);
                }

                return;
            }

            case ChangesetEntityType.Page:
            {
                if (isRemoving)
                {
                    var pageId = chg.EditedPageId.Value;
                    await _pages.RemoveAsync(pageId, user);

                    await _search.RemovePageAsync(pageId);
                }
                else
                {
                    var vm     = JsonConvert.DeserializeObject <PageEditorVM>(prev.UpdatedState);
                    var pageId = vm.Id == Guid.Empty ? chg.EditedPageId.Value : vm.Id;     // workaround for a legacy bug
                    var page   = await _pages.UpdateAsync(vm, user, id, pageId);

                    await _search.AddPageAsync(page);
                }
                return;
            }

            case ChangesetEntityType.Relation:
            {
                if (isRemoving)
                {
                    await _rels.RemoveAsync(chg.EditedRelationId.Value, user);
                }
                else
                {
                    var vm = JsonConvert.DeserializeObject <RelationEditorVM>(prev.UpdatedState);
                    await _rels.UpdateAsync(vm, user, id);
                }

                return;
            }

            default:
                throw new ArgumentException($"Неизвестный тип сущности: {chg.EntityType}!");
            }
        }