LibraryPanels: Refactor to use context.Context instead of models.ReqContext (#39561)

This commit is contained in:
Agnès Toulet
2021-09-27 09:04:36 +02:00
committed by GitHub
parent 1d1da09568
commit 57b20335e6
12 changed files with 157 additions and 163 deletions

View File

@@ -26,7 +26,7 @@ func (l *LibraryElementService) registerAPIEndpoints() {
// createHandler handles POST /api/library-elements.
func (l *LibraryElementService) createHandler(c *models.ReqContext, cmd CreateLibraryElementCommand) response.Response {
element, err := l.createLibraryElement(c, cmd)
element, err := l.createLibraryElement(c.Req.Context(), c.SignedInUser, cmd)
if err != nil {
return toLibraryElementError(err, "Failed to create library element")
}
@@ -36,7 +36,7 @@ func (l *LibraryElementService) createHandler(c *models.ReqContext, cmd CreateLi
// deleteHandler handles DELETE /api/library-elements/:uid.
func (l *LibraryElementService) deleteHandler(c *models.ReqContext) response.Response {
err := l.deleteLibraryElement(c, macaron.Params(c.Req)[":uid"])
err := l.deleteLibraryElement(c.Req.Context(), c.SignedInUser, macaron.Params(c.Req)[":uid"])
if err != nil {
return toLibraryElementError(err, "Failed to delete library element")
}
@@ -46,7 +46,7 @@ func (l *LibraryElementService) deleteHandler(c *models.ReqContext) response.Res
// getHandler handles GET /api/library-elements/:uid.
func (l *LibraryElementService) getHandler(c *models.ReqContext) response.Response {
element, err := l.getLibraryElementByUid(c, macaron.Params(c.Req)[":uid"])
element, err := l.getLibraryElementByUid(c.Req.Context(), c.SignedInUser, macaron.Params(c.Req)[":uid"])
if err != nil {
return toLibraryElementError(err, "Failed to get library element")
}
@@ -66,7 +66,7 @@ func (l *LibraryElementService) getAllHandler(c *models.ReqContext) response.Res
excludeUID: c.Query("excludeUid"),
folderFilter: c.Query("folderFilter"),
}
elementsResult, err := l.getAllLibraryElements(c, query)
elementsResult, err := l.getAllLibraryElements(c.Req.Context(), c.SignedInUser, query)
if err != nil {
return toLibraryElementError(err, "Failed to get library elements")
}
@@ -76,7 +76,7 @@ func (l *LibraryElementService) getAllHandler(c *models.ReqContext) response.Res
// patchHandler handles PATCH /api/library-elements/:uid
func (l *LibraryElementService) patchHandler(c *models.ReqContext, cmd patchLibraryElementCommand) response.Response {
element, err := l.patchLibraryElement(c, cmd, macaron.Params(c.Req)[":uid"])
element, err := l.patchLibraryElement(c.Req.Context(), c.SignedInUser, cmd, macaron.Params(c.Req)[":uid"])
if err != nil {
return toLibraryElementError(err, "Failed to update library element")
}
@@ -86,7 +86,7 @@ func (l *LibraryElementService) patchHandler(c *models.ReqContext, cmd patchLibr
// getConnectionsHandler handles GET /api/library-panels/:uid/connections/.
func (l *LibraryElementService) getConnectionsHandler(c *models.ReqContext) response.Response {
connections, err := l.getConnections(c, macaron.Params(c.Req)[":uid"])
connections, err := l.getConnections(c.Req.Context(), c.SignedInUser, macaron.Params(c.Req)[":uid"])
if err != nil {
return toLibraryElementError(err, "Failed to get connections")
}
@@ -96,7 +96,7 @@ func (l *LibraryElementService) getConnectionsHandler(c *models.ReqContext) resp
// getByNameHandler handles GET /api/library-elements/name/:name/.
func (l *LibraryElementService) getByNameHandler(c *models.ReqContext) response.Response {
elements, err := l.getLibraryElementsByName(c)
elements, err := l.getLibraryElementsByName(c.Req.Context(), c.SignedInUser, macaron.Params(c.Req)[":name"])
if err != nil {
return toLibraryElementError(err, "Failed to get library element")
}

View File

@@ -14,7 +14,6 @@ import (
"github.com/grafana/grafana/pkg/services/sqlstore"
"github.com/grafana/grafana/pkg/services/sqlstore/migrator"
"github.com/grafana/grafana/pkg/util"
"gopkg.in/macaron.v1"
)
const (
@@ -91,7 +90,7 @@ func getLibraryElement(dialect migrator.Dialect, session *sqlstore.DBSession, ui
}
// createLibraryElement adds a library element.
func (l *LibraryElementService) createLibraryElement(c *models.ReqContext, cmd CreateLibraryElementCommand) (LibraryElementDTO, error) {
func (l *LibraryElementService) createLibraryElement(c context.Context, signedInUser *models.SignedInUser, cmd CreateLibraryElementCommand) (LibraryElementDTO, error) {
if err := l.requireSupportedElementKind(cmd.Kind); err != nil {
return LibraryElementDTO{}, err
}
@@ -106,7 +105,7 @@ func (l *LibraryElementService) createLibraryElement(c *models.ReqContext, cmd C
}
}
element := LibraryElement{
OrgID: c.SignedInUser.OrgId,
OrgID: signedInUser.OrgId,
FolderID: cmd.FolderID,
UID: createUID,
Name: cmd.Name,
@@ -117,16 +116,16 @@ func (l *LibraryElementService) createLibraryElement(c *models.ReqContext, cmd C
Created: time.Now(),
Updated: time.Now(),
CreatedBy: c.SignedInUser.UserId,
UpdatedBy: c.SignedInUser.UserId,
CreatedBy: signedInUser.UserId,
UpdatedBy: signedInUser.UserId,
}
if err := syncFieldsWithModel(&element); err != nil {
return LibraryElementDTO{}, err
}
err := l.SQLStore.WithTransactionalDbSession(c.Req.Context(), func(session *sqlstore.DBSession) error {
if err := l.requirePermissionsOnFolder(c.Req.Context(), c.SignedInUser, cmd.FolderID); err != nil {
err := l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
if err := l.requirePermissionsOnFolder(c, signedInUser, cmd.FolderID); err != nil {
return err
}
if _, err := session.Insert(&element); err != nil {
@@ -155,13 +154,13 @@ func (l *LibraryElementService) createLibraryElement(c *models.ReqContext, cmd C
Updated: element.Updated,
CreatedBy: LibraryElementDTOMetaUser{
ID: element.CreatedBy,
Name: c.SignedInUser.Login,
AvatarURL: dtos.GetGravatarUrl(c.SignedInUser.Email),
Name: signedInUser.Login,
AvatarURL: dtos.GetGravatarUrl(signedInUser.Email),
},
UpdatedBy: LibraryElementDTOMetaUser{
ID: element.UpdatedBy,
Name: c.SignedInUser.Login,
AvatarURL: dtos.GetGravatarUrl(c.SignedInUser.Email),
Name: signedInUser.Login,
AvatarURL: dtos.GetGravatarUrl(signedInUser.Email),
},
},
}
@@ -170,13 +169,13 @@ func (l *LibraryElementService) createLibraryElement(c *models.ReqContext, cmd C
}
// deleteLibraryElement deletes a library element.
func (l *LibraryElementService) deleteLibraryElement(c *models.ReqContext, uid string) error {
return l.SQLStore.WithTransactionalDbSession(c.Req.Context(), func(session *sqlstore.DBSession) error {
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, c.SignedInUser.OrgId)
func (l *LibraryElementService) deleteLibraryElement(c context.Context, signedInUser *models.SignedInUser, uid string) error {
return l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgId)
if err != nil {
return err
}
if err := l.requirePermissionsOnFolder(c.Req.Context(), c.SignedInUser, element.FolderID); err != nil {
if err := l.requirePermissionsOnFolder(c, signedInUser, element.FolderID); err != nil {
return err
}
var connectionIDs []struct {
@@ -204,9 +203,9 @@ func (l *LibraryElementService) deleteLibraryElement(c *models.ReqContext, uid s
}
// getLibraryElements gets a Library Element where param == value
func getLibraryElements(c *models.ReqContext, store *sqlstore.SQLStore, params []Pair) ([]LibraryElementDTO, error) {
func getLibraryElements(c context.Context, store *sqlstore.SQLStore, signedInUser *models.SignedInUser, params []Pair) ([]LibraryElementDTO, error) {
libraryElements := make([]LibraryElementWithMeta, 0)
err := store.WithDbSession(c.Req.Context(), func(session *sqlstore.DBSession) error {
err := store.WithDbSession(c, func(session *sqlstore.DBSession) error {
builder := sqlstore.SQLBuilder{}
builder.Write(selectLibraryElementDTOWithMeta)
builder.Write(", 'General' as folder_name ")
@@ -220,8 +219,8 @@ func getLibraryElements(c *models.ReqContext, store *sqlstore.SQLStore, params [
builder.Write(getFromLibraryElementDTOWithMeta(store.Dialect))
builder.Write(" INNER JOIN dashboard AS dashboard on le.folder_id = dashboard.id AND le.folder_id <> 0")
writeParamSelectorSQL(&builder, params...)
if c.SignedInUser.OrgRole != models.ROLE_ADMIN {
builder.WriteDashboardPermissionFilter(c.SignedInUser, models.PERMISSION_VIEW)
if signedInUser.OrgRole != models.ROLE_ADMIN {
builder.WriteDashboardPermissionFilter(signedInUser, models.PERMISSION_VIEW)
}
builder.Write(` OR dashboard.id=0`)
if err := session.SQL(builder.GetSQLString(), builder.GetParams()...).Find(&libraryElements); err != nil {
@@ -274,8 +273,8 @@ func getLibraryElements(c *models.ReqContext, store *sqlstore.SQLStore, params [
}
// getLibraryElementByUid gets a Library Element by uid.
func (l *LibraryElementService) getLibraryElementByUid(c *models.ReqContext, UID string) (LibraryElementDTO, error) {
libraryElements, err := getLibraryElements(c, l.SQLStore, []Pair{{key: "org_id", value: c.SignedInUser.OrgId}, {key: "uid", value: UID}})
func (l *LibraryElementService) getLibraryElementByUid(c context.Context, signedInUser *models.SignedInUser, UID string) (LibraryElementDTO, error) {
libraryElements, err := getLibraryElements(c, l.SQLStore, signedInUser, []Pair{{key: "org_id", value: signedInUser.OrgId}, {key: "uid", value: UID}})
if err != nil {
return LibraryElementDTO{}, err
}
@@ -287,12 +286,12 @@ func (l *LibraryElementService) getLibraryElementByUid(c *models.ReqContext, UID
}
// getLibraryElementByName gets a Library Element by name.
func (l *LibraryElementService) getLibraryElementsByName(c *models.ReqContext) ([]LibraryElementDTO, error) {
return getLibraryElements(c, l.SQLStore, []Pair{{"org_id", c.SignedInUser.OrgId}, {"name", macaron.Params(c.Req)[":name"]}})
func (l *LibraryElementService) getLibraryElementsByName(c context.Context, signedInUser *models.SignedInUser, name string) ([]LibraryElementDTO, error) {
return getLibraryElements(c, l.SQLStore, signedInUser, []Pair{{"org_id", signedInUser.OrgId}, {"name", name}})
}
// getAllLibraryElements gets all Library Elements.
func (l *LibraryElementService) getAllLibraryElements(c *models.ReqContext, query searchLibraryElementsQuery) (LibraryElementSearchResult, error) {
func (l *LibraryElementService) getAllLibraryElements(c context.Context, signedInUser *models.SignedInUser, query searchLibraryElementsQuery) (LibraryElementSearchResult, error) {
elements := make([]LibraryElementWithMeta, 0)
result := LibraryElementSearchResult{}
if query.perPage <= 0 {
@@ -309,14 +308,14 @@ func (l *LibraryElementService) getAllLibraryElements(c *models.ReqContext, quer
if folderFilter.parseError != nil {
return LibraryElementSearchResult{}, folderFilter.parseError
}
err := l.SQLStore.WithDbSession(c.Context.Req.Context(), func(session *sqlstore.DBSession) error {
err := l.SQLStore.WithDbSession(c, func(session *sqlstore.DBSession) error {
builder := sqlstore.SQLBuilder{}
if folderFilter.includeGeneralFolder {
builder.Write(selectLibraryElementDTOWithMeta)
builder.Write(", 'General' as folder_name ")
builder.Write(", '' as folder_uid ")
builder.Write(getFromLibraryElementDTOWithMeta(l.SQLStore.Dialect))
builder.Write(` WHERE le.org_id=? AND le.folder_id=0`, c.SignedInUser.OrgId)
builder.Write(` WHERE le.org_id=? AND le.folder_id=0`, signedInUser.OrgId)
writeKindSQL(query, &builder)
writeSearchStringSQL(query, l.SQLStore, &builder)
writeExcludeSQL(query, &builder)
@@ -328,7 +327,7 @@ func (l *LibraryElementService) getAllLibraryElements(c *models.ReqContext, quer
builder.Write(", dashboard.uid as folder_uid ")
builder.Write(getFromLibraryElementDTOWithMeta(l.SQLStore.Dialect))
builder.Write(" INNER JOIN dashboard AS dashboard on le.folder_id = dashboard.id AND le.folder_id<>0")
builder.Write(` WHERE le.org_id=?`, c.SignedInUser.OrgId)
builder.Write(` WHERE le.org_id=?`, signedInUser.OrgId)
writeKindSQL(query, &builder)
writeSearchStringSQL(query, l.SQLStore, &builder)
writeExcludeSQL(query, &builder)
@@ -336,8 +335,8 @@ func (l *LibraryElementService) getAllLibraryElements(c *models.ReqContext, quer
if err := folderFilter.writeFolderFilterSQL(false, &builder); err != nil {
return err
}
if c.SignedInUser.OrgRole != models.ROLE_ADMIN {
builder.WriteDashboardPermissionFilter(c.SignedInUser, models.PERMISSION_VIEW)
if signedInUser.OrgRole != models.ROLE_ADMIN {
builder.WriteDashboardPermissionFilter(signedInUser, models.PERMISSION_VIEW)
}
if query.sortDirection == search.SortAlphaDesc.Name {
builder.Write(" ORDER BY 1 DESC")
@@ -385,7 +384,7 @@ func (l *LibraryElementService) getAllLibraryElements(c *models.ReqContext, quer
var libraryElements []LibraryElement
countBuilder := sqlstore.SQLBuilder{}
countBuilder.Write("SELECT * FROM library_element AS le")
countBuilder.Write(` WHERE le.org_id=?`, c.SignedInUser.OrgId)
countBuilder.Write(` WHERE le.org_id=?`, signedInUser.OrgId)
writeKindSQL(query, &countBuilder)
writeSearchStringSQL(query, l.SQLStore, &countBuilder)
writeExcludeSQL(query, &countBuilder)
@@ -434,13 +433,13 @@ func (l *LibraryElementService) handleFolderIDPatches(ctx context.Context, eleme
}
// patchLibraryElement updates a Library Element.
func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd patchLibraryElementCommand, uid string) (LibraryElementDTO, error) {
func (l *LibraryElementService) patchLibraryElement(c context.Context, signedInUser *models.SignedInUser, cmd patchLibraryElementCommand, uid string) (LibraryElementDTO, error) {
var dto LibraryElementDTO
if err := l.requireSupportedElementKind(cmd.Kind); err != nil {
return LibraryElementDTO{}, err
}
err := l.SQLStore.WithTransactionalDbSession(c.Req.Context(), func(session *sqlstore.DBSession) error {
elementInDB, err := getLibraryElement(l.SQLStore.Dialect, session, uid, c.SignedInUser.OrgId)
err := l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
elementInDB, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgId)
if err != nil {
return err
}
@@ -457,7 +456,7 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
return errLibraryElementUIDTooLong
}
_, err := getLibraryElement(l.SQLStore.Dialect, session, updateUID, c.SignedInUser.OrgId)
_, err := getLibraryElement(l.SQLStore.Dialect, session, updateUID, signedInUser.OrgId)
if !errors.Is(err, ErrLibraryElementNotFound) {
return errLibraryElementAlreadyExists
}
@@ -465,7 +464,7 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
var libraryElement = LibraryElement{
ID: elementInDB.ID,
OrgID: c.SignedInUser.OrgId,
OrgID: signedInUser.OrgId,
FolderID: cmd.FolderID,
UID: updateUID,
Name: cmd.Name,
@@ -477,7 +476,7 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
Created: elementInDB.Created,
CreatedBy: elementInDB.CreatedBy,
Updated: time.Now(),
UpdatedBy: c.SignedInUser.UserId,
UpdatedBy: signedInUser.UserId,
}
if cmd.Name == "" {
@@ -486,7 +485,7 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
if cmd.Model == nil {
libraryElement.Model = elementInDB.Model
}
if err := l.handleFolderIDPatches(c.Req.Context(), &libraryElement, elementInDB.FolderID, cmd.FolderID, c.SignedInUser); err != nil {
if err := l.handleFolderIDPatches(c, &libraryElement, elementInDB.FolderID, cmd.FolderID, signedInUser); err != nil {
return err
}
if err := syncFieldsWithModel(&libraryElement); err != nil {
@@ -523,8 +522,8 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
},
UpdatedBy: LibraryElementDTOMetaUser{
ID: libraryElement.UpdatedBy,
Name: c.SignedInUser.Login,
AvatarURL: dtos.GetGravatarUrl(c.SignedInUser.Email),
Name: signedInUser.Login,
AvatarURL: dtos.GetGravatarUrl(signedInUser.Email),
},
},
}
@@ -536,10 +535,10 @@ func (l *LibraryElementService) patchLibraryElement(c *models.ReqContext, cmd pa
}
// getConnections gets all connections for a Library Element.
func (l *LibraryElementService) getConnections(c *models.ReqContext, uid string) ([]LibraryElementConnectionDTO, error) {
func (l *LibraryElementService) getConnections(c context.Context, signedInUser *models.SignedInUser, uid string) ([]LibraryElementConnectionDTO, error) {
connections := make([]LibraryElementConnectionDTO, 0)
err := l.SQLStore.WithDbSession(c.Context.Req.Context(), func(session *sqlstore.DBSession) error {
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, c.SignedInUser.OrgId)
err := l.SQLStore.WithDbSession(c, func(session *sqlstore.DBSession) error {
element, err := getLibraryElement(l.SQLStore.Dialect, session, uid, signedInUser.OrgId)
if err != nil {
return err
}
@@ -550,8 +549,8 @@ func (l *LibraryElementService) getConnections(c *models.ReqContext, uid string)
builder.Write(" LEFT JOIN " + l.SQLStore.Dialect.Quote("user") + " AS u1 ON lec.created_by = u1.id")
builder.Write(" INNER JOIN dashboard AS dashboard on lec.connection_id = dashboard.id")
builder.Write(` WHERE lec.element_id=?`, element.ID)
if c.SignedInUser.OrgRole != models.ROLE_ADMIN {
builder.WriteDashboardPermissionFilter(c.SignedInUser, models.PERMISSION_VIEW)
if signedInUser.OrgRole != models.ROLE_ADMIN {
builder.WriteDashboardPermissionFilter(signedInUser, models.PERMISSION_VIEW)
}
if err := session.SQL(builder.GetSQLString(), builder.GetParams()...).Find(&libraryElementConnections); err != nil {
return err
@@ -579,9 +578,9 @@ func (l *LibraryElementService) getConnections(c *models.ReqContext, uid string)
}
//getElementsForDashboardID gets all elements for a specific dashboard
func (l *LibraryElementService) getElementsForDashboardID(c *models.ReqContext, dashboardID int64) (map[string]LibraryElementDTO, error) {
func (l *LibraryElementService) getElementsForDashboardID(c context.Context, dashboardID int64) (map[string]LibraryElementDTO, error) {
libraryElementMap := make(map[string]LibraryElementDTO)
err := l.SQLStore.WithDbSession(c.Context.Req.Context(), func(session *sqlstore.DBSession) error {
err := l.SQLStore.WithDbSession(c, func(session *sqlstore.DBSession) error {
var libraryElements []LibraryElementWithMeta
sql := selectLibraryElementDTOWithMeta +
", coalesce(dashboard.title, 'General') AS folder_name" +
@@ -634,18 +633,18 @@ func (l *LibraryElementService) getElementsForDashboardID(c *models.ReqContext,
}
// connectElementsToDashboardID adds connections for all elements Library Elements in a Dashboard.
func (l *LibraryElementService) connectElementsToDashboardID(c *models.ReqContext, elementUIDs []string, dashboardID int64) error {
err := l.SQLStore.WithTransactionalDbSession(c.Req.Context(), func(session *sqlstore.DBSession) error {
func (l *LibraryElementService) connectElementsToDashboardID(c context.Context, signedInUser *models.SignedInUser, elementUIDs []string, dashboardID int64) error {
err := l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
_, err := session.Exec("DELETE FROM "+models.LibraryElementConnectionTableName+" WHERE kind=1 AND connection_id=?", dashboardID)
if err != nil {
return err
}
for _, elementUID := range elementUIDs {
element, err := getLibraryElement(l.SQLStore.Dialect, session, elementUID, c.SignedInUser.OrgId)
element, err := getLibraryElement(l.SQLStore.Dialect, session, elementUID, signedInUser.OrgId)
if err != nil {
return err
}
if err := l.requirePermissionsOnFolder(c.Req.Context(), c.SignedInUser, element.FolderID); err != nil {
if err := l.requirePermissionsOnFolder(c, signedInUser, element.FolderID); err != nil {
return err
}
@@ -654,7 +653,7 @@ func (l *LibraryElementService) connectElementsToDashboardID(c *models.ReqContex
Kind: 1,
ConnectionID: dashboardID,
Created: time.Now(),
CreatedBy: c.SignedInUser.UserId,
CreatedBy: signedInUser.UserId,
}
if _, err := session.Insert(&connection); err != nil {
if l.SQLStore.Dialect.IsUniqueConstraintViolation(err) {
@@ -670,8 +669,8 @@ func (l *LibraryElementService) connectElementsToDashboardID(c *models.ReqContex
}
// disconnectElementsFromDashboardID deletes connections for all Library Elements in a Dashboard.
func (l *LibraryElementService) disconnectElementsFromDashboardID(c *models.ReqContext, dashboardID int64) error {
return l.SQLStore.WithTransactionalDbSession(c.Context.Req.Context(), func(session *sqlstore.DBSession) error {
func (l *LibraryElementService) disconnectElementsFromDashboardID(c context.Context, dashboardID int64) error {
return l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
_, err := session.Exec("DELETE FROM "+models.LibraryElementConnectionTableName+" WHERE kind=1 AND connection_id=?", dashboardID)
if err != nil {
return err
@@ -681,12 +680,12 @@ func (l *LibraryElementService) disconnectElementsFromDashboardID(c *models.ReqC
}
// deleteLibraryElementsInFolderUID deletes all Library Elements in a folder.
func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c *models.ReqContext, folderUID string) error {
return l.SQLStore.WithTransactionalDbSession(c.Req.Context(), func(session *sqlstore.DBSession) error {
func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c context.Context, signedInUser *models.SignedInUser, folderUID string) error {
return l.SQLStore.WithTransactionalDbSession(c, func(session *sqlstore.DBSession) error {
var folderUIDs []struct {
ID int64 `xorm:"id"`
}
err := session.SQL("SELECT id from dashboard WHERE uid=? AND org_id=? AND is_folder=?", folderUID, c.SignedInUser.OrgId, l.SQLStore.Dialect.BooleanStr(true)).Find(&folderUIDs)
err := session.SQL("SELECT id from dashboard WHERE uid=? AND org_id=? AND is_folder=?", folderUID, signedInUser.OrgId, l.SQLStore.Dialect.BooleanStr(true)).Find(&folderUIDs)
if err != nil {
return err
}
@@ -695,7 +694,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c *models.ReqCo
}
folderID := folderUIDs[0].ID
if err := l.requirePermissionsOnFolder(c.Req.Context(), c.SignedInUser, folderID); err != nil {
if err := l.requirePermissionsOnFolder(c, signedInUser, folderID); err != nil {
return err
}
var connectionIDs []struct {
@@ -704,7 +703,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c *models.ReqCo
sql := "SELECT lec.connection_id FROM library_element AS le"
sql += " INNER JOIN " + models.LibraryElementConnectionTableName + " AS lec on le.id = lec.element_id"
sql += " WHERE le.folder_id=? AND le.org_id=?"
err = session.SQL(sql, folderID, c.SignedInUser.OrgId).Find(&connectionIDs)
err = session.SQL(sql, folderID, signedInUser.OrgId).Find(&connectionIDs)
if err != nil {
return err
}
@@ -715,7 +714,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c *models.ReqCo
var elementIDs []struct {
ID int64 `xorm:"id"`
}
err = session.SQL("SELECT id from library_element WHERE folder_id=? AND org_id=?", folderID, c.SignedInUser.OrgId).Find(&elementIDs)
err = session.SQL("SELECT id from library_element WHERE folder_id=? AND org_id=?", folderID, signedInUser.OrgId).Find(&elementIDs)
if err != nil {
return err
}
@@ -725,7 +724,7 @@ func (l *LibraryElementService) deleteLibraryElementsInFolderUID(c *models.ReqCo
return err
}
}
if _, err := session.Exec("DELETE FROM library_element WHERE folder_id=? AND org_id=?", folderID, c.SignedInUser.OrgId); err != nil {
if _, err := session.Exec("DELETE FROM library_element WHERE folder_id=? AND org_id=?", folderID, signedInUser.OrgId); err != nil {
return err
}

View File

@@ -1,6 +1,8 @@
package libraryelements
import (
"context"
"github.com/grafana/grafana/pkg/api/routing"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/models"
@@ -21,12 +23,12 @@ func ProvideService(cfg *setting.Cfg, sqlStore *sqlstore.SQLStore, routeRegister
// Service is a service for operating on library elements.
type Service interface {
CreateElement(c *models.ReqContext, cmd CreateLibraryElementCommand) (LibraryElementDTO, error)
GetElement(c *models.ReqContext, UID string) (LibraryElementDTO, error)
GetElementsForDashboard(c *models.ReqContext, dashboardID int64) (map[string]LibraryElementDTO, error)
ConnectElementsToDashboard(c *models.ReqContext, elementUIDs []string, dashboardID int64) error
DisconnectElementsFromDashboard(c *models.ReqContext, dashboardID int64) error
DeleteLibraryElementsInFolder(c *models.ReqContext, folderUID string) error
CreateElement(c context.Context, signedInUser *models.SignedInUser, cmd CreateLibraryElementCommand) (LibraryElementDTO, error)
GetElement(c context.Context, signedInUser *models.SignedInUser, UID string) (LibraryElementDTO, error)
GetElementsForDashboard(c context.Context, dashboardID int64) (map[string]LibraryElementDTO, error)
ConnectElementsToDashboard(c context.Context, signedInUser *models.SignedInUser, elementUIDs []string, dashboardID int64) error
DisconnectElementsFromDashboard(c context.Context, dashboardID int64) error
DeleteLibraryElementsInFolder(c context.Context, signedInUser *models.SignedInUser, folderUID string) error
}
// LibraryElementService is the service for the Library Element feature.
@@ -38,31 +40,31 @@ type LibraryElementService struct {
}
// CreateElement creates a Library Element.
func (l *LibraryElementService) CreateElement(c *models.ReqContext, cmd CreateLibraryElementCommand) (LibraryElementDTO, error) {
return l.createLibraryElement(c, cmd)
func (l *LibraryElementService) CreateElement(c context.Context, signedInUser *models.SignedInUser, cmd CreateLibraryElementCommand) (LibraryElementDTO, error) {
return l.createLibraryElement(c, signedInUser, cmd)
}
// GetElement gets an element from a UID.
func (l *LibraryElementService) GetElement(c *models.ReqContext, UID string) (LibraryElementDTO, error) {
return l.getLibraryElementByUid(c, UID)
func (l *LibraryElementService) GetElement(c context.Context, signedInUser *models.SignedInUser, UID string) (LibraryElementDTO, error) {
return l.getLibraryElementByUid(c, signedInUser, UID)
}
// GetElementsForDashboard gets all connected elements for a specific dashboard.
func (l *LibraryElementService) GetElementsForDashboard(c *models.ReqContext, dashboardID int64) (map[string]LibraryElementDTO, error) {
func (l *LibraryElementService) GetElementsForDashboard(c context.Context, dashboardID int64) (map[string]LibraryElementDTO, error) {
return l.getElementsForDashboardID(c, dashboardID)
}
// ConnectElementsToDashboard connects elements to a specific dashboard.
func (l *LibraryElementService) ConnectElementsToDashboard(c *models.ReqContext, elementUIDs []string, dashboardID int64) error {
return l.connectElementsToDashboardID(c, elementUIDs, dashboardID)
func (l *LibraryElementService) ConnectElementsToDashboard(c context.Context, signedInUser *models.SignedInUser, elementUIDs []string, dashboardID int64) error {
return l.connectElementsToDashboardID(c, signedInUser, elementUIDs, dashboardID)
}
// DisconnectElementsFromDashboard disconnects elements from a specific dashboard.
func (l *LibraryElementService) DisconnectElementsFromDashboard(c *models.ReqContext, dashboardID int64) error {
func (l *LibraryElementService) DisconnectElementsFromDashboard(c context.Context, dashboardID int64) error {
return l.disconnectElementsFromDashboardID(c, dashboardID)
}
// DeleteLibraryElementsInFolder deletes all elements for a specific folder.
func (l *LibraryElementService) DeleteLibraryElementsInFolder(c *models.ReqContext, folderUID string) error {
return l.deleteLibraryElementsInFolderUID(c, folderUID)
func (l *LibraryElementService) DeleteLibraryElementsInFolder(c context.Context, signedInUser *models.SignedInUser, folderUID string) error {
return l.deleteLibraryElementsInFolderUID(c, signedInUser, folderUID)
}

View File

@@ -67,7 +67,7 @@ func TestDeleteLibraryElement(t *testing.T) {
Data: simplejson.NewFromAny(dashJSON),
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
err := sc.service.ConnectElementsToDashboard(sc.reqContext, []string{sc.initialResult.Result.UID}, dashInDB.Id)
err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, dashInDB.Id)
require.NoError(t, err)
sc.ctx.Req = macaron.SetURLParams(sc.ctx.Req, map[string]string{":uid": sc.initialResult.Result.UID})

View File

@@ -120,7 +120,7 @@ func TestGetLibraryElement(t *testing.T) {
Data: simplejson.NewFromAny(dashJSON),
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
err := sc.service.ConnectElementsToDashboard(sc.reqContext, []string{sc.initialResult.Result.UID}, dashInDB.Id)
err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, dashInDB.Id)
require.NoError(t, err)
expected := func(res libraryElementResult) libraryElementResult {

View File

@@ -59,10 +59,10 @@ func TestDeleteLibraryPanelsInFolder(t *testing.T) {
Data: simplejson.NewFromAny(dashJSON),
}
dashInDB := createDashboard(t, sc.sqlStore, sc.user, &dash, sc.folder.Id)
err := sc.service.ConnectElementsToDashboard(sc.reqContext, []string{sc.initialResult.Result.UID}, dashInDB.Id)
err := sc.service.ConnectElementsToDashboard(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, []string{sc.initialResult.Result.UID}, dashInDB.Id)
require.NoError(t, err)
err = sc.service.DeleteLibraryElementsInFolder(sc.reqContext, sc.folder.Uid)
err = sc.service.DeleteLibraryElementsInFolder(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, sc.folder.Uid)
require.EqualError(t, err, ErrFolderHasConnectedLibraryElements.Error())
})
@@ -80,7 +80,7 @@ func TestDeleteLibraryPanelsInFolder(t *testing.T) {
require.NotNil(t, result.Result)
require.Equal(t, 2, len(result.Result.Elements))
err = sc.service.DeleteLibraryElementsInFolder(sc.reqContext, sc.folder.Uid)
err = sc.service.DeleteLibraryElementsInFolder(sc.reqContext.Req.Context(), sc.reqContext.SignedInUser, sc.folder.Uid)
require.NoError(t, err)
resp = sc.service.getAllHandler(sc.reqContext)
require.Equal(t, 200, resp.Status())