mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 10:03:33 -06:00
* Remove user from preferences, stars, orguser, team member * Fix lint * Add Delete user from org and dashboard acl * Delete user from user auth * Add DeleteUser to quota * Add test files and adjust user auth store * Rename package in wire for user auth * Import Quota Service interface in other services * do the same in tests * fix lint tests * Fix tests * Add some tests * Rename InsertUser and DeleteUser to InsertOrgUser and DeleteOrgUser * Rename DeleteUser to DeleteByUser in quota * changing a method name in few additional places * Fix in other places * Fix lint * Fix tests * Rename DeleteOrgUser to DeleteUserFromAll * Update pkg/services/org/orgimpl/org_test.go Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Update pkg/services/preference/prefimpl/inmemory_test.go Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com> * Rename Acl to ACL * Fix wire after merge with main * Move test to uni test Co-authored-by: Emil Tullstedt <emil.tullstedt@grafana.com>
79 lines
2.3 KiB
Go
79 lines
2.3 KiB
Go
package starimpl
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/services/sqlstore"
|
|
"github.com/grafana/grafana/pkg/services/sqlstore/db"
|
|
"github.com/grafana/grafana/pkg/services/star"
|
|
)
|
|
|
|
type store interface {
|
|
Get(context.Context, *star.IsStarredByUserQuery) (bool, error)
|
|
Insert(context.Context, *star.StarDashboardCommand) error
|
|
Delete(context.Context, *star.UnstarDashboardCommand) error
|
|
DeleteByUser(context.Context, int64) error
|
|
List(context.Context, *star.GetUserStarsQuery) (*star.GetUserStarsResult, error)
|
|
}
|
|
|
|
type sqlStore struct {
|
|
db db.DB
|
|
}
|
|
|
|
func (s *sqlStore) Get(ctx context.Context, query *star.IsStarredByUserQuery) (bool, error) {
|
|
var isStarred bool
|
|
err := s.db.WithDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
|
rawSQL := "SELECT 1 from star where user_id=? and dashboard_id=?"
|
|
results, err := sess.Query(rawSQL, query.UserID, query.DashboardID)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
isStarred = len(results) != 0
|
|
return nil
|
|
})
|
|
return isStarred, err
|
|
}
|
|
|
|
func (s *sqlStore) Insert(ctx context.Context, cmd *star.StarDashboardCommand) error {
|
|
return s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
|
entity := star.Star{
|
|
UserID: cmd.UserID,
|
|
DashboardID: cmd.DashboardID,
|
|
}
|
|
|
|
_, err := sess.Insert(&entity)
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (s *sqlStore) Delete(ctx context.Context, cmd *star.UnstarDashboardCommand) error {
|
|
return s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
|
var rawSQL = "DELETE FROM star WHERE user_id=? and dashboard_id=?"
|
|
_, err := sess.Exec(rawSQL, cmd.UserID, cmd.DashboardID)
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (s *sqlStore) DeleteByUser(ctx context.Context, userID int64) error {
|
|
return s.db.WithTransactionalDbSession(ctx, func(sess *sqlstore.DBSession) error {
|
|
var rawSQL = "DELETE FROM star WHERE user_id = ?"
|
|
_, err := sess.Exec(rawSQL, userID)
|
|
return err
|
|
})
|
|
}
|
|
|
|
func (s *sqlStore) List(ctx context.Context, query *star.GetUserStarsQuery) (*star.GetUserStarsResult, error) {
|
|
userStars := make(map[int64]bool)
|
|
err := s.db.WithDbSession(ctx, func(dbSession *sqlstore.DBSession) error {
|
|
var stars = make([]star.Star, 0)
|
|
err := dbSession.Where("user_id=?", query.UserID).Find(&stars)
|
|
for _, star := range stars {
|
|
userStars[star.DashboardID] = true
|
|
}
|
|
return err
|
|
})
|
|
return &star.GetUserStarsResult{UserStars: userStars}, err
|
|
}
|