2020-10-16 19:12:53 +02:00
|
|
|
package api
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-03-22 15:24:34 +01:00
|
|
|
"errors"
|
2020-10-16 11:41:56 +02:00
|
|
|
"fmt"
|
2021-08-10 04:57:45 +02:00
|
|
|
"io"
|
2020-10-16 11:41:56 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"path/filepath"
|
2022-01-10 21:33:05 +01:00
|
|
|
"runtime/debug"
|
2020-11-12 19:16:59 +01:00
|
|
|
"strconv"
|
2020-10-16 11:41:56 +02:00
|
|
|
"strings"
|
2020-12-07 20:40:16 +01:00
|
|
|
"time"
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/app"
|
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-06-11 14:24:51 +02:00
|
|
|
"github.com/mattermost/focalboard/server/services/audit"
|
2022-03-22 15:24:34 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/permissions"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/utils"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-16 11:41:56 +02:00
|
|
|
)
|
|
|
|
|
2021-02-03 01:54:15 +01:00
|
|
|
const (
|
2021-06-21 11:21:42 +02:00
|
|
|
HeaderRequestedWith = "X-Requested-With"
|
|
|
|
HeaderRequestedWithXML = "XMLHttpRequest"
|
2021-08-10 04:57:45 +02:00
|
|
|
UploadFormFileKey = "file"
|
2021-02-03 01:54:15 +01:00
|
|
|
)
|
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
const (
|
2022-03-22 15:24:34 +01:00
|
|
|
ErrorNoTeamCode = 1000
|
|
|
|
ErrorNoTeamMessage = "No team"
|
2021-03-26 19:01:54 +01:00
|
|
|
)
|
|
|
|
|
2021-07-09 03:09:02 +02:00
|
|
|
type PermissionError struct {
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pe PermissionError) Error() string {
|
|
|
|
return pe.msg
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
// ----------------------------------------------------------------------------------------------------
|
|
|
|
// REST APIs
|
|
|
|
|
2020-10-16 16:21:42 +02:00
|
|
|
type API struct {
|
2021-06-18 16:03:42 +02:00
|
|
|
app *app.App
|
2021-05-24 19:06:11 +02:00
|
|
|
authService string
|
2022-03-22 15:24:34 +01:00
|
|
|
permissions permissions.PermissionsService
|
2021-05-24 19:06:11 +02:00
|
|
|
singleUserToken string
|
|
|
|
MattermostAuth bool
|
2021-05-29 08:23:10 +02:00
|
|
|
logger *mlog.Logger
|
2021-06-11 14:24:51 +02:00
|
|
|
audit *audit.Audit
|
2020-10-16 16:21:42 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func NewAPI(app *app.App, singleUserToken string, authService string, permissions permissions.PermissionsService,
|
|
|
|
logger *mlog.Logger, audit *audit.Audit) *API {
|
2021-02-09 21:27:34 +01:00
|
|
|
return &API{
|
2021-06-18 16:03:42 +02:00
|
|
|
app: app,
|
2021-02-09 21:27:34 +01:00
|
|
|
singleUserToken: singleUserToken,
|
2021-03-26 19:01:54 +01:00
|
|
|
authService: authService,
|
2022-03-22 15:24:34 +01:00
|
|
|
permissions: permissions,
|
2021-05-29 08:23:10 +02:00
|
|
|
logger: logger,
|
2021-06-11 14:24:51 +02:00
|
|
|
audit: audit,
|
2021-02-09 21:27:34 +01:00
|
|
|
}
|
2020-10-16 16:21:42 +02:00
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
|
|
|
func (a *API) RegisterRoutes(r *mux.Router) {
|
2021-02-05 19:28:52 +01:00
|
|
|
apiv1 := r.PathPrefix("/api/v1").Subrouter()
|
2022-01-10 21:33:05 +01:00
|
|
|
apiv1.Use(a.panicHandler)
|
2021-02-05 19:28:52 +01:00
|
|
|
apiv1.Use(a.requireCSRFToken)
|
2020-12-07 20:40:16 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// Board APIs
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/boards", a.sessionRequired(a.handleGetBoards)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/boards/search", a.sessionRequired(a.handleSearchBoards)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/templates", a.sessionRequired(a.handleGetTemplates)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/boards", a.sessionRequired(a.handleCreateBoard)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}", a.attachSession(a.handleGetBoard, false)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}", a.sessionRequired(a.handlePatchBoard)).Methods("PATCH")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}", a.sessionRequired(a.handleDeleteBoard)).Methods("DELETE")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/duplicate", a.sessionRequired(a.handleDuplicateBoard)).Methods("POST")
|
2022-03-25 17:46:58 +01:00
|
|
|
apiv1.HandleFunc("/boards/{boardID}/blocks", a.attachSession(a.handleGetBlocks, false)).Methods("GET")
|
2022-03-22 15:24:34 +01:00
|
|
|
apiv1.HandleFunc("/boards/{boardID}/blocks", a.sessionRequired(a.handlePostBlocks)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/blocks", a.sessionRequired(a.handlePatchBlocks)).Methods("PATCH")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/blocks/{blockID}", a.sessionRequired(a.handleDeleteBlock)).Methods("DELETE")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/blocks/{blockID}", a.sessionRequired(a.handlePatchBlock)).Methods("PATCH")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/blocks/{blockID}/undelete", a.sessionRequired(a.handleUndeleteBlock)).Methods("POST")
|
2022-04-05 17:00:04 +02:00
|
|
|
apiv1.HandleFunc("/boards/{boardID}/blocks/{blockID}/duplicate", a.sessionRequired(a.handleDuplicateBlock)).Methods("POST")
|
GetBoardMetadata API (#2569)
* wip
* Added data migration for populating categories
* wip
* Added data migration for populating categories
* Store WIP
* migration WIP
* category CRUD APIs complete
* category block API WIP
* block category update API done
* Fetcehed data into store
* Started displayting sidebar data
* sidebar WIP
* Dashboard - basic changes
* Sidebar dashboard btn and board switcher UI only
* Sidebar dashboard btn and board switcher UI only
* create category dialog WIP
* Create category webapp side done
* Integrated move card to other category
* board to block
* Disabled dashboard route for now as we'll implement it in phase 2
* WIP
* Added logic to open last board/view on per team level
* Add workspace to teams and boards migrations (#1986)
* Add workspace to teams and boards migrations
* Update json annotations on board models
* boards search dialog WIP
* Seach dialog WIP
* Implemented opening boiard from search results
* Boards switcher styliung
* Handled update category WS event
* Template support
* personal server support and styling fixes
* test fix WIP
* Fixed a bug causing boards to not be moved correctly beteen categories
* Fixed webapp tests
* fix
* Store changes (#2011)
* Permissions phase 1 - Websocket updates (#2014)
* Store changes
* Websockets changes
* Permissions phase 1 - Permissions service (#2015)
* Store changes
* Websockets changes
* Permissions service
* Api and app updates (#2016)
* Store changes
* Websockets changes
* Permissions service
* New API and App changes
* Delete and Patch boards and blocks endpoints
* Used correct variable
* Webapp changes WIP
* Open correct team URL
* Fixed get block API
* Used React context for workspace users
* WIP
* On load navigation sorted out
* WIP
* Nav fix
* categories WS broadcast
* Used real search API
* Fixed unfurl ppreview
* set active team in sidebar
* IMplemented navigation on changing team in sidebar
* Misc fixes
* close rows inside transaction (#2045)
* update syntax for mysql (#2044)
* Upadted mutator for new patchBlock API
* Updated patchBlock API to use new URL
* Listeining to correct event in plugin mode
* Implemented WS messages for category operations:
* Fix duplicated build tags on Makefile
* Sidebar enhancements
* Add missing prefix to SQLite migration and fix flaky tests
* Sidebar boards menu enhancement
* Fix board page interactions (#2144)
* Fix patch board card properties error
* Fix board interactions
* Fix insert blocks interactions
* Fix app tests (#2104)
* Add json1 tag to vscode launch (#2157)
* Fix add, delete and update boards and add board patch generation (#2146)
* Fix update boards and add board patch generation
* Make add board and add template work, as well as deleting a board
* Update the state on board deletion
* Delete unused variable
* Fix bad parenthesis
* Fix board creation inside plugin, options were coming null due websocket message serialization
* update property type mutators to use boards API (#2168)
* Add permissions modal (#2196)
* Initial integration
* Permissions modal, websocket updates and API tests implemented
* Avoid updating/removing user if there is only one admin left
* Fix duplicates on board search
* Adds integration test
* Addressing PR review comments
Co-authored-by: Jesús Espino <jespinog@gmail.com>
* Merge
* I'm able to compile now
* Some fixes around tests execution
* Fixing migrations
* Fixing migrations order
* WIP
* Fixing some other compilation problems on tests
* Some typescript tests fixed
* Fixing javascript tests
* Fixing compilation
* Fixing some problems to create boards
* Load the templates on initial load
* Improvements over initial team templates import
* Adding new fields in the database
* Working on adding duplicate board api
* Removing RootID concept entirely
* Improving a bit the subscriptions
* Fixing store tests for notificationHints
* Fixing more tests
* fixing tests
* Fixing tests
* Fixing tests
* Fixing some small bugs related to templates
* Fixing registration link generation/regeneration
* Fixing cypress tests
* Adding store tests for duplicateBoard and duplicateBlock
* Addressing some TODO comments
* Making the export api simpler
* Add redirect component for old workspace urls
* Removing Dashboard code
* Delete only the built-in templates on update
* fixing tests
* Adding users autocompletion
* Updating snapshots
* Fixing bad merge
* fix panic when creating new card in notifysubscriptions (#2352)
* fix lint errors (#2353)
* fix lint errors
* fix panic when creating new card in notifysubscriptions (#2352)
* fix lint errors
* fix unit test
* Revert "fix unit test"
This reverts commit 0ad78aed65745521c0bb45790c9ea91b6c316c44.
Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
* fix sql syntax error for SearchUsersByTeam (#2357)
* Fix mentions delivery (#2358)
* fix sql syntax error for SearchUsersByTeam
* fix mentions delivery
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* update api for octoClient calls, pass correct variables to mutator (#2359)
* Fixing tests after merge
* Fix sidebar context menu UI issue (#2399)
* Fix notification diff for text blocks (#2386)
* fix notification diff for text blocks; fix various linter errors.
* fix URLs to cards
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Permissions branch: Fix card links (#2391)
* fix notification diff for text blocks; fix various linter errors.
* fix URLs to cards
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Fixing sqlite tests
* Fixing server tests
* Update migrations to create global templates. (#2397)
* fix duplicate templates
* revert migrate.go
* update UI for empty templates
* implement updating built-in templates as global (teamId = 0)
* handle error if board not found
* update unit test
* fix more tests
* Update blocks_test.go
Fix merge issue
* fix migration sql error (#2414)
* Fixing frontend tests
* Set target team ID when using a global template (#2419)
* Fix some server tests
* Fixing onboarding creation
* Permissions branch: Fix unit tests and CI errors (part 1) (#2425)
* Fixing some small memory leaks (#2400)
* Fixing some small memory leaks
* fixing tests
* passing the tags to all test targets
* Increasing the timeout of the tests
* Fix some type checkings
* Permissions branch: Fixes all the linter errors (#2429)
* fix linter errors
* Reestructuring the router and splitting in more subcomponents (#2403)
* Reestructuring the router and splitting in more subcomponents
* Removing console.log calls
* Removing unneeded selector
* Addressing PR comment
* Fix redirection to one team when you load directly the boards home path
* Using properly the lastTeamID to redirect the user if needed
* don't allow last admin change/deleted (#2416)
* don't allow last admin change/deleted
* update for i18-extract
* fixed en.json
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com>
* Splitting BoardPage component into simpler/smaller components (#2435)
* Splitting BoardPage component into simpler/smaller components
* Removing unneeded import
* Replace go migrate with morph permissions (#2424)
* merge origin/replace-go-migrate-with-morph
* run go mod tidy on mattermost-plugin and increase test timeout
* fix merge issue temprorarily
* remove some debug changes
* fixing the linter
* Allow always team 0 (global) templates fetch (#2472)
* Fix problem with viewId 0 in the URL (#2473)
* Migrate from binddata to goembed (#2471)
* Adding join logic to the board switcher (#2434)
* Adding join logic to the board switcher
* Using already existing client function and removing the joinBoard one
* Adding support for autojoin based on url
* Fixing frontend tests
* fix webapp compile error, missing enableSharedBoards (#2501)
* Fixing duplication on postgres
* Adding back views to the sidebar (#2494)
* Fix #2507. Update Swagger comments (#2508)
* Fix the flash of the template selector on board/team switch (#2490)
* Fix the flash of the template selector on board/team switch
* More fixes specially around error handling
* Fixing the bot badge (#2487)
* simplifying a bit the team store sync between channels and focalboard (#2481)
* Fix menu tests (#2528)
* fix failing menu tests
* fix lint error
* Added keyboard shortcut for boards switcher (#2407)
* Added keyboard shortcut for boards switcher
* Fixed a type error
* Added some inline comments
* Fixed lint
* Fixed bug with scroll jumping when the card is opened: (#2477)
- avoid remounting of `ScrollingComponent` for each render of `Kanban` component
- property `autoFocus` set to false for `CalculationOptions` because it triggers `blur` even for the button in Jest tests and closes the menu
- snapshots for tests with `CalculationOptions` updated
* Adding the frontend support for permissions and applying it to a big part of the interface. (#2536)
* Initial work on permissions gates
* Applying permissions gates in more places
* Adding more checks to the interface
* Adding more permissions gates and keeping the store up to date
* fixing some tests
* Fixing some more tests
* Fixing another test
* Fixing all tests and adding some more
* Adding no-permission snapshot tests
* Addressing PR review comments
* Fixing invert behavior
* Permissions branch: No sqlstore calls after app shutdown (#2530)
* fix webapp compile error, missing enableSharedBoards
* refactor app init wip
* - ensure all block change notifications are finished before shutting down app
- fix unit tests for mysql (insert_at only has 1 second resolution!)
* adjust logging
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Fixed migrations to allow upgrading from previous version (#2535)
* Added mechanism to check if schema migration is needed
* WIP
* WIP
* WIP
* WIP
* Fixed migration
* Fixed for SQLite
* minor cleaniup
* Deleted old schema migration table after running migrations
* Removed a debug log
* Fixed a bug where the code always tried to delete a table which may or may not exist
* Show properly the user avatar in the ShareBoard component (#2542)
* Fixing the last CI problems from the permissions-branch (#2541)
* Fix history ordering
* Giving some times to avoid possible race conditions
* Empty
* Reverting accidental change in the config.json
* Optimizing table view (#2540)
* Optimizing table view
* Reducing the amount of rendering for tables
* Some other performance improvements
* Improve the activeView updates
* Some extra simplifications
* Another small improvement
* Fixing tests
* Fixing linter errors
* Reducing a bit the amount of dependency with big objects in the store
* Small simplification
* Removing Commenter role from the user role selector (#2561)
* Shareboard cleanup (#2550)
* Initial work on permissions gates
* Applying permissions gates in more places
* Adding more checks to the interface
* Adding more permissions gates and keeping the store up to date
* fixing some tests
* Fixing some more tests
* Fixing another test
* Fixing all tests and adding some more
* Adding no-permission snapshot tests
* Addressing PR review comments
* cleanup some shareboard settings
* remove unused property, fix for user items being displayed for non admin
* revert change, allow users to show
Co-authored-by: Jesús Espino <jespinog@gmail.com>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* GetBoardMetadata API
* Integration tests. WIP
* getBoardHistory
* Working integration test
* Fix ordering, add store tests
* Fix: Update board_history update_at on patch
* sqltests
* Fix unmarshall delete boards_history
* testGetBlockMetadata with delete and undelete
* Handle board not found
* Fixing comments and cards with the new optimizations in the store (#2560)
* Fixing property creation (#2563)
* Cleanup
* Fix user selection in table view (#2565)
* Fixing focus new row in table view (#2567)
* Permissions branch: Fix sqlite table lock (CI) (#2568)
* fix sqlite table lock
* remove test db on teardown
* revert .gitignore
* fix goimport on migration code
* fix typo
* more linter fixes
* clean up tmp db for sqlstore tests
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Fixing snapshots
* Remove debug log
* Return metadata for deleted boards
* Migrating center panel to functional component (#2562)
* Migrating center panel to functional component
* Fixing some tests
* Fixing another test
* Fixing linter errors
* Fixing types errors
* Fixing linter error
* Fixing cypress tests
* Fixing the last cypress test
* Simpliying a bit the code
* Making property insertion more robust
* Updating checkbox test
* License check
* Cleanup and update Swagger docs
* Merge from main
* Fix bad merge
* Fix Linux-app build break
* do mod tidy
* Fix server lint
* Require credentials (not only read token)
* Add missing defer CloseRows
* do mod tidy
Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com>
Co-authored-by: Miguel de la Cruz <miguel@mcrx.me>
Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
Co-authored-by: Jesús Espino <jespinog@gmail.com>
Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com>
Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
Co-authored-by: kamre <eremchenko@gmail.com>
2022-03-29 23:47:49 +02:00
|
|
|
apiv1.HandleFunc("/boards/{boardID}/metadata", a.sessionRequired(a.handleGetBoardMetadata)).Methods("GET")
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
// Member APIs
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/members", a.sessionRequired(a.handleGetMembersForBoard)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/members", a.sessionRequired(a.handleAddMember)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/members/{userID}", a.sessionRequired(a.handleUpdateMember)).Methods("PUT")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/members/{userID}", a.sessionRequired(a.handleDeleteMember)).Methods("DELETE")
|
2022-03-29 10:14:33 +02:00
|
|
|
apiv1.HandleFunc("/boards/{boardID}/join", a.sessionRequired(a.handleJoinBoard)).Methods("POST")
|
2022-04-05 17:00:04 +02:00
|
|
|
apiv1.HandleFunc("/boards/{boardID}/leave", a.sessionRequired(a.handleLeaveBoard)).Methods("POST")
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
// Sharing APIs
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/sharing", a.sessionRequired(a.handlePostSharing)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/boards/{boardID}/sharing", a.sessionRequired(a.handleGetSharing)).Methods("GET")
|
|
|
|
|
|
|
|
// Team APIs
|
|
|
|
apiv1.HandleFunc("/teams", a.sessionRequired(a.handleGetTeams)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}", a.sessionRequired(a.handleGetTeam)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/regenerate_signup_token", a.sessionRequired(a.handlePostTeamRegenerateSignupToken)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/users", a.sessionRequired(a.handleGetTeamUsers)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/archive/export", a.sessionRequired(a.handleArchiveExportTeam)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/{boardID}/files", a.sessionRequired(a.handleUploadFile)).Methods("POST")
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2021-03-26 19:01:54 +01:00
|
|
|
// User APIs
|
2021-02-05 19:28:52 +01:00
|
|
|
apiv1.HandleFunc("/users/me", a.sessionRequired(a.handleGetMe)).Methods("GET")
|
2022-03-22 15:24:34 +01:00
|
|
|
apiv1.HandleFunc("/users/me/memberships", a.sessionRequired(a.handleGetMyMemberships)).Methods("GET")
|
2021-02-05 19:28:52 +01:00
|
|
|
apiv1.HandleFunc("/users/{userID}", a.sessionRequired(a.handleGetUser)).Methods("GET")
|
|
|
|
apiv1.HandleFunc("/users/{userID}/changepassword", a.sessionRequired(a.handleChangePassword)).Methods("POST")
|
2022-02-28 12:28:16 +01:00
|
|
|
apiv1.HandleFunc("/users/{userID}/config", a.sessionRequired(a.handleUpdateUserConfig)).Methods(http.MethodPut)
|
2020-11-06 16:46:35 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// BoardsAndBlocks APIs
|
|
|
|
apiv1.HandleFunc("/boards-and-blocks", a.sessionRequired(a.handleCreateBoardsAndBlocks)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/boards-and-blocks", a.sessionRequired(a.handlePatchBoardsAndBlocks)).Methods("PATCH")
|
|
|
|
apiv1.HandleFunc("/boards-and-blocks", a.sessionRequired(a.handleDeleteBoardsAndBlocks)).Methods("DELETE")
|
|
|
|
|
|
|
|
// Auth APIs
|
2021-02-05 19:28:52 +01:00
|
|
|
apiv1.HandleFunc("/login", a.handleLogin).Methods("POST")
|
2021-12-01 10:21:31 +01:00
|
|
|
apiv1.HandleFunc("/logout", a.sessionRequired(a.handleLogout)).Methods("POST")
|
2021-02-05 19:28:52 +01:00
|
|
|
apiv1.HandleFunc("/register", a.handleRegister).Methods("POST")
|
2021-09-01 23:53:27 +02:00
|
|
|
apiv1.HandleFunc("/clientConfig", a.getClientConfig).Methods("GET")
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// Category APIs
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/categories", a.sessionRequired(a.handleCreateCategory)).Methods(http.MethodPost)
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/categories/{categoryID}", a.sessionRequired(a.handleUpdateCategory)).Methods(http.MethodPut)
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/categories/{categoryID}", a.sessionRequired(a.handleDeleteCategory)).Methods(http.MethodDelete)
|
2021-02-05 19:45:28 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// Category Block APIs
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/categories", a.sessionRequired(a.handleGetUserCategoryBlocks)).Methods(http.MethodGet)
|
|
|
|
apiv1.HandleFunc("/teams/{teamID}/categories/{categoryID}/blocks/{blockID}", a.sessionRequired(a.handleUpdateCategoryBlock)).Methods(http.MethodPost)
|
2021-09-08 06:52:03 +02:00
|
|
|
|
2021-02-05 19:45:28 +01:00
|
|
|
// Get Files API
|
2022-04-05 17:00:04 +02:00
|
|
|
apiv1.HandleFunc("/files/teams/{teamID}/{boardID}/{filename}", a.attachSession(a.handleServeFile, false)).Methods("GET")
|
2021-12-10 16:46:37 +01:00
|
|
|
|
|
|
|
// Subscriptions
|
2022-03-22 15:24:34 +01:00
|
|
|
apiv1.HandleFunc("/subscriptions", a.sessionRequired(a.handleCreateSubscription)).Methods("POST")
|
|
|
|
apiv1.HandleFunc("/subscriptions/{blockID}/{subscriberID}", a.sessionRequired(a.handleDeleteSubscription)).Methods("DELETE")
|
|
|
|
apiv1.HandleFunc("/subscriptions/{subscriberID}", a.sessionRequired(a.handleGetSubscriptions)).Methods("GET")
|
2022-02-02 01:01:29 +01:00
|
|
|
|
2022-02-28 12:28:16 +01:00
|
|
|
// onboarding tour endpoints
|
2022-03-22 15:24:34 +01:00
|
|
|
apiv1.HandleFunc("/teams/{teamID}/onboard", a.sessionRequired(a.handleOnboard)).Methods(http.MethodPost)
|
2022-02-28 12:28:16 +01:00
|
|
|
|
2022-02-02 01:01:29 +01:00
|
|
|
// archives
|
2022-03-22 15:24:34 +01:00
|
|
|
apiv1.HandleFunc("/boards/{boardID}/archive/export", a.sessionRequired(a.handleArchiveExportBoard)).Methods("GET")
|
2022-03-31 20:04:20 +02:00
|
|
|
apiv1.HandleFunc("/teams/{teamID}/archive/import", a.sessionRequired(a.handleArchiveImport)).Methods("POST")
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2021-01-20 22:52:25 +01:00
|
|
|
func (a *API) RegisterAdminRoutes(r *mux.Router) {
|
2021-01-22 23:14:12 +01:00
|
|
|
r.HandleFunc("/api/v1/admin/users/{username}/password", a.adminRequired(a.handleAdminSetPassword)).Methods("POST")
|
2021-01-20 22:52:25 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func getUserID(r *http.Request) string {
|
|
|
|
ctx := r.Context()
|
|
|
|
session, ok := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return session.UserID
|
|
|
|
}
|
|
|
|
|
2022-01-10 21:33:05 +01:00
|
|
|
func (a *API) panicHandler(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer func() {
|
|
|
|
if p := recover(); p != nil {
|
|
|
|
a.logger.Error("Http handler panic",
|
|
|
|
mlog.Any("panic", p),
|
|
|
|
mlog.String("stack", string(debug.Stack())),
|
|
|
|
mlog.String("uri", r.URL.Path),
|
|
|
|
)
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", nil)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-02-05 19:28:52 +01:00
|
|
|
func (a *API) requireCSRFToken(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2021-02-03 01:54:15 +01:00
|
|
|
if !a.checkCSRFToken(r) {
|
2021-05-29 08:23:10 +02:00
|
|
|
a.logger.Error("checkCSRFToken FAILED")
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "checkCSRFToken FAILED", nil)
|
2021-02-03 01:54:15 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-05 19:28:52 +01:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
})
|
2021-02-03 01:54:15 +01:00
|
|
|
}
|
|
|
|
|
2021-09-01 23:53:27 +02:00
|
|
|
func (a *API) getClientConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
clientConfig := a.app.GetClientConfig()
|
|
|
|
|
|
|
|
configData, err := json.Marshal(clientConfig)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonBytesResponse(w, http.StatusOK, configData)
|
|
|
|
}
|
|
|
|
|
2021-02-03 01:54:15 +01:00
|
|
|
func (a *API) checkCSRFToken(r *http.Request) bool {
|
2021-06-21 11:21:42 +02:00
|
|
|
token := r.Header.Get(HeaderRequestedWith)
|
|
|
|
return token == HeaderRequestedWithXML
|
2021-02-03 01:54:15 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) hasValidReadTokenForBoard(r *http.Request, boardID string) bool {
|
2021-03-30 01:27:35 +02:00
|
|
|
query := r.URL.Query()
|
|
|
|
readToken := query.Get("read_token")
|
|
|
|
|
|
|
|
if len(readToken) < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
isValid, err := a.app.IsValidReadToken(boardID, readToken)
|
2021-03-30 01:27:35 +02:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.logger.Error("IsValidReadTokenForBoard ERROR", mlog.Err(err))
|
2021-03-30 01:27:35 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return isValid
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handleGetBlocks(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation GET /api/v1/boards/{boardID}/blocks getBlocks
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
|
|
|
// Returns blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2021-03-26 19:01:54 +01:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Board ID
|
2021-03-26 19:01:54 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 20:29:20 +01:00
|
|
|
// - name: parent_id
|
|
|
|
// in: query
|
|
|
|
// description: ID of parent block, omit to specify all blocks
|
|
|
|
// required: false
|
|
|
|
// type: string
|
|
|
|
// - name: type
|
|
|
|
// in: query
|
|
|
|
// description: Type of blocks to return, omit to specify all types
|
|
|
|
// required: false
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2021-02-17 20:29:20 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
query := r.URL.Query()
|
|
|
|
parentID := query.Get("parent_id")
|
|
|
|
blockType := query.Get("type")
|
2021-08-02 17:46:00 +02:00
|
|
|
all := query.Get("all")
|
2021-10-22 18:13:31 +02:00
|
|
|
blockID := query.Get("block_id")
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
hasValidReadToken := a.hasValidReadTokenForBoard(r, boardID)
|
|
|
|
if userID == "" && !hasValidReadToken {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusUnauthorized, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
board, err := a.app.GetBoard(boardID)
|
2021-03-26 19:01:54 +01:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "Board not found", nil)
|
2021-03-26 19:01:54 +01:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if !hasValidReadToken {
|
|
|
|
if board.IsTemplate && board.Type == model.BoardTypeOpen {
|
2022-04-01 21:42:30 +02:00
|
|
|
if board.TeamID != model.GlobalTeamID && !a.permissions.HasPermissionToTeam(userID, board.TeamID, model.PermissionViewTeam) {
|
2022-03-25 17:46:58 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board template"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "getBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.AddMeta("boardID", boardID)
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.AddMeta("parentID", parentID)
|
|
|
|
auditRec.AddMeta("blockType", blockType)
|
2021-08-02 17:46:00 +02:00
|
|
|
auditRec.AddMeta("all", all)
|
2021-10-22 18:13:31 +02:00
|
|
|
auditRec.AddMeta("blockID", blockID)
|
2021-06-11 14:24:51 +02:00
|
|
|
|
2021-08-02 17:46:00 +02:00
|
|
|
var blocks []model.Block
|
2021-10-22 18:13:31 +02:00
|
|
|
var block *model.Block
|
|
|
|
switch {
|
|
|
|
case all != "":
|
2022-03-22 15:24:34 +01:00
|
|
|
blocks, err = a.app.GetBlocksForBoard(boardID)
|
2021-08-02 17:46:00 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
2021-10-22 18:13:31 +02:00
|
|
|
case blockID != "":
|
2022-03-22 15:24:34 +01:00
|
|
|
block, err = a.app.GetBlockByID(blockID)
|
2021-10-22 18:13:31 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if block != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
if block.BoardID != boardID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-10-22 18:13:31 +02:00
|
|
|
blocks = append(blocks, *block)
|
|
|
|
}
|
|
|
|
default:
|
2022-03-22 15:24:34 +01:00
|
|
|
blocks, err = a.app.GetBlocks(boardID, parentID, blockType)
|
2021-08-02 17:46:00 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
a.logger.Debug("GetBlocks",
|
2022-03-22 15:24:34 +01:00
|
|
|
mlog.String("boardID", boardID),
|
2021-05-29 08:23:10 +02:00
|
|
|
mlog.String("parentID", parentID),
|
|
|
|
mlog.String("blockType", blockType),
|
2021-10-22 18:13:31 +02:00
|
|
|
mlog.String("blockID", blockID),
|
2021-05-29 08:23:10 +02:00
|
|
|
mlog.Int("block_count", len(blocks)),
|
|
|
|
)
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
json, err := json.Marshal(blocks)
|
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
2021-06-11 14:24:51 +02:00
|
|
|
|
|
|
|
auditRec.AddMeta("blockCount", len(blocks))
|
|
|
|
auditRec.Success()
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleCreateCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var category model.Category
|
|
|
|
|
|
|
|
err = json.Unmarshal(requestBody, &category)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "createCategory", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
|
|
|
|
// user can only create category for themselves
|
|
|
|
if category.UserID != session.UserID {
|
|
|
|
a.errorResponse(
|
|
|
|
w,
|
|
|
|
r.URL.Path,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
fmt.Sprintf("userID %s and category userID %s mismatch", session.UserID, category.UserID),
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
|
|
|
|
if category.TeamID != teamID {
|
|
|
|
a.errorResponse(
|
|
|
|
w,
|
|
|
|
r.URL.Path,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"teamID mismatch",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
createdCategory, err := a.app.CreateCategory(&category)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(createdCategory)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.AddMeta("categoryID", createdCategory.ID)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleUpdateCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
categoryID := vars["categoryID"]
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var category model.Category
|
|
|
|
err = json.Unmarshal(requestBody, &category)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "updateCategory", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
if categoryID != category.ID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "categoryID mismatch in patch and body", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
|
|
|
|
// user can only update category for themselves
|
|
|
|
if category.UserID != session.UserID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "user ID mismatch in session and category", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
if category.TeamID != teamID {
|
|
|
|
a.errorResponse(
|
|
|
|
w,
|
|
|
|
r.URL.Path,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"teamID mismatch",
|
|
|
|
nil,
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedCategory, err := a.app.UpdateCategory(&category)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, app.ErrorCategoryDeleted):
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", err)
|
|
|
|
case errors.Is(err, app.ErrorCategoryPermissionDenied):
|
2022-04-05 17:00:04 +02:00
|
|
|
// TODO: The permissions should be handled as much as possible at
|
|
|
|
// the API level, this needs to be changed
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", err)
|
|
|
|
default:
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(updatedCategory)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDeleteCategory(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
userID := session.UserID
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
categoryID := vars["categoryID"]
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "deleteCategory", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
deletedCategory, err := a.app.DeleteCategory(categoryID, userID, teamID)
|
|
|
|
if err != nil {
|
2022-04-05 17:00:04 +02:00
|
|
|
switch {
|
|
|
|
case errors.Is(err, app.ErrorInvalidCategory):
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
case errors.Is(err, app.ErrorCategoryPermissionDenied):
|
|
|
|
// TODO: The permissions should be handled as much as possible at
|
|
|
|
// the API level, this needs to be changed
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", err)
|
2022-04-05 17:00:04 +02:00
|
|
|
default:
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(deletedCategory)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetUserCategoryBlocks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getUserCategoryBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
categoryBlocks, err := a.app.GetUserCategoryBlocks(userID, teamID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(categoryBlocks)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleUpdateCategoryBlock(w http.ResponseWriter, r *http.Request) {
|
|
|
|
auditRec := a.makeAuditRecord(r, "updateCategoryBlock", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
categoryID := vars["categoryID"]
|
|
|
|
blockID := vars["blockID"]
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
// TODO: Check the category and the team matches
|
2022-03-22 15:24:34 +01:00
|
|
|
err := a.app.AddUpdateUserCategoryBlock(teamID, userID, categoryID, blockID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, []byte("ok"))
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handlePostBlocks(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation POST /api/v1/boards/{boardID}/blocks updateBlocks
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
2021-11-05 11:54:27 +01:00
|
|
|
// Insert blocks. The specified IDs will only be used to link
|
|
|
|
// blocks with existing ones, the rest will be replaced by server
|
|
|
|
// generated IDs
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2021-03-26 19:01:54 +01:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Board ID
|
2021-03-26 19:01:54 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 20:29:20 +01:00
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: array of blocks to insert or update
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2021-11-05 11:54:27 +01:00
|
|
|
// schema:
|
|
|
|
// items:
|
|
|
|
// $ref: '#/definitions/Block'
|
|
|
|
// type: array
|
2021-02-17 20:29:20 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
// in phase 1 we use "manage_board_cards", but we would have to
|
|
|
|
// check on specific actions for phase 2
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to make board changes"})
|
2021-03-26 19:01:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-16 19:12:53 +02:00
|
|
|
var blocks []model.Block
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-22 13:34:42 +02:00
|
|
|
err = json.Unmarshal(requestBody, &blocks)
|
2020-10-16 11:41:56 +02:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, block := range blocks {
|
|
|
|
// Error checking
|
|
|
|
if len(block.Type) < 1 {
|
2021-02-17 20:29:20 +01:00
|
|
|
message := fmt.Sprintf("missing type for block id %s", block.ID)
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
if block.CreateAt < 1 {
|
2021-02-17 20:29:20 +01:00
|
|
|
message := fmt.Sprintf("invalid createAt for block id %s", block.ID)
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
if block.UpdateAt < 1 {
|
2021-02-17 20:29:20 +01:00
|
|
|
message := fmt.Sprintf("invalid UpdateAt for block id %s", block.ID)
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
if block.BoardID != boardID {
|
|
|
|
message := fmt.Sprintf("invalid BoardID for block id %s", block.ID)
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2022-01-06 15:41:18 +01:00
|
|
|
blocks = model.GenerateBlockIDs(blocks, a.logger)
|
2021-11-05 11:54:27 +01:00
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "postBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
2021-07-08 16:36:43 +02:00
|
|
|
ctx := r.Context()
|
2021-07-09 03:09:02 +02:00
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
2022-02-28 12:28:16 +01:00
|
|
|
|
|
|
|
model.StampModificationMetadata(userID, blocks, auditRec)
|
2021-07-08 16:36:43 +02:00
|
|
|
|
2022-02-04 23:12:28 +01:00
|
|
|
// this query param exists when creating template from board, or board from template
|
2022-02-02 18:12:59 +01:00
|
|
|
sourceBoardID := r.URL.Query().Get("sourceBoardID")
|
|
|
|
if sourceBoardID != "" {
|
2022-03-22 15:24:34 +01:00
|
|
|
if updateFileIDsErr := a.app.CopyCardFiles(sourceBoardID, blocks); updateFileIDsErr != nil {
|
2022-02-02 18:12:59 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", updateFileIDsErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
newBlocks, err := a.app.InsertBlocks(blocks, session.UserID, true)
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
a.logger.Debug("POST Blocks", mlog.Int("block_count", len(blocks)))
|
2021-11-05 11:54:27 +01:00
|
|
|
|
|
|
|
json, err := json.Marshal(newBlocks)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
2021-06-11 14:24:51 +02:00
|
|
|
|
|
|
|
auditRec.AddMeta("blockCount", len(blocks))
|
|
|
|
auditRec.Success()
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2022-02-28 12:28:16 +01:00
|
|
|
func (a *API) handleUpdateUserConfig(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation PATCH /api/v1/users/{userID}/config updateUserConfig
|
|
|
|
//
|
|
|
|
// Updates user config
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: userID
|
|
|
|
// in: path
|
|
|
|
// description: User ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: User config patch to apply
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/UserPropPatch"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
2022-03-22 15:24:34 +01:00
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2022-02-28 12:28:16 +01:00
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var patch *model.UserPropPatch
|
|
|
|
err = json.Unmarshal(requestBody, &patch)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userID := vars["userID"]
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "updateUserConfig", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
// a user can update only own config
|
|
|
|
if userID != session.UserID {
|
2022-04-05 17:00:04 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", nil)
|
2022-02-28 12:28:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedConfig, err := a.app.UpdateUserConfig(userID, *patch)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(updatedConfig)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2020-12-07 20:40:16 +01:00
|
|
|
func (a *API) handleGetUser(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 20:29:20 +01:00
|
|
|
// swagger:operation GET /api/v1/users/{userID} getUser
|
|
|
|
//
|
|
|
|
// Returns a user
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: userID
|
|
|
|
// in: path
|
|
|
|
// description: User ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-12-07 20:40:16 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
userID := vars["userID"]
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "postBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("userID", userID)
|
|
|
|
|
2021-06-18 16:03:42 +02:00
|
|
|
user, err := a.app.GetUser(userID)
|
2020-12-07 20:40:16 +01:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-12-07 20:40:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userData, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-12-07 20:40:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
jsonBytesResponse(w, http.StatusOK, userData)
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.Success()
|
2020-12-07 20:40:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetMe(w http.ResponseWriter, r *http.Request) {
|
2021-02-17 20:29:20 +01:00
|
|
|
// swagger:operation GET /api/v1/users/me getMe
|
|
|
|
//
|
|
|
|
// Returns the currently logged-in user
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
userID := getUserID(r)
|
|
|
|
|
2020-12-07 20:40:16 +01:00
|
|
|
var user *model.User
|
|
|
|
var err error
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "getMe", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if userID == model.SingleUser {
|
2021-10-07 13:51:01 +02:00
|
|
|
now := utils.GetMillis()
|
2020-12-07 20:40:16 +01:00
|
|
|
user = &model.User{
|
2022-02-02 01:01:29 +01:00
|
|
|
ID: model.SingleUser,
|
|
|
|
Username: model.SingleUser,
|
|
|
|
Email: model.SingleUser,
|
2020-12-07 20:40:16 +01:00
|
|
|
CreateAt: now,
|
|
|
|
UpdateAt: now,
|
|
|
|
}
|
|
|
|
} else {
|
2022-03-22 15:24:34 +01:00
|
|
|
user, err = a.app.GetUser(userID)
|
2020-12-07 20:40:16 +01:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-12-07 20:40:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
userData, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-12-07 20:40:16 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
jsonBytesResponse(w, http.StatusOK, userData)
|
2021-06-11 14:24:51 +02:00
|
|
|
|
|
|
|
auditRec.AddMeta("userID", user.ID)
|
|
|
|
auditRec.Success()
|
2020-12-07 20:40:16 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleGetMyMemberships(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/users/me/memberships getMyMemberships
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
2022-03-22 15:24:34 +01:00
|
|
|
// Returns the currently users board memberships
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-03-22 15:24:34 +01:00
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/BoardMember"
|
2021-02-17 20:29:20 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
userID := getUserID(r)
|
2021-01-12 20:16:25 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec := a.makeAuditRecord(r, "getMyBoardMemberships", audit.Fail)
|
|
|
|
auditRec.AddMeta("userID", userID)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
members, err := a.app.GetMembersForUser(userID)
|
2021-03-26 19:01:54 +01:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2021-03-26 19:01:54 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
membersData, err := json.Marshal(members)
|
2020-10-16 16:21:42 +02:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-10-16 16:21:42 +02:00
|
|
|
return
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
jsonBytesResponse(w, http.StatusOK, membersData)
|
2021-06-11 14:24:51 +02:00
|
|
|
|
|
|
|
auditRec.Success()
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleDeleteBlock(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation DELETE /api/v1/boards/{boardID}/blocks/{blockID} deleteBlock
|
2022-02-22 18:42:49 +01:00
|
|
|
//
|
2022-03-22 15:24:34 +01:00
|
|
|
// Deletes a block
|
2022-02-22 18:42:49 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2022-02-22 18:42:49 +01:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Board ID
|
2022-02-22 18:42:49 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: blockID
|
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: ID of block to delete
|
2022-02-22 18:42:49 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: block not found
|
2022-02-22 18:42:49 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
userID := getUserID(r)
|
2022-02-22 18:42:49 +01:00
|
|
|
vars := mux.Vars(r)
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := vars["boardID"]
|
2022-02-22 18:42:49 +01:00
|
|
|
blockID := vars["blockID"]
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to make board changes"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
block, err := a.app.GetBlockByID(blockID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if block == nil || block.BoardID != boardID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "deleteBlock", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("blockID", blockID)
|
|
|
|
|
|
|
|
err = a.app.DeleteBlock(blockID, userID)
|
2022-02-22 18:42:49 +01:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2022-02-22 18:42:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.logger.Debug("DELETE Block", mlog.String("boardID", boardID), mlog.String("blockID", blockID))
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleUndeleteBlock(w http.ResponseWriter, r *http.Request) {
|
2022-04-05 17:00:04 +02:00
|
|
|
// swagger:operation POST /api/v1/boards/{boardID}/blocks/{blockID}/undelete undeleteBlock
|
2022-03-22 15:24:34 +01:00
|
|
|
//
|
|
|
|
// Undeletes a block
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-04-05 17:00:04 +02:00
|
|
|
// - name: boardID
|
2022-03-22 15:24:34 +01:00
|
|
|
// in: path
|
2022-04-05 17:00:04 +02:00
|
|
|
// description: Board ID
|
2022-03-22 15:24:34 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: blockID
|
|
|
|
// in: path
|
|
|
|
// description: ID of block to undelete
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-04-05 17:00:04 +02:00
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BlockPatch"
|
|
|
|
// '404':
|
|
|
|
// description: block not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
blockID := vars["blockID"]
|
2022-04-05 17:00:04 +02:00
|
|
|
boardID := vars["boardID"]
|
|
|
|
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
block, err := a.app.GetLastBlockHistoryEntry(blockID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if board.ID != block.BoardID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modify board members"})
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
|
2022-02-22 18:42:49 +01:00
|
|
|
auditRec := a.makeAuditRecord(r, "undeleteBlock", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("blockID", blockID)
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
undeletedBlock, err := a.app.UndeleteBlock(blockID, userID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
undeletedBlockData, err := json.Marshal(undeletedBlock)
|
2022-02-22 18:42:49 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("UNDELETE Block", mlog.String("blockID", blockID))
|
2022-04-05 17:00:04 +02:00
|
|
|
jsonBytesResponse(w, http.StatusOK, undeletedBlockData)
|
2022-02-22 18:42:49 +01:00
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
func (a *API) handlePatchBlock(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation PATCH /api/v1/boards/{boardID}/blocks/{blockID} patchBlock
|
2021-08-06 14:10:24 +02:00
|
|
|
//
|
|
|
|
// Partially updates a block
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2021-08-06 14:10:24 +02:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Board ID
|
2021-08-06 14:10:24 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: blockID
|
|
|
|
// in: path
|
|
|
|
// description: ID of block to patch
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: block patch to apply
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BlockPatch"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: block not found
|
2021-08-06 14:10:24 +02:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
userID := getUserID(r)
|
2021-08-06 14:10:24 +02:00
|
|
|
vars := mux.Vars(r)
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := vars["boardID"]
|
2021-08-06 14:10:24 +02:00
|
|
|
blockID := vars["blockID"]
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to make board changes"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
block, err := a.app.GetBlockByID(blockID)
|
2021-08-06 14:10:24 +02:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if block == nil || block.BoardID != boardID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
2021-08-06 14:10:24 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var patch *model.BlockPatch
|
|
|
|
err = json.Unmarshal(requestBody, &patch)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "patchBlock", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.AddMeta("boardID", boardID)
|
2021-08-06 14:10:24 +02:00
|
|
|
auditRec.AddMeta("blockID", blockID)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
err = a.app.PatchBlock(blockID, patch, userID)
|
2021-08-06 14:10:24 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.logger.Debug("PATCH Block", mlog.String("boardID", boardID), mlog.String("blockID", blockID))
|
2021-08-06 14:10:24 +02:00
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2021-12-10 15:17:00 +01:00
|
|
|
func (a *API) handlePatchBlocks(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation PATCH /api/v1/boards/{boardID}/blocks/ patchBlocks
|
2021-12-10 15:17:00 +01:00
|
|
|
//
|
|
|
|
// Partially updates batch of blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2021-12-10 15:17:00 +01:00
|
|
|
// in: path
|
|
|
|
// description: Workspace ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: block Ids and block patches to apply
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BlockPatchBatch"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
userID := session.UserID
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
2021-12-10 15:17:00 +01:00
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var patches *model.BlockPatchBatch
|
|
|
|
err = json.Unmarshal(requestBody, &patches)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "patchBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
for i := range patches.BlockIDs {
|
|
|
|
auditRec.AddMeta("block_"+strconv.FormatInt(int64(i), 10), patches.BlockIDs[i])
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
for _, blockID := range patches.BlockIDs {
|
|
|
|
var block *model.Block
|
|
|
|
block, err = a.app.GetBlockByID(blockID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to make board changes"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, block.BoardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to make board changes"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
err = a.app.PatchBlocks(teamID, patches, userID)
|
2021-12-10 15:17:00 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("PATCH Blocks", mlog.String("patches", strconv.Itoa(len(patches.BlockIDs))))
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// Sharing
|
2021-01-14 01:56:01 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleGetSharing(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/boards/{boardID}/sharing getSharing
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
2022-03-22 15:24:34 +01:00
|
|
|
// Returns sharing information for a board
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2021-03-26 19:01:54 +01:00
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2021-03-26 19:01:54 +01:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Board ID
|
2021-03-26 19:01:54 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 20:29:20 +01:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
2022-03-22 15:24:34 +01:00
|
|
|
// "$ref": "#/definitions/Sharing"
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2021-02-17 20:29:20 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
boardID := vars["boardID"]
|
2021-03-31 00:25:16 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
userID := getUserID(r)
|
2022-04-05 17:00:04 +02:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionShareBoard) {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to sharing the board"})
|
|
|
|
return
|
2021-01-14 01:56:01 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec := a.makeAuditRecord(r, "getSharing", audit.Fail)
|
2021-06-11 14:24:51 +02:00
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
|
|
|
|
sharing, err := a.app.GetSharing(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if sharing == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
2021-06-11 14:24:51 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
sharingData, err := json.Marshal(sharing)
|
2021-01-14 01:56:01 +01:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2021-01-14 01:56:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
jsonBytesResponse(w, http.StatusOK, sharingData)
|
|
|
|
|
|
|
|
a.logger.Debug("GET sharing",
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.String("shareID", sharing.ID),
|
|
|
|
mlog.Bool("enabled", sharing.Enabled),
|
|
|
|
)
|
|
|
|
auditRec.AddMeta("shareID", sharing.ID)
|
|
|
|
auditRec.AddMeta("enabled", sharing.Enabled)
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.Success()
|
2021-01-14 01:56:01 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handlePostSharing(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /api/v1/boards/{boardID}/sharing postSharing
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
2022-03-22 15:24:34 +01:00
|
|
|
// Sets sharing information for a board
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2021-03-26 19:01:54 +01:00
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2021-03-26 19:01:54 +01:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Board ID
|
2021-03-26 19:01:54 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: sharing information for a root block
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Sharing"
|
2021-02-17 20:29:20 +01:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionShareBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to sharing the board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
2021-01-14 01:56:01 +01:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2021-01-14 01:56:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
var sharing model.Sharing
|
|
|
|
err = json.Unmarshal(requestBody, &sharing)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stamp boardID from the URL
|
|
|
|
sharing.ID = boardID
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "postSharing", audit.Fail)
|
2021-06-11 14:24:51 +02:00
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.AddMeta("shareID", sharing.ID)
|
|
|
|
auditRec.AddMeta("enabled", sharing.Enabled)
|
|
|
|
|
|
|
|
// Stamp ModifiedBy
|
|
|
|
modifiedBy := userID
|
|
|
|
if userID == model.SingleUser {
|
|
|
|
modifiedBy = ""
|
|
|
|
}
|
|
|
|
sharing.ModifiedBy = modifiedBy
|
|
|
|
|
|
|
|
if userID == model.SingleUser {
|
|
|
|
userID = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.app.GetClientConfig().EnablePublicSharedBoards {
|
|
|
|
a.logger.Info(
|
|
|
|
"Attempt to turn on sharing for board via API failed, sharing off in configuration.",
|
|
|
|
mlog.String("boardID", sharing.ID),
|
|
|
|
mlog.String("userID", userID))
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "Turning on sharing for board failed, see log for details.", nil)
|
|
|
|
return
|
|
|
|
}
|
2021-06-11 14:24:51 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
sharing.ModifiedBy = userID
|
2021-01-14 01:56:01 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
err = a.app.UpsertSharing(sharing)
|
2021-01-14 01:56:01 +01:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2021-01-14 01:56:01 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
a.logger.Debug("POST sharing", mlog.String("sharingID", sharing.ID))
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.Success()
|
2021-01-14 01:56:01 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// Team
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleGetTeams(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/teams getTeams
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
2022-03-22 15:24:34 +01:00
|
|
|
// Returns information of all the teams
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2022-03-22 15:24:34 +01:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Team"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
teams, err := a.app.GetTeamsForUser(userID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getTeams", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("teamCount", len(teams))
|
|
|
|
|
|
|
|
data, err := json.Marshal(teams)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetTeam(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/teams/{teamID} getTeam
|
|
|
|
//
|
|
|
|
// Returns information of the root team
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Team"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
teamID := vars["teamID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to team"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var team *model.Team
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if a.MattermostAuth {
|
|
|
|
team, err = a.app.GetTeam(teamID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
}
|
|
|
|
if team == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusUnauthorized, "invalid team", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
team, err = a.app.GetRootTeam()
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getTeam", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("resultTeamID", team.ID)
|
|
|
|
|
|
|
|
data, err := json.Marshal(team)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handlePostTeamRegenerateSignupToken(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /api/v1/teams/{teamID}/regenerate_signup_token regenerateSignupToken
|
|
|
|
//
|
|
|
|
// Regenerates the signup token for the root team
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2022-04-04 09:49:27 +02:00
|
|
|
if a.MattermostAuth {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotImplemented, "not permitted in plugin mode", nil)
|
2022-04-05 17:00:04 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
team, err := a.app.GetRootTeam()
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "regenerateSignupToken", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
|
|
|
|
team.SignupToken = utils.NewID(utils.IDTypeToken)
|
|
|
|
|
|
|
|
err = a.app.UpsertTeamSignupToken(*team)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
// File upload
|
|
|
|
|
|
|
|
func (a *API) handleServeFile(w http.ResponseWriter, r *http.Request) {
|
2022-03-31 23:02:49 +02:00
|
|
|
// swagger:operation GET "api/v1/files/teams/{teamID}/{boardID}/{filename} getFile
|
2022-03-22 15:24:34 +01:00
|
|
|
//
|
|
|
|
// Returns the contents of an uploaded file
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// - image/jpg
|
|
|
|
// - image/png
|
2022-01-26 14:45:44 +01:00
|
|
|
// - image/gif
|
2021-02-17 20:29:20 +01:00
|
|
|
// parameters:
|
2022-03-31 23:02:49 +02:00
|
|
|
// - name: teamID
|
2021-03-30 01:27:35 +02:00
|
|
|
// in: path
|
2022-03-31 23:02:49 +02:00
|
|
|
// description: Team ID
|
2021-03-30 01:27:35 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-03-31 23:02:49 +02:00
|
|
|
// - name: boardID
|
2021-03-30 01:27:35 +02:00
|
|
|
// in: path
|
2022-03-31 23:02:49 +02:00
|
|
|
// description: Board ID
|
2021-03-30 01:27:35 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-03-31 23:02:49 +02:00
|
|
|
// - name: filename
|
2021-02-17 20:29:20 +01:00
|
|
|
// in: path
|
2022-03-31 23:02:49 +02:00
|
|
|
// description: name of the file
|
2021-02-17 20:29:20 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: file not found
|
2021-02-17 20:29:20 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
vars := mux.Vars(r)
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := vars["boardID"]
|
2020-10-16 11:41:56 +02:00
|
|
|
filename := vars["filename"]
|
2022-03-22 15:24:34 +01:00
|
|
|
userID := getUserID(r)
|
|
|
|
|
2022-03-31 23:02:49 +02:00
|
|
|
hasValidReadToken := a.hasValidReadTokenForBoard(r, boardID)
|
2022-04-05 17:00:04 +02:00
|
|
|
if userID == "" && !hasValidReadToken {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusUnauthorized, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-31 23:02:49 +02:00
|
|
|
if !hasValidReadToken && !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
board, err := a.app.GetBoard(boardID)
|
2021-03-30 01:27:35 +02:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
2021-03-30 01:27:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "getFile", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("teamID", board.TeamID)
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.AddMeta("filename", filename)
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
contentType := "image/jpg"
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
fileExtension := strings.ToLower(filepath.Ext(filename))
|
|
|
|
if fileExtension == "png" {
|
|
|
|
contentType = "image/png"
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:45:44 +01:00
|
|
|
if fileExtension == "gif" {
|
|
|
|
contentType = "image/gif"
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
w.Header().Set("Content-Type", contentType)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
fileReader, err := a.app.GetFileReader(board.TeamID, boardID, filename)
|
2021-05-24 19:06:11 +02:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2021-05-24 19:06:11 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fileReader.Close()
|
|
|
|
http.ServeContent(w, r, filename, time.Now(), fileReader)
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.Success()
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
// FileUploadResponse is the response to a file upload
|
|
|
|
// swagger:model
|
|
|
|
type FileUploadResponse struct {
|
2021-02-23 20:42:28 +01:00
|
|
|
// The FileID to retrieve the uploaded file
|
2021-02-17 20:29:20 +01:00
|
|
|
// required: true
|
2021-02-23 20:42:28 +01:00
|
|
|
FileID string `json:"fileId"`
|
2021-02-17 20:29:20 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 04:57:45 +02:00
|
|
|
func FileUploadResponseFromJSON(data io.Reader) (*FileUploadResponse, error) {
|
|
|
|
var fileUploadResponse FileUploadResponse
|
|
|
|
|
|
|
|
if err := json.NewDecoder(data).Decode(&fileUploadResponse); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &fileUploadResponse, nil
|
|
|
|
}
|
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
func (a *API) handleUploadFile(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation POST /api/v1/teams/{teamID}/boards/{boardID}/files uploadFile
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
2021-03-30 01:27:35 +02:00
|
|
|
// Upload a binary file, attached to a root block
|
2021-02-17 20:29:20 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// consumes:
|
|
|
|
// - multipart/form-data
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: teamID
|
2021-03-30 01:27:35 +02:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: ID of the team
|
2021-03-30 01:27:35 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: boardID
|
2021-03-30 01:27:35 +02:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Board ID
|
2021-03-30 01:27:35 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2021-02-17 20:29:20 +01:00
|
|
|
// - name: uploaded file
|
|
|
|
// in: formData
|
|
|
|
// type: file
|
|
|
|
// description: The file to upload
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/FileUploadResponse"
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2021-02-17 20:29:20 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2021-03-30 01:27:35 +02:00
|
|
|
vars := mux.Vars(r)
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := vars["boardID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to make board changes"})
|
|
|
|
return
|
|
|
|
}
|
2021-03-30 01:27:35 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
board, err := a.app.GetBoard(boardID)
|
2021-03-30 01:27:35 +02:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
2021-03-30 01:27:35 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-10 04:57:45 +02:00
|
|
|
file, handle, err := r.FormFile(UploadFormFileKey)
|
2020-10-16 11:41:56 +02:00
|
|
|
if err != nil {
|
2022-04-05 17:00:04 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "uploadFile", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("teamID", board.TeamID)
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.AddMeta("filename", handle.Filename)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
fileID, err := a.app.SaveFile(file, board.TeamID, boardID, handle.Filename)
|
2020-10-16 11:41:56 +02:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2021-02-17 20:29:20 +01:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2021-05-29 08:23:10 +02:00
|
|
|
a.logger.Debug("uploadFile",
|
|
|
|
mlog.String("filename", handle.Filename),
|
2021-06-21 11:21:42 +02:00
|
|
|
mlog.String("fileID", fileID),
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
2021-06-21 11:21:42 +02:00
|
|
|
data, err := json.Marshal(FileUploadResponse{FileID: fileID})
|
2021-02-17 20:29:20 +01:00
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2020-10-16 11:41:56 +02:00
|
|
|
return
|
|
|
|
}
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2021-02-17 20:29:20 +01:00
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2021-06-11 14:24:51 +02:00
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
auditRec.AddMeta("fileID", fileID)
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec.Success()
|
2020-10-16 11:41:56 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleGetTeamUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/teams/{teamID}/users getTeamUsers
|
2021-06-04 15:23:15 +02:00
|
|
|
//
|
2022-03-22 15:24:34 +01:00
|
|
|
// Returns team users
|
2021-06-04 15:23:15 +02:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: teamID
|
2021-06-04 15:23:15 +02:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Team ID
|
2021-06-04 15:23:15 +02:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: search
|
|
|
|
// in: query
|
|
|
|
// description: string to filter users list
|
|
|
|
// required: false
|
|
|
|
// type: string
|
2021-06-04 15:23:15 +02:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2022-03-22 15:24:34 +01:00
|
|
|
teamID := vars["teamID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
query := r.URL.Query()
|
|
|
|
searchQuery := query.Get("search")
|
2021-06-04 15:23:15 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "Access denied to team", PermissionError{"access denied to team"})
|
2021-06-04 15:23:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:24:51 +02:00
|
|
|
auditRec := a.makeAuditRecord(r, "getUsers", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
users, err := a.app.SearchTeamUsers(teamID, searchQuery)
|
2021-06-04 15:23:15 +02:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "searchQuery="+searchQuery, err)
|
2021-06-04 15:23:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(users)
|
|
|
|
if err != nil {
|
2021-07-27 18:57:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
2021-06-04 15:23:15 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2021-06-11 14:24:51 +02:00
|
|
|
|
|
|
|
auditRec.AddMeta("userCount", len(users))
|
|
|
|
auditRec.Success()
|
2021-06-04 15:23:15 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleGetBoards(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/teams/{teamID}/boards getBoards
|
|
|
|
//
|
|
|
|
// Returns team boards
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Board"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2021-12-10 16:46:37 +01:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
teamID := mux.Vars(r)["teamID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to team"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getBoards", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("teamID", teamID)
|
|
|
|
|
|
|
|
// retrieve boards list
|
|
|
|
boards, err := a.app.GetBoardsForUserAndTeam(userID, teamID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("GetBoards",
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.Int("boardsCount", len(boards)),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(boards)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.AddMeta("boardsCount", len(boards))
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetTemplates(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/teams/{teamID}/templates getTemplates
|
2021-12-10 16:46:37 +01:00
|
|
|
//
|
2022-03-22 15:24:34 +01:00
|
|
|
// Returns team templates
|
2021-12-10 16:46:37 +01:00
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2022-03-22 15:24:34 +01:00
|
|
|
// - name: teamID
|
2021-12-10 16:46:37 +01:00
|
|
|
// in: path
|
2022-03-22 15:24:34 +01:00
|
|
|
// description: Team ID
|
2021-12-10 16:46:37 +01:00
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-03-22 15:24:34 +01:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Board"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
teamID := mux.Vars(r)["teamID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
2022-04-01 21:42:30 +02:00
|
|
|
if teamID != model.GlobalTeamID && !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to team"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getTemplates", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("teamID", teamID)
|
|
|
|
|
|
|
|
// retrieve boards list
|
2022-03-23 11:51:48 +01:00
|
|
|
boards, err := a.app.GetTemplateBoards(teamID, userID)
|
2022-03-22 15:24:34 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
results := []*model.Board{}
|
|
|
|
for _, board := range boards {
|
|
|
|
if board.Type == model.BoardTypeOpen {
|
|
|
|
results = append(results, board)
|
|
|
|
} else if a.permissions.HasPermissionToBoard(userID, board.ID, model.PermissionViewBoard) {
|
|
|
|
results = append(results, board)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.logger.Debug("GetTemplates",
|
|
|
|
mlog.String("teamID", teamID),
|
2022-04-05 17:00:04 +02:00
|
|
|
mlog.Int("boardsCount", len(results)),
|
2022-03-22 15:24:34 +01:00
|
|
|
)
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
data, err := json.Marshal(results)
|
2022-03-22 15:24:34 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
auditRec.AddMeta("templatesCount", len(results))
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscriptions
|
|
|
|
|
|
|
|
func (a *API) handleCreateSubscription(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /api/v1/subscriptions createSubscription
|
|
|
|
//
|
|
|
|
// Creates a subscription to a block for a user. The user will receive change notifications for the block.
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-12-10 16:46:37 +01:00
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: subscription definition
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Subscription"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var sub model.Subscription
|
|
|
|
|
|
|
|
err = json.Unmarshal(requestBody, &sub)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = sub.IsValid(); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "createSubscription", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("subscriber_id", sub.SubscriberID)
|
|
|
|
auditRec.AddMeta("block_id", sub.BlockID)
|
|
|
|
|
|
|
|
// User can only create subscriptions for themselves (for now)
|
|
|
|
if session.UserID != sub.SubscriberID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "userID and subscriberID mismatch", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for valid block
|
2022-03-22 15:24:34 +01:00
|
|
|
block, err := a.app.GetBlockByID(sub.BlockID)
|
2022-01-05 10:02:06 +01:00
|
|
|
if err != nil || block == nil {
|
2021-12-10 16:46:37 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "invalid blockID", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
subNew, err := a.app.CreateSubscription(&sub)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("CREATE subscription",
|
|
|
|
mlog.String("subscriber_id", subNew.SubscriberID),
|
|
|
|
mlog.String("block_id", subNew.BlockID),
|
|
|
|
)
|
|
|
|
|
|
|
|
json, err := json.Marshal(subNew)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDeleteSubscription(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation DELETE /api/v1/subscriptions/{blockID}/{subscriberID} deleteSubscription
|
2021-12-10 16:46:37 +01:00
|
|
|
//
|
|
|
|
// Deletes a subscription a user has for a a block. The user will no longer receive change notifications for the block.
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
2021-12-15 20:54:23 +01:00
|
|
|
// - name: blockID
|
2021-12-10 16:46:37 +01:00
|
|
|
// in: path
|
|
|
|
// description: Block ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: subscriberID
|
|
|
|
// in: path
|
|
|
|
// description: Subscriber ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
blockID := vars["blockID"]
|
|
|
|
subscriberID := vars["subscriberID"]
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "deleteSubscription", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("block_id", blockID)
|
|
|
|
auditRec.AddMeta("subscriber_id", subscriberID)
|
|
|
|
|
|
|
|
// User can only delete subscriptions for themselves
|
|
|
|
if session.UserID != subscriberID {
|
2022-04-05 17:00:04 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "access denied", nil)
|
2021-12-10 16:46:37 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
_, err := a.app.DeleteSubscription(blockID, subscriberID)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("DELETE subscription",
|
|
|
|
mlog.String("blockID", blockID),
|
|
|
|
mlog.String("subscriberID", subscriberID),
|
|
|
|
)
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetSubscriptions(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation GET /api/v1/subscriptions/{subscriberID} getSubscriptions
|
2021-12-10 16:46:37 +01:00
|
|
|
//
|
|
|
|
// Gets subscriptions for a user.
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: subscriberID
|
|
|
|
// in: path
|
|
|
|
// description: Subscriber ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/User"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
ctx := r.Context()
|
|
|
|
session := ctx.Value(sessionContextKey).(*model.Session)
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
subscriberID := vars["subscriberID"]
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getSubscriptions", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("subscriber_id", subscriberID)
|
|
|
|
|
|
|
|
// User can only get subscriptions for themselves (for now)
|
|
|
|
if session.UserID != subscriberID {
|
2022-04-05 17:00:04 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "access denied", nil)
|
2021-12-10 16:46:37 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
subs, err := a.app.GetSubscriptions(subscriberID)
|
2021-12-10 16:46:37 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("GET subscriptions",
|
|
|
|
mlog.String("subscriberID", subscriberID),
|
|
|
|
mlog.Int("count", len(subs)),
|
|
|
|
)
|
|
|
|
|
|
|
|
json, err := json.Marshal(subs)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
jsonBytesResponse(w, http.StatusOK, json)
|
|
|
|
|
|
|
|
auditRec.AddMeta("subscription_count", len(subs))
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleCreateBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /api/v1/boards createBoard
|
|
|
|
//
|
|
|
|
// Creates a new board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: the board to create
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Board"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/Board'
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var newBoard *model.Board
|
|
|
|
if err = json.Unmarshal(requestBody, &newBoard); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if newBoard.Type == model.BoardTypeOpen {
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, newBoard.TeamID, model.PermissionCreatePublicChannel) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to create public boards"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, newBoard.TeamID, model.PermissionCreatePrivateChannel) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to create private boards"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = newBoard.IsValid(); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "createBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("teamID", newBoard.TeamID)
|
|
|
|
auditRec.AddMeta("boardType", newBoard.Type)
|
|
|
|
|
|
|
|
// create board
|
|
|
|
board, err := a.app.CreateBoard(newBoard, userID, true)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("CreateBoard",
|
|
|
|
mlog.String("teamID", board.TeamID),
|
|
|
|
mlog.String("boardID", board.ID),
|
|
|
|
mlog.String("boardType", string(board.Type)),
|
|
|
|
mlog.String("userID", userID),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(board)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-02-28 12:28:16 +01:00
|
|
|
func (a *API) handleOnboard(w http.ResponseWriter, r *http.Request) {
|
2022-03-22 15:24:34 +01:00
|
|
|
// swagger:operation POST /api/v1/team/{teamID}/onboard onboard
|
2022-02-28 12:28:16 +01:00
|
|
|
//
|
|
|
|
// Onboards a user on Boards.
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
2022-03-22 15:24:34 +01:00
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
2022-02-28 12:28:16 +01:00
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/OnboardingResponse"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2022-03-22 15:24:34 +01:00
|
|
|
teamID := mux.Vars(r)["teamID"]
|
2022-04-05 17:00:04 +02:00
|
|
|
userID := getUserID(r)
|
2022-02-28 12:28:16 +01:00
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to create board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
teamID, boardID, err := a.app.PrepareOnboardingTour(userID, teamID)
|
2022-02-28 12:28:16 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
response := map[string]string{
|
2022-03-22 15:24:34 +01:00
|
|
|
"teamID": teamID,
|
|
|
|
"boardID": boardID,
|
2022-02-28 12:28:16 +01:00
|
|
|
}
|
|
|
|
data, err := json.Marshal(response)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleGetBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/boards/{boardID} getBoard
|
|
|
|
//
|
|
|
|
// Returns a board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/Board"
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2020-10-16 11:41:56 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
hasValidReadToken := a.hasValidReadTokenForBoard(r, boardID)
|
|
|
|
if userID == "" && !hasValidReadToken {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusUnauthorized, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
2020-10-22 15:22:36 +02:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
2020-10-22 15:22:36 +02:00
|
|
|
}
|
2020-10-16 16:21:42 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if !hasValidReadToken {
|
|
|
|
if board.Type == model.BoardTypePrivate {
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, board.TeamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
|
|
|
|
a.logger.Debug("GetBoard",
|
|
|
|
mlog.String("boardID", boardID),
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
data, err := json.Marshal(board)
|
2021-03-26 19:01:54 +01:00
|
|
|
if err != nil {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
2021-03-26 19:01:54 +01:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2021-05-29 08:23:10 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec.Success()
|
2021-05-29 08:23:10 +02:00
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handlePatchBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation PATCH /api/v1/boards/{boardID} patchBoard
|
|
|
|
//
|
|
|
|
// Partially updates a board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: board patch to apply
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BoardPatch"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/Board'
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
2021-09-08 06:52:03 +02:00
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var patch *model.BoardPatch
|
|
|
|
if err = json.Unmarshal(requestBody, &patch); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = patch.IsValid(); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardProperties) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modifying board properties"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.Type != nil {
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardType) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modifying board type"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "patchBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("userID", userID)
|
|
|
|
|
|
|
|
// patch board
|
|
|
|
updatedBoard, err := a.app.PatchBoard(patch, boardID, userID)
|
2021-09-08 06:52:03 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
a.logger.Debug("PatchBoard",
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.String("userID", userID),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(updatedBoard)
|
2021-09-08 06:52:03 +02:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
// response
|
2021-09-08 06:52:03 +02:00
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDeleteBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation DELETE /api/v1/boards/{boardID} deleteBoard
|
|
|
|
//
|
|
|
|
// Removes a board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
// Check if board exists
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionDeleteBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to delete board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "deleteBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
|
|
|
|
if err := a.app.DeleteBoard(boardID, userID); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("DELETE Board", mlog.String("boardID", boardID))
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDuplicateBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /api/v1/boards/{boardID}/duplicate duplicateBoard
|
|
|
|
//
|
|
|
|
// Returns the new created board and all the blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/BoardsAndBlocks'
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
query := r.URL.Query()
|
|
|
|
asTemplate := query.Get("asTemplate")
|
|
|
|
toTeam := query.Get("toTeam")
|
|
|
|
|
2022-04-04 09:39:29 +02:00
|
|
|
if userID == "" {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusUnauthorized, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if toTeam == "" && !a.permissions.HasPermissionToTeam(userID, board.TeamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to team"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if board.IsTemplate && board.Type == model.BoardTypeOpen {
|
|
|
|
if board.TeamID != model.GlobalTeamID && !a.permissions.HasPermissionToTeam(userID, board.TeamID, model.PermissionViewTeam) {
|
2022-04-04 09:39:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2022-04-05 17:00:04 +02:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
2022-04-04 09:39:29 +02:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
2022-03-22 15:24:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "duplicateBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
|
|
|
|
a.logger.Debug("DuplicateBoard",
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
)
|
|
|
|
|
|
|
|
boardsAndBlocks, _, err := a.app.DuplicateBoard(boardID, userID, toTeam, asTemplate == "true")
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(boardsAndBlocks)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDuplicateBlock(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /api/v1/boards/{boardID}/blocks/{blockID}/duplicate duplicateBlock
|
|
|
|
//
|
|
|
|
// Returns the new created blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: blockID
|
|
|
|
// in: path
|
|
|
|
// description: Block ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Block"
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board or block not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
blockID := mux.Vars(r)["blockID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
query := r.URL.Query()
|
|
|
|
asTemplate := query.Get("asTemplate")
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
}
|
|
|
|
|
2022-04-04 09:39:29 +02:00
|
|
|
if userID == "" {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusUnauthorized, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
block, err := a.app.GetBlockByID(blockID)
|
2022-03-22 15:24:34 +01:00
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
2022-04-05 17:00:04 +02:00
|
|
|
if block == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if board.ID != block.BoardID {
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modify board members"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec := a.makeAuditRecord(r, "duplicateBlock", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("blockID", blockID)
|
|
|
|
|
|
|
|
a.logger.Debug("DuplicateBlock",
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.String("blockID", blockID),
|
|
|
|
)
|
|
|
|
|
|
|
|
blocks, err := a.app.DuplicateBlock(boardID, blockID, userID, asTemplate == "true")
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(blocks)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
GetBoardMetadata API (#2569)
* wip
* Added data migration for populating categories
* wip
* Added data migration for populating categories
* Store WIP
* migration WIP
* category CRUD APIs complete
* category block API WIP
* block category update API done
* Fetcehed data into store
* Started displayting sidebar data
* sidebar WIP
* Dashboard - basic changes
* Sidebar dashboard btn and board switcher UI only
* Sidebar dashboard btn and board switcher UI only
* create category dialog WIP
* Create category webapp side done
* Integrated move card to other category
* board to block
* Disabled dashboard route for now as we'll implement it in phase 2
* WIP
* Added logic to open last board/view on per team level
* Add workspace to teams and boards migrations (#1986)
* Add workspace to teams and boards migrations
* Update json annotations on board models
* boards search dialog WIP
* Seach dialog WIP
* Implemented opening boiard from search results
* Boards switcher styliung
* Handled update category WS event
* Template support
* personal server support and styling fixes
* test fix WIP
* Fixed a bug causing boards to not be moved correctly beteen categories
* Fixed webapp tests
* fix
* Store changes (#2011)
* Permissions phase 1 - Websocket updates (#2014)
* Store changes
* Websockets changes
* Permissions phase 1 - Permissions service (#2015)
* Store changes
* Websockets changes
* Permissions service
* Api and app updates (#2016)
* Store changes
* Websockets changes
* Permissions service
* New API and App changes
* Delete and Patch boards and blocks endpoints
* Used correct variable
* Webapp changes WIP
* Open correct team URL
* Fixed get block API
* Used React context for workspace users
* WIP
* On load navigation sorted out
* WIP
* Nav fix
* categories WS broadcast
* Used real search API
* Fixed unfurl ppreview
* set active team in sidebar
* IMplemented navigation on changing team in sidebar
* Misc fixes
* close rows inside transaction (#2045)
* update syntax for mysql (#2044)
* Upadted mutator for new patchBlock API
* Updated patchBlock API to use new URL
* Listeining to correct event in plugin mode
* Implemented WS messages for category operations:
* Fix duplicated build tags on Makefile
* Sidebar enhancements
* Add missing prefix to SQLite migration and fix flaky tests
* Sidebar boards menu enhancement
* Fix board page interactions (#2144)
* Fix patch board card properties error
* Fix board interactions
* Fix insert blocks interactions
* Fix app tests (#2104)
* Add json1 tag to vscode launch (#2157)
* Fix add, delete and update boards and add board patch generation (#2146)
* Fix update boards and add board patch generation
* Make add board and add template work, as well as deleting a board
* Update the state on board deletion
* Delete unused variable
* Fix bad parenthesis
* Fix board creation inside plugin, options were coming null due websocket message serialization
* update property type mutators to use boards API (#2168)
* Add permissions modal (#2196)
* Initial integration
* Permissions modal, websocket updates and API tests implemented
* Avoid updating/removing user if there is only one admin left
* Fix duplicates on board search
* Adds integration test
* Addressing PR review comments
Co-authored-by: Jesús Espino <jespinog@gmail.com>
* Merge
* I'm able to compile now
* Some fixes around tests execution
* Fixing migrations
* Fixing migrations order
* WIP
* Fixing some other compilation problems on tests
* Some typescript tests fixed
* Fixing javascript tests
* Fixing compilation
* Fixing some problems to create boards
* Load the templates on initial load
* Improvements over initial team templates import
* Adding new fields in the database
* Working on adding duplicate board api
* Removing RootID concept entirely
* Improving a bit the subscriptions
* Fixing store tests for notificationHints
* Fixing more tests
* fixing tests
* Fixing tests
* Fixing tests
* Fixing some small bugs related to templates
* Fixing registration link generation/regeneration
* Fixing cypress tests
* Adding store tests for duplicateBoard and duplicateBlock
* Addressing some TODO comments
* Making the export api simpler
* Add redirect component for old workspace urls
* Removing Dashboard code
* Delete only the built-in templates on update
* fixing tests
* Adding users autocompletion
* Updating snapshots
* Fixing bad merge
* fix panic when creating new card in notifysubscriptions (#2352)
* fix lint errors (#2353)
* fix lint errors
* fix panic when creating new card in notifysubscriptions (#2352)
* fix lint errors
* fix unit test
* Revert "fix unit test"
This reverts commit 0ad78aed65745521c0bb45790c9ea91b6c316c44.
Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
* fix sql syntax error for SearchUsersByTeam (#2357)
* Fix mentions delivery (#2358)
* fix sql syntax error for SearchUsersByTeam
* fix mentions delivery
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* update api for octoClient calls, pass correct variables to mutator (#2359)
* Fixing tests after merge
* Fix sidebar context menu UI issue (#2399)
* Fix notification diff for text blocks (#2386)
* fix notification diff for text blocks; fix various linter errors.
* fix URLs to cards
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Permissions branch: Fix card links (#2391)
* fix notification diff for text blocks; fix various linter errors.
* fix URLs to cards
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Fixing sqlite tests
* Fixing server tests
* Update migrations to create global templates. (#2397)
* fix duplicate templates
* revert migrate.go
* update UI for empty templates
* implement updating built-in templates as global (teamId = 0)
* handle error if board not found
* update unit test
* fix more tests
* Update blocks_test.go
Fix merge issue
* fix migration sql error (#2414)
* Fixing frontend tests
* Set target team ID when using a global template (#2419)
* Fix some server tests
* Fixing onboarding creation
* Permissions branch: Fix unit tests and CI errors (part 1) (#2425)
* Fixing some small memory leaks (#2400)
* Fixing some small memory leaks
* fixing tests
* passing the tags to all test targets
* Increasing the timeout of the tests
* Fix some type checkings
* Permissions branch: Fixes all the linter errors (#2429)
* fix linter errors
* Reestructuring the router and splitting in more subcomponents (#2403)
* Reestructuring the router and splitting in more subcomponents
* Removing console.log calls
* Removing unneeded selector
* Addressing PR comment
* Fix redirection to one team when you load directly the boards home path
* Using properly the lastTeamID to redirect the user if needed
* don't allow last admin change/deleted (#2416)
* don't allow last admin change/deleted
* update for i18-extract
* fixed en.json
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com>
* Splitting BoardPage component into simpler/smaller components (#2435)
* Splitting BoardPage component into simpler/smaller components
* Removing unneeded import
* Replace go migrate with morph permissions (#2424)
* merge origin/replace-go-migrate-with-morph
* run go mod tidy on mattermost-plugin and increase test timeout
* fix merge issue temprorarily
* remove some debug changes
* fixing the linter
* Allow always team 0 (global) templates fetch (#2472)
* Fix problem with viewId 0 in the URL (#2473)
* Migrate from binddata to goembed (#2471)
* Adding join logic to the board switcher (#2434)
* Adding join logic to the board switcher
* Using already existing client function and removing the joinBoard one
* Adding support for autojoin based on url
* Fixing frontend tests
* fix webapp compile error, missing enableSharedBoards (#2501)
* Fixing duplication on postgres
* Adding back views to the sidebar (#2494)
* Fix #2507. Update Swagger comments (#2508)
* Fix the flash of the template selector on board/team switch (#2490)
* Fix the flash of the template selector on board/team switch
* More fixes specially around error handling
* Fixing the bot badge (#2487)
* simplifying a bit the team store sync between channels and focalboard (#2481)
* Fix menu tests (#2528)
* fix failing menu tests
* fix lint error
* Added keyboard shortcut for boards switcher (#2407)
* Added keyboard shortcut for boards switcher
* Fixed a type error
* Added some inline comments
* Fixed lint
* Fixed bug with scroll jumping when the card is opened: (#2477)
- avoid remounting of `ScrollingComponent` for each render of `Kanban` component
- property `autoFocus` set to false for `CalculationOptions` because it triggers `blur` even for the button in Jest tests and closes the menu
- snapshots for tests with `CalculationOptions` updated
* Adding the frontend support for permissions and applying it to a big part of the interface. (#2536)
* Initial work on permissions gates
* Applying permissions gates in more places
* Adding more checks to the interface
* Adding more permissions gates and keeping the store up to date
* fixing some tests
* Fixing some more tests
* Fixing another test
* Fixing all tests and adding some more
* Adding no-permission snapshot tests
* Addressing PR review comments
* Fixing invert behavior
* Permissions branch: No sqlstore calls after app shutdown (#2530)
* fix webapp compile error, missing enableSharedBoards
* refactor app init wip
* - ensure all block change notifications are finished before shutting down app
- fix unit tests for mysql (insert_at only has 1 second resolution!)
* adjust logging
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Fixed migrations to allow upgrading from previous version (#2535)
* Added mechanism to check if schema migration is needed
* WIP
* WIP
* WIP
* WIP
* Fixed migration
* Fixed for SQLite
* minor cleaniup
* Deleted old schema migration table after running migrations
* Removed a debug log
* Fixed a bug where the code always tried to delete a table which may or may not exist
* Show properly the user avatar in the ShareBoard component (#2542)
* Fixing the last CI problems from the permissions-branch (#2541)
* Fix history ordering
* Giving some times to avoid possible race conditions
* Empty
* Reverting accidental change in the config.json
* Optimizing table view (#2540)
* Optimizing table view
* Reducing the amount of rendering for tables
* Some other performance improvements
* Improve the activeView updates
* Some extra simplifications
* Another small improvement
* Fixing tests
* Fixing linter errors
* Reducing a bit the amount of dependency with big objects in the store
* Small simplification
* Removing Commenter role from the user role selector (#2561)
* Shareboard cleanup (#2550)
* Initial work on permissions gates
* Applying permissions gates in more places
* Adding more checks to the interface
* Adding more permissions gates and keeping the store up to date
* fixing some tests
* Fixing some more tests
* Fixing another test
* Fixing all tests and adding some more
* Adding no-permission snapshot tests
* Addressing PR review comments
* cleanup some shareboard settings
* remove unused property, fix for user items being displayed for non admin
* revert change, allow users to show
Co-authored-by: Jesús Espino <jespinog@gmail.com>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* GetBoardMetadata API
* Integration tests. WIP
* getBoardHistory
* Working integration test
* Fix ordering, add store tests
* Fix: Update board_history update_at on patch
* sqltests
* Fix unmarshall delete boards_history
* testGetBlockMetadata with delete and undelete
* Handle board not found
* Fixing comments and cards with the new optimizations in the store (#2560)
* Fixing property creation (#2563)
* Cleanup
* Fix user selection in table view (#2565)
* Fixing focus new row in table view (#2567)
* Permissions branch: Fix sqlite table lock (CI) (#2568)
* fix sqlite table lock
* remove test db on teardown
* revert .gitignore
* fix goimport on migration code
* fix typo
* more linter fixes
* clean up tmp db for sqlstore tests
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
* Fixing snapshots
* Remove debug log
* Return metadata for deleted boards
* Migrating center panel to functional component (#2562)
* Migrating center panel to functional component
* Fixing some tests
* Fixing another test
* Fixing linter errors
* Fixing types errors
* Fixing linter error
* Fixing cypress tests
* Fixing the last cypress test
* Simpliying a bit the code
* Making property insertion more robust
* Updating checkbox test
* License check
* Cleanup and update Swagger docs
* Merge from main
* Fix bad merge
* Fix Linux-app build break
* do mod tidy
* Fix server lint
* Require credentials (not only read token)
* Add missing defer CloseRows
* do mod tidy
Co-authored-by: Harshil Sharma <harshilsharma63@gmail.com>
Co-authored-by: Miguel de la Cruz <miguel@mcrx.me>
Co-authored-by: Scott Bishel <scott.bishel@mattermost.com>
Co-authored-by: Jesús Espino <jespinog@gmail.com>
Co-authored-by: Doug Lauder <wiggin77@warpmail.net>
Co-authored-by: Mattermod <mattermod@users.noreply.github.com>
Co-authored-by: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com>
Co-authored-by: Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com>
Co-authored-by: kamre <eremchenko@gmail.com>
2022-03-29 23:47:49 +02:00
|
|
|
func (a *API) handleGetBoardMetadata(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/boards/{boardID}/metadata getBoardMetadata
|
|
|
|
//
|
|
|
|
// Returns a board's metadata
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BoardMetadata"
|
|
|
|
// '404':
|
|
|
|
// description: board not found
|
|
|
|
// '501':
|
|
|
|
// description: required license not found
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
board, boardMetadata, err := a.app.GetBoardMetadata(boardID)
|
|
|
|
if errors.Is(err, app.ErrInsufficientLicense) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotImplemented, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil || boardMetadata == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if board.Type == model.BoardTypePrivate {
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, board.TeamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getBoardMetadata", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
|
|
|
|
data, err := json.Marshal(boardMetadata)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleSearchBoards(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/teams/{teamID}/boards/search searchBoards
|
|
|
|
//
|
|
|
|
// Returns the boards that match with a search term
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: teamID
|
|
|
|
// in: path
|
|
|
|
// description: Team ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: q
|
|
|
|
// in: query
|
|
|
|
// description: The search term. Must have at least one character
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/Board"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
teamID := mux.Vars(r)["teamID"]
|
|
|
|
term := r.URL.Query().Get("q")
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to team"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(term) == 0 {
|
|
|
|
jsonStringResponse(w, http.StatusOK, "[]")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "searchBoards", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelRead, auditRec)
|
|
|
|
auditRec.AddMeta("teamID", teamID)
|
|
|
|
|
|
|
|
// retrieve boards list
|
|
|
|
boards, err := a.app.SearchBoardsForUserAndTeam(term, userID, teamID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("SearchBoards",
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.Int("boardsCount", len(boards)),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(boards)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.AddMeta("boardsCount", len(boards))
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleGetMembersForBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation GET /api/v1/boards/{boardID}/members getMembersForBoard
|
|
|
|
//
|
|
|
|
// Returns the members of the board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// type: array
|
|
|
|
// items:
|
|
|
|
// "$ref": "#/definitions/BoardMember"
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board members"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "getMembersForBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
|
|
|
|
members, err := a.app.GetMembersForBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("GetMembersForBoard",
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.Int("membersCount", len(members)),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(members)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleAddMember(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /boards/{boardID}/members addMember
|
|
|
|
//
|
|
|
|
// Adds a new member to a board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: membership to replace the current one with
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BoardMember"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/BoardMember'
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
2022-04-05 17:00:04 +02:00
|
|
|
userID := getUserID(r)
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardRoles) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modify board members"})
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqBoardMember *model.BoardMember
|
|
|
|
if err = json.Unmarshal(requestBody, &reqBoardMember); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqBoardMember.UserID == "" {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// currently all memberships are created as editors by default
|
|
|
|
newBoardMember := &model.BoardMember{
|
|
|
|
UserID: reqBoardMember.UserID,
|
|
|
|
BoardID: boardID,
|
|
|
|
SchemeEditor: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "addMember", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("addedUserID", reqBoardMember.UserID)
|
|
|
|
|
|
|
|
member, err := a.app.AddMemberToBoard(newBoardMember)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("AddMember",
|
|
|
|
mlog.String("boardID", board.ID),
|
|
|
|
mlog.String("addedUserID", reqBoardMember.UserID),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(member)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-03-29 10:14:33 +02:00
|
|
|
func (a *API) handleJoinBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /boards/{boardID}/join joinBoard
|
|
|
|
//
|
|
|
|
// Become a member of a board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/BoardMember'
|
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2022-04-05 17:00:04 +02:00
|
|
|
// '403':
|
2022-03-29 10:14:33 +02:00
|
|
|
// description: access denied
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
if userID == "" {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board.Type != model.BoardTypeOpen {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, board.TeamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// currently all memberships are created as editors by default
|
|
|
|
// TODO: Support different public roles
|
|
|
|
newBoardMember := &model.BoardMember{
|
|
|
|
UserID: userID,
|
|
|
|
BoardID: boardID,
|
|
|
|
SchemeEditor: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "joinBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("addedUserID", userID)
|
|
|
|
|
|
|
|
member, err := a.app.AddMemberToBoard(newBoardMember)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
a.logger.Debug("JoinBoard",
|
2022-03-29 10:14:33 +02:00
|
|
|
mlog.String("boardID", board.ID),
|
|
|
|
mlog.String("addedUserID", userID),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(member)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
func (a *API) handleLeaveBoard(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /boards/{boardID}/leave leaveBoard
|
|
|
|
//
|
|
|
|
// Remove your own membership from a board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// '404':
|
|
|
|
// description: board not found
|
|
|
|
// '403':
|
|
|
|
// description: access denied
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
if userID == "" {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "leaveBoard", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("addedUserID", userID)
|
|
|
|
|
|
|
|
err = a.app.DeleteBoardMember(boardID, userID)
|
|
|
|
if errors.Is(err, app.ErrBoardMemberIsLastAdmin) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("LeaveBoard",
|
|
|
|
mlog.String("boardID", board.ID),
|
|
|
|
mlog.String("addedUserID", userID),
|
|
|
|
)
|
|
|
|
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
func (a *API) handleUpdateMember(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation PUT /boards/{boardID}/members/{userID} updateMember
|
|
|
|
//
|
|
|
|
// Updates a board member
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: userID
|
|
|
|
// in: path
|
|
|
|
// description: User ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: membership to replace the current one with
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BoardMember"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/BoardMember'
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
paramsUserID := mux.Vars(r)["userID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var reqBoardMember *model.BoardMember
|
|
|
|
if err = json.Unmarshal(requestBody, &reqBoardMember); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
newBoardMember := &model.BoardMember{
|
|
|
|
UserID: paramsUserID,
|
|
|
|
BoardID: boardID,
|
|
|
|
SchemeAdmin: reqBoardMember.SchemeAdmin,
|
|
|
|
SchemeEditor: reqBoardMember.SchemeEditor,
|
|
|
|
SchemeCommenter: reqBoardMember.SchemeCommenter,
|
|
|
|
SchemeViewer: reqBoardMember.SchemeViewer,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardRoles) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modify board members"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "patchMember", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("patchedUserID", paramsUserID)
|
|
|
|
|
|
|
|
member, err := a.app.UpdateBoardMember(newBoardMember)
|
|
|
|
if errors.Is(err, app.ErrBoardMemberIsLastAdmin) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("PatchMember",
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.String("patchedUserID", paramsUserID),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(member)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDeleteMember(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation DELETE /api/v1/boards/{boardID}/members/{userID} deleteMember
|
|
|
|
//
|
|
|
|
// Deletes a member from a board
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: boardID
|
|
|
|
// in: path
|
|
|
|
// description: Board ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// - name: userID
|
|
|
|
// in: path
|
|
|
|
// description: User ID
|
|
|
|
// required: true
|
|
|
|
// type: string
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
2022-04-05 17:00:04 +02:00
|
|
|
// '404':
|
|
|
|
// description: board not found
|
2022-03-22 15:24:34 +01:00
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
boardID := mux.Vars(r)["boardID"]
|
|
|
|
paramsUserID := mux.Vars(r)["userID"]
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardRoles) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modify board members"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
auditRec := a.makeAuditRecord(r, "deleteMember", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardID", boardID)
|
|
|
|
auditRec.AddMeta("addedUserID", paramsUserID)
|
|
|
|
|
|
|
|
deleteErr := a.app.DeleteBoardMember(boardID, paramsUserID)
|
|
|
|
if errors.Is(deleteErr, app.ErrBoardMemberIsLastAdmin) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", deleteErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if deleteErr != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", deleteErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("DeleteMember",
|
|
|
|
mlog.String("boardID", boardID),
|
|
|
|
mlog.String("addedUserID", paramsUserID),
|
|
|
|
)
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleCreateBoardsAndBlocks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation POST /api/v1/boards-and-blocks insertBoardsAndBlocks
|
|
|
|
//
|
|
|
|
// Creates new boards and blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: the boards and blocks to create
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/BoardsAndBlocks"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/BoardsAndBlocks'
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var newBab *model.BoardsAndBlocks
|
|
|
|
if err = json.Unmarshal(requestBody, &newBab); err != nil {
|
|
|
|
// a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if len(newBab.Boards) == 0 {
|
|
|
|
message := "at least one board is required"
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
teamID := ""
|
|
|
|
boardIDs := map[string]bool{}
|
|
|
|
for _, board := range newBab.Boards {
|
|
|
|
boardIDs[board.ID] = true
|
|
|
|
|
|
|
|
if teamID == "" {
|
|
|
|
teamID = board.TeamID
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if teamID != board.TeamID {
|
|
|
|
message := "cannot create boards for multiple teams"
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if board.ID == "" {
|
|
|
|
message := "boards need an ID to be referenced from the blocks"
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToTeam(userID, teamID, model.PermissionViewTeam) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board template"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
for _, block := range newBab.Blocks {
|
|
|
|
// Error checking
|
|
|
|
if len(block.Type) < 1 {
|
|
|
|
message := fmt.Sprintf("missing type for block id %s", block.ID)
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if block.CreateAt < 1 {
|
|
|
|
message := fmt.Sprintf("invalid createAt for block id %s", block.ID)
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if block.UpdateAt < 1 {
|
|
|
|
message := fmt.Sprintf("invalid UpdateAt for block id %s", block.ID)
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
if !boardIDs[block.BoardID] {
|
|
|
|
message := fmt.Sprintf("invalid BoardID %s (not exists in the created boards)", block.BoardID)
|
2022-03-22 15:24:34 +01:00
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, message, nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// IDs of boards and blocks are used to confirm that they're
|
|
|
|
// linked and then regenerated by the server
|
|
|
|
newBab, err = model.GenerateBoardsAndBlocksIDs(newBab, a.logger)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "createBoardsAndBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("teamID", teamID)
|
|
|
|
auditRec.AddMeta("userID", userID)
|
|
|
|
auditRec.AddMeta("boardsCount", len(newBab.Boards))
|
|
|
|
auditRec.AddMeta("blocksCount", len(newBab.Blocks))
|
|
|
|
|
|
|
|
// create boards and blocks
|
|
|
|
bab, err := a.app.CreateBoardsAndBlocks(newBab, userID, true)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("CreateBoardsAndBlocks",
|
|
|
|
mlog.String("teamID", teamID),
|
|
|
|
mlog.String("userID", userID),
|
|
|
|
mlog.Int("boardCount", len(bab.Boards)),
|
|
|
|
mlog.Int("blockCount", len(bab.Blocks)),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(bab)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, err.Error(), err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handlePatchBoardsAndBlocks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation PATCH /api/v1/boards-and-blocks patchBoardsAndBlocks
|
|
|
|
//
|
|
|
|
// Patches a set of related boards and blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: the patches for the boards and blocks
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/PatchBoardsAndBlocks"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// schema:
|
|
|
|
// $ref: '#/definitions/BoardsAndBlocks'
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var pbab *model.PatchBoardsAndBlocks
|
|
|
|
if err = json.Unmarshal(requestBody, &pbab); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = pbab.IsValid(); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
teamID := ""
|
|
|
|
boardIDMap := map[string]bool{}
|
|
|
|
for i, boardID := range pbab.BoardIDs {
|
|
|
|
boardIDMap[boardID] = true
|
|
|
|
patch := pbab.BoardPatches[i]
|
|
|
|
|
|
|
|
if err = patch.IsValid(); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardProperties) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modifying board properties"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if patch.Type != nil {
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionManageBoardType) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modifying board type"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
board, err2 := a.app.GetBoard(boardID)
|
|
|
|
if err2 != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if teamID == "" {
|
|
|
|
teamID = board.TeamID
|
|
|
|
}
|
|
|
|
if teamID != board.TeamID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, blockID := range pbab.BlockIDs {
|
|
|
|
block, err2 := a.app.GetBlockByID(blockID)
|
|
|
|
if err2 != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := boardIDMap[block.BoardID]; !ok {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
2022-04-05 17:00:04 +02:00
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, block.BoardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modifying cards"})
|
|
|
|
return
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "patchBoardsAndBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardsCount", len(pbab.BoardIDs))
|
|
|
|
auditRec.AddMeta("blocksCount", len(pbab.BlockIDs))
|
|
|
|
|
|
|
|
bab, err := a.app.PatchBoardsAndBlocks(pbab, userID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("PATCH BoardsAndBlocks",
|
|
|
|
mlog.Int("boardsCount", len(pbab.BoardIDs)),
|
|
|
|
mlog.Int("blocksCount", len(pbab.BlockIDs)),
|
|
|
|
)
|
|
|
|
|
|
|
|
data, err := json.Marshal(bab)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonBytesResponse(w, http.StatusOK, data)
|
|
|
|
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *API) handleDeleteBoardsAndBlocks(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// swagger:operation DELETE /api/v1/boards-and-blocks deleteBoardsAndBlocks
|
|
|
|
//
|
|
|
|
// Deletes boards and blocks
|
|
|
|
//
|
|
|
|
// ---
|
|
|
|
// produces:
|
|
|
|
// - application/json
|
|
|
|
// parameters:
|
|
|
|
// - name: Body
|
|
|
|
// in: body
|
|
|
|
// description: the boards and blocks to delete
|
|
|
|
// required: true
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/DeleteBoardsAndBlocks"
|
|
|
|
// security:
|
|
|
|
// - BearerAuth: []
|
|
|
|
// responses:
|
|
|
|
// '200':
|
|
|
|
// description: success
|
|
|
|
// default:
|
|
|
|
// description: internal error
|
|
|
|
// schema:
|
|
|
|
// "$ref": "#/definitions/ErrorResponse"
|
|
|
|
|
|
|
|
userID := getUserID(r)
|
|
|
|
|
|
|
|
requestBody, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var dbab *model.DeleteBoardsAndBlocks
|
|
|
|
if err = json.Unmarshal(requestBody, &dbab); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// user must have permission to delete all the boards, and that
|
|
|
|
// would include the permission to manage their blocks
|
|
|
|
teamID := ""
|
2022-04-05 17:00:04 +02:00
|
|
|
boardIDMap := map[string]bool{}
|
2022-03-22 15:24:34 +01:00
|
|
|
for _, boardID := range dbab.Boards {
|
2022-04-05 17:00:04 +02:00
|
|
|
boardIDMap[boardID] = true
|
2022-03-22 15:24:34 +01:00
|
|
|
// all boards in the request should belong to the same team
|
|
|
|
board, err := a.app.GetBoard(boardID)
|
|
|
|
if err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if board == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if teamID == "" {
|
|
|
|
teamID = board.TeamID
|
|
|
|
}
|
|
|
|
if teamID != board.TeamID {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// permission check
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionDeleteBoard) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to delete board"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 17:00:04 +02:00
|
|
|
for _, blockID := range dbab.Blocks {
|
|
|
|
block, err2 := a.app.GetBlockByID(blockID)
|
|
|
|
if err2 != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if block == nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := boardIDMap[block.BoardID]; !ok {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", nil)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !a.permissions.HasPermissionToBoard(userID, block.BoardID, model.PermissionManageBoardCards) {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to modifying cards"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 15:24:34 +01:00
|
|
|
if err := dbab.IsValid(); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusBadRequest, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
auditRec := a.makeAuditRecord(r, "deleteBoardsAndBlocks", audit.Fail)
|
|
|
|
defer a.audit.LogRecord(audit.LevelModify, auditRec)
|
|
|
|
auditRec.AddMeta("boardsCount", len(dbab.Boards))
|
|
|
|
auditRec.AddMeta("blocksCount", len(dbab.Blocks))
|
|
|
|
|
|
|
|
if err := a.app.DeleteBoardsAndBlocks(dbab, userID); err != nil {
|
|
|
|
a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
a.logger.Debug("DELETE BoardsAndBlocks",
|
|
|
|
mlog.Int("boardsCount", len(dbab.Boards)),
|
|
|
|
mlog.Int("blocksCount", len(dbab.Blocks)),
|
|
|
|
)
|
|
|
|
|
|
|
|
// response
|
|
|
|
jsonStringResponse(w, http.StatusOK, "{}")
|
|
|
|
auditRec.Success()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response helpers
|
|
|
|
|
|
|
|
func (a *API) errorResponse(w http.ResponseWriter, api string, code int, message string, sourceError error) {
|
2022-04-06 13:54:40 +02:00
|
|
|
if code == http.StatusUnauthorized {
|
|
|
|
a.logger.Debug("API DEBUG",
|
|
|
|
mlog.Int("code", code),
|
|
|
|
mlog.Err(sourceError),
|
|
|
|
mlog.String("msg", message),
|
|
|
|
mlog.String("api", api),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
a.logger.Error("API ERROR",
|
|
|
|
mlog.Int("code", code),
|
|
|
|
mlog.Err(sourceError),
|
|
|
|
mlog.String("msg", message),
|
|
|
|
mlog.String("api", api),
|
|
|
|
)
|
|
|
|
}
|
2022-03-22 15:24:34 +01:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
data, err := json.Marshal(model.ErrorResponse{Error: message, ErrorCode: code})
|
|
|
|
if err != nil {
|
|
|
|
data = []byte("{}")
|
|
|
|
}
|
|
|
|
w.WriteHeader(code)
|
|
|
|
_, _ = w.Write(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonStringResponse(w http.ResponseWriter, code int, message string) { //nolint:unparam
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
fmt.Fprint(w, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
func jsonBytesResponse(w http.ResponseWriter, code int, json []byte) { //nolint:unparam
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
_, _ = w.Write(json)
|
2021-09-08 06:52:03 +02:00
|
|
|
}
|