public ActionResult Edit(int id, EditableApiAccessKeyViewModel editableViewModel)
        {
            if (ModelState.IsValid)
            {
                var selectedPermissions = GetSelectedItems(editableViewModel.SelectableApiPermissions).Select(x => (ApiAccessPermissionEnumModel)x);
                var apiAccessKeyModel   = Mapper.Map <ApiAccessKeyModel>(editableViewModel);

                var result = m_apiAccessKeyManager.UpdateApiAccessKey(id, apiAccessKeyModel, selectedPermissions);

                if (!result.HasError)
                {
                    return(RedirectToAction(nameof(View), new { id }));
                }

                ModelState.AddModelError(result.Error.Message);
            }

            var viewModel = ViewModelBuilder.BuildApiAccessKeyViewModel(ModelState, editableViewModel);

            return(View(viewModel));
        }
        public ActionResult Create(EditableApiAccessKeyViewModel editableViewModel)
        {
            if (ModelState.IsValid)
            {
                var selectedPermissions = GetSelectedItems(editableViewModel.SelectableApiPermissions).Select(x => (ApiAccessPermissionEnumModel)x);
                var apiAccessKeyModel   = Mapper.Map <ApiAccessKeyModel>(editableViewModel);

                apiAccessKeyModel.ApiKeyHash = m_hashManager.GenerateHash(editableViewModel.EditableApiAccessKeyHashViewModel.Value, editableViewModel.EditableApiAccessKeyHashViewModel.Algorithm);

                var result = m_apiAccessKeyManager.CreateApiAccessKey(apiAccessKeyModel, selectedPermissions);

                if (!result.HasError)
                {
                    return(RedirectToAction(nameof(View), new { id = result.Result }));
                }

                ModelState.AddModelError(result.Error.Message);
            }

            var vm = ViewModelBuilder.BuildApiAccessKeyViewModel(ModelState, editableViewModel);

            return(View(vm));
        }
        public EditableApiAccessKeyViewModel GetEditableApiAccessKeyViewModel(
            IList <SelectableViewModel <ApiAccessPermissionEnumViewModel> > selectableApiPermissions,
            ApiAccessKeyViewModel apiAccessKey = null)
        {
            var editableApiAccessKey = new EditableApiAccessKeyViewModel();

            editableApiAccessKey.EditableApiAccessKeyHashViewModel = new EditableApiAccessKeyHashViewModel();

            if (apiAccessKey != null)
            {
                editableApiAccessKey.Id          = apiAccessKey.Id;
                editableApiAccessKey.Name        = apiAccessKey.Name;
                editableApiAccessKey.Permissions = apiAccessKey.Permissions;

                if (apiAccessKey.Permissions != null)
                {
                    foreach (var selectableApiPermission in selectableApiPermissions)
                    {
                        foreach (var apiAccessPermission in apiAccessKey.Permissions)
                        {
                            if (selectableApiPermission.Item == apiAccessPermission)
                            {
                                selectableApiPermission.IsSelected = true;
                                break;
                            }
                        }
                    }
                }

                if (apiAccessKey.ApiAccessKeyHashViewModel != null)
                {
                    editableApiAccessKey.EditableApiAccessKeyHashViewModel.Algorithm = apiAccessKey.ApiAccessKeyHashViewModel.Algorithm;
                }
            }
            editableApiAccessKey.SelectableApiPermissions = selectableApiPermissions;
            return(editableApiAccessKey);
        }