2022-06-28 07:32:25 -05:00
package org
import (
"errors"
2022-08-10 04:56:48 -05:00
"strings"
2022-06-28 07:32:25 -05:00
"time"
2022-09-23 05:40:02 -05:00
"github.com/grafana/grafana/pkg/models/roletype"
2022-09-23 06:19:34 -05:00
"github.com/grafana/grafana/pkg/services/user"
2023-03-06 01:57:46 -06:00
"github.com/grafana/grafana/pkg/util/errutil"
2022-06-28 07:32:25 -05:00
)
// Typed errors
var (
2023-03-22 12:41:59 -05:00
ErrOrgNameTaken = errors . New ( "organization name is taken" )
ErrLastOrgAdmin = errors . New ( "cannot remove last organization admin" )
ErrOrgUserNotFound = errors . New ( "cannot find the organization user" )
ErrOrgUserAlreadyAdded = errors . New ( "user is already added to organization" )
ErrOrgNotFound = errutil . NewBase ( errutil . StatusNotFound , "org.notFound" , errutil . WithPublicMessage ( "organization not found" ) )
ErrCannotChangeRoleForExternallySyncedUser = errutil . NewBase ( errutil . StatusForbidden , "org.externallySynced" , errutil . WithPublicMessage ( "cannot change role for externally synced user" ) )
2022-06-28 07:32:25 -05:00
)
type Org struct {
ID int64 ` xorm:"pk autoincr 'id'" `
Version int
Name string
Address1 string
Address2 string
City string
ZipCode string
State string
Country string
Created time . Time
Updated time . Time
}
type OrgUser struct {
ID int64 ` xorm:"pk autoincr 'id'" `
OrgID int64 ` xorm:"org_id" `
UserID int64 ` xorm:"user_id" `
Role RoleType
Created time . Time
Updated time . Time
}
2022-09-23 05:40:02 -05:00
type RoleType = roletype . RoleType
2022-06-28 07:32:25 -05:00
const (
2022-08-10 04:56:48 -05:00
RoleViewer RoleType = "Viewer"
RoleEditor RoleType = "Editor"
RoleAdmin RoleType = "Admin"
2022-06-28 07:32:25 -05:00
)
type CreateOrgCommand struct {
Name string ` json:"name" binding:"Required" `
// initial admin user for account
2022-09-22 12:02:55 -05:00
UserID int64 ` json:"-" xorm:"user_id" `
2022-06-28 07:32:25 -05:00
}
type GetOrgIDForNewUserCommand struct {
Email string
Login string
OrgID int64
OrgName string
SkipOrgSetup bool
}
2022-08-10 04:56:48 -05:00
2022-08-16 10:50:45 -05:00
type GetUserOrgListQuery struct {
2022-09-22 12:02:55 -05:00
UserID int64 ` xorm:"user_id" `
2022-08-16 10:50:45 -05:00
}
type UserOrgDTO struct {
2022-09-22 12:02:55 -05:00
OrgID int64 ` json:"orgId" xorm:"org_id" `
2022-08-16 10:50:45 -05:00
Name string ` json:"name" `
Role RoleType ` json:"role" `
}
2022-08-23 12:26:21 -05:00
type UpdateOrgCommand struct {
Name string
OrgId int64
}
2022-09-20 02:55:40 -05:00
type SearchOrgsQuery struct {
Query string
Name string
Limit int
Page int
2022-09-22 12:02:55 -05:00
IDs [ ] int64 ` xorm:"ids" `
2022-09-20 02:55:40 -05:00
}
type OrgDTO struct {
2022-09-22 12:02:55 -05:00
ID int64 ` json:"id" xorm:"id" `
2022-09-20 02:55:40 -05:00
Name string ` json:"name" `
}
2023-01-04 09:20:26 -06:00
type GetOrgByIDQuery struct {
2022-09-20 05:57:51 -05:00
ID int64
}
type GetOrgByNameQuery struct {
Name string
}
2022-09-21 11:00:18 -05:00
type UpdateOrgAddressCommand struct {
OrgID int64 ` xorm:"org_id" `
Address
}
type Address struct {
Address1 string ` json:"address1" `
Address2 string ` json:"address2" `
City string ` json:"city" `
ZipCode string ` json:"zipCode" `
State string ` json:"state" `
Country string ` json:"country" `
}
type DeleteOrgCommand struct {
ID int64 ` xorm:"id" `
}
2022-09-23 02:58:17 -05:00
type AddOrgUserCommand struct {
LoginOrEmail string ` json:"loginOrEmail" binding:"Required" `
Role RoleType ` json:"role" binding:"Required" `
2022-09-27 03:34:31 -05:00
OrgID int64 ` json:"-" xorm:"org_id" `
UserID int64 ` json:"-" xorm:"user_id" `
2022-09-23 02:58:17 -05:00
// internal use: avoid adding service accounts to orgs via user routes
AllowAddingServiceAccount bool ` json:"-" `
}
type UpdateOrgUserCommand struct {
Role RoleType ` json:"role" binding:"Required" `
OrgID int64 ` json:"-" `
UserID int64 ` json:"-" `
}
type OrgUserDTO struct {
2023-03-22 12:41:59 -05:00
OrgID int64 ` json:"orgId" xorm:"org_id" `
UserID int64 ` json:"userId" xorm:"user_id" `
Email string ` json:"email" `
Name string ` json:"name" `
AvatarURL string ` json:"avatarUrl" xorm:"avatar_url" `
Login string ` json:"login" `
Role string ` json:"role" `
LastSeenAt time . Time ` json:"lastSeenAt" `
Updated time . Time ` json:"-" `
Created time . Time ` json:"-" `
LastSeenAtAge string ` json:"lastSeenAtAge" `
AccessControl map [ string ] bool ` json:"accessControl,omitempty" `
IsDisabled bool ` json:"isDisabled" `
AuthLabels [ ] string ` json:"authLabels" xorm:"-" `
IsExternallySynced bool ` json:"isExternallySynced" `
2022-09-23 02:58:17 -05:00
}
type RemoveOrgUserCommand struct {
2022-09-27 08:33:38 -05:00
UserID int64 ` xorm:"user_id" `
OrgID int64 ` xorm:"org_id" `
2022-09-23 02:58:17 -05:00
ShouldDeleteOrphanedUser bool
UserWasDeleted bool
}
2022-09-23 06:19:34 -05:00
type GetOrgUsersQuery struct {
2022-09-27 08:33:38 -05:00
UserID int64 ` xorm:"user_id" `
2022-09-27 03:34:31 -05:00
OrgID int64 ` xorm:"org_id" `
2022-09-23 06:19:34 -05:00
Query string
2023-01-09 02:54:33 -06:00
Page int
2022-09-23 06:19:34 -05:00
Limit int
// Flag used to allow oss edition to query users without access control
DontEnforceAccessControl bool
User * user . SignedInUser
}
type SearchOrgUsersQuery struct {
2023-01-09 02:54:33 -06:00
UserID int64 ` xorm:"user_id" `
OrgID int64 ` xorm:"org_id" `
Query string
Page int
Limit int
// Flag used to allow oss edition to query users without access control
DontEnforceAccessControl bool
2022-09-23 06:19:34 -05:00
User * user . SignedInUser
}
type SearchOrgUsersQueryResult struct {
TotalCount int64 ` json:"totalCount" `
2023-01-09 02:54:33 -06:00
OrgUsers [ ] * OrgUserDTO ` json:"orgUsers" `
2022-09-23 06:19:34 -05:00
Page int ` json:"page" `
PerPage int ` json:"perPage" `
}
2022-09-22 12:02:55 -05:00
type ByOrgName [ ] * UserOrgDTO
2023-01-04 09:20:26 -06:00
type OrgDetailsDTO struct {
ID int64 ` json:"id" `
Name string ` json:"name" `
Address Address ` json:"address" `
}
2022-09-22 12:02:55 -05:00
// Len returns the length of an array of organisations.
func ( o ByOrgName ) Len ( ) int {
return len ( o )
}
// Swap swaps two indices of an array of organizations.
func ( o ByOrgName ) Swap ( i , j int ) {
o [ i ] , o [ j ] = o [ j ] , o [ i ]
}
// Less returns whether element i of an array of organizations is less than element j.
func ( o ByOrgName ) Less ( i , j int ) bool {
if strings . ToLower ( o [ i ] . Name ) < strings . ToLower ( o [ j ] . Name ) {
return true
}
return o [ i ] . Name < o [ j ] . Name
}
2022-11-14 13:08:10 -06:00
const (
QuotaTargetSrv string = "org"
OrgQuotaTarget string = "org"
OrgUserQuotaTarget string = "org_user"
)