mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-26 09:09:36 -05:00
commit
5efdccd1d8
2 changed files with 37 additions and 19 deletions
|
@ -13,9 +13,12 @@ import (
|
||||||
"path"
|
"path"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
// Needed for the MySQL driver
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
|
|
||||||
|
// Needed for the Postgresql driver
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
|
|
||||||
"code.gitea.io/gitea/models/migrations"
|
"code.gitea.io/gitea/models/migrations"
|
||||||
|
@ -45,16 +48,22 @@ func sessionRelease(sess *xorm.Session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
x *xorm.Engine
|
x *xorm.Engine
|
||||||
tables []interface{}
|
tables []interface{}
|
||||||
|
|
||||||
|
// HasEngine specifies if we have a xorm.Engine
|
||||||
HasEngine bool
|
HasEngine bool
|
||||||
|
|
||||||
|
// DbCfg holds the database settings
|
||||||
DbCfg struct {
|
DbCfg struct {
|
||||||
Type, Host, Name, User, Passwd, Path, SSLMode string
|
Type, Host, Name, User, Passwd, Path, SSLMode string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EnableSQLite3 use SQLite3
|
||||||
EnableSQLite3 bool
|
EnableSQLite3 bool
|
||||||
EnableTiDB bool
|
|
||||||
|
// EnableTiDB enable TiDB
|
||||||
|
EnableTiDB bool
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -69,12 +78,13 @@ func init() {
|
||||||
new(Team), new(OrgUser), new(TeamUser), new(TeamRepo),
|
new(Team), new(OrgUser), new(TeamUser), new(TeamRepo),
|
||||||
new(Notice), new(EmailAddress))
|
new(Notice), new(EmailAddress))
|
||||||
|
|
||||||
gonicNames := []string{"SSL"}
|
gonicNames := []string{"SSL", "UID"}
|
||||||
for _, name := range gonicNames {
|
for _, name := range gonicNames {
|
||||||
core.LintGonicMapper[name] = true
|
core.LintGonicMapper[name] = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadConfigs loads the database settings
|
||||||
func LoadConfigs() {
|
func LoadConfigs() {
|
||||||
sec := setting.Cfg.Section("database")
|
sec := setting.Cfg.Section("database")
|
||||||
DbCfg.Type = sec.Key("DB_TYPE").String()
|
DbCfg.Type = sec.Key("DB_TYPE").String()
|
||||||
|
@ -115,7 +125,7 @@ func parsePostgreSQLHostPort(info string) (string, string) {
|
||||||
|
|
||||||
func getEngine() (*xorm.Engine, error) {
|
func getEngine() (*xorm.Engine, error) {
|
||||||
connStr := ""
|
connStr := ""
|
||||||
var Param string = "?"
|
var Param = "?"
|
||||||
if strings.Contains(DbCfg.Name, Param) {
|
if strings.Contains(DbCfg.Name, Param) {
|
||||||
Param = "&"
|
Param = "&"
|
||||||
}
|
}
|
||||||
|
@ -159,6 +169,7 @@ func getEngine() (*xorm.Engine, error) {
|
||||||
return xorm.NewEngine(DbCfg.Type, connStr)
|
return xorm.NewEngine(DbCfg.Type, connStr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewTestEngine sets a new test xorm.Engine
|
||||||
func NewTestEngine(x *xorm.Engine) (err error) {
|
func NewTestEngine(x *xorm.Engine) (err error) {
|
||||||
x, err = getEngine()
|
x, err = getEngine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -169,6 +180,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {
|
||||||
return x.StoreEngine("InnoDB").Sync2(tables...)
|
return x.StoreEngine("InnoDB").Sync2(tables...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SetEngine sets the xorm.Engine
|
||||||
func SetEngine() (err error) {
|
func SetEngine() (err error) {
|
||||||
x, err = getEngine()
|
x, err = getEngine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -191,6 +203,7 @@ func SetEngine() (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewEngine initializes a new xorm.Engine
|
||||||
func NewEngine() (err error) {
|
func NewEngine() (err error) {
|
||||||
if err = SetEngine(); err != nil {
|
if err = SetEngine(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -211,6 +224,7 @@ func NewEngine() (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Statistic contains the database statistics
|
||||||
type Statistic struct {
|
type Statistic struct {
|
||||||
Counter struct {
|
Counter struct {
|
||||||
User, Org, PublicKey,
|
User, Org, PublicKey,
|
||||||
|
@ -222,6 +236,7 @@ type Statistic struct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetStatistic returns the database statistics
|
||||||
func GetStatistic() (stats Statistic) {
|
func GetStatistic() (stats Statistic) {
|
||||||
stats.Counter.User = CountUsers()
|
stats.Counter.User = CountUsers()
|
||||||
stats.Counter.Org = CountOrganizations()
|
stats.Counter.Org = CountOrganizations()
|
||||||
|
@ -248,6 +263,7 @@ func GetStatistic() (stats Statistic) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Ping tests if database is alive
|
||||||
func Ping() error {
|
func Ping() error {
|
||||||
return x.Ping()
|
return x.Ping()
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,7 +15,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrOrgNotExist = errors.New("Organization does not exist")
|
// ErrOrgNotExist organization does not exist
|
||||||
|
ErrOrgNotExist = errors.New("Organization does not exist")
|
||||||
|
// ErrTeamNotExist team does not exist
|
||||||
ErrTeamNotExist = errors.New("Team does not exist")
|
ErrTeamNotExist = errors.New("Team does not exist")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -68,7 +70,7 @@ func (org *User) GetMembers() error {
|
||||||
|
|
||||||
var ids = make([]int64, len(ous))
|
var ids = make([]int64, len(ous))
|
||||||
for i, ou := range ous {
|
for i, ou := range ous {
|
||||||
ids[i] = ou.Uid
|
ids[i] = ou.UID
|
||||||
}
|
}
|
||||||
org.Members, err = GetUsersByIDs(ids)
|
org.Members, err = GetUsersByIDs(ids)
|
||||||
return err
|
return err
|
||||||
|
@ -127,7 +129,7 @@ func CreateOrganization(org, owner *User) (err error) {
|
||||||
|
|
||||||
// Add initial creator to organization and owner team.
|
// Add initial creator to organization and owner team.
|
||||||
if _, err = sess.Insert(&OrgUser{
|
if _, err = sess.Insert(&OrgUser{
|
||||||
Uid: owner.ID,
|
UID: owner.ID,
|
||||||
OrgID: org.ID,
|
OrgID: org.ID,
|
||||||
IsOwner: true,
|
IsOwner: true,
|
||||||
NumTeams: 1,
|
NumTeams: 1,
|
||||||
|
@ -235,7 +237,7 @@ func DeleteOrganization(org *User) (err error) {
|
||||||
// OrgUser represents an organization-user relation.
|
// OrgUser represents an organization-user relation.
|
||||||
type OrgUser struct {
|
type OrgUser struct {
|
||||||
ID int64 `xorm:"pk autoincr"`
|
ID int64 `xorm:"pk autoincr"`
|
||||||
Uid int64 `xorm:"INDEX UNIQUE(s)"`
|
UID int64 `xorm:"INDEX UNIQUE(s)"`
|
||||||
OrgID int64 `xorm:"INDEX UNIQUE(s)"`
|
OrgID int64 `xorm:"INDEX UNIQUE(s)"`
|
||||||
IsPublic bool
|
IsPublic bool
|
||||||
IsOwner bool
|
IsOwner bool
|
||||||
|
@ -243,29 +245,29 @@ type OrgUser struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsOrganizationOwner returns true if given user is in the owner team.
|
// IsOrganizationOwner returns true if given user is in the owner team.
|
||||||
func IsOrganizationOwner(orgId, uid int64) bool {
|
func IsOrganizationOwner(orgID, uid int64) bool {
|
||||||
has, _ := x.
|
has, _ := x.
|
||||||
Where("is_owner=?", true).
|
Where("is_owner=?", true).
|
||||||
And("uid=?", uid).
|
And("uid=?", uid).
|
||||||
And("org_id=?", orgId).
|
And("org_id=?", orgID).
|
||||||
Get(new(OrgUser))
|
Get(new(OrgUser))
|
||||||
return has
|
return has
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsOrganizationMember returns true if given user is member of organization.
|
// IsOrganizationMember returns true if given user is member of organization.
|
||||||
func IsOrganizationMember(orgId, uid int64) bool {
|
func IsOrganizationMember(orgID, uid int64) bool {
|
||||||
has, _ := x.
|
has, _ := x.
|
||||||
Where("uid=?", uid).
|
Where("uid=?", uid).
|
||||||
And("org_id=?", orgId).
|
And("org_id=?", orgID).
|
||||||
Get(new(OrgUser))
|
Get(new(OrgUser))
|
||||||
return has
|
return has
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsPublicMembership returns true if given user public his/her membership.
|
// IsPublicMembership returns true if given user public his/her membership.
|
||||||
func IsPublicMembership(orgId, uid int64) bool {
|
func IsPublicMembership(orgID, uid int64) bool {
|
||||||
has, _ := x.
|
has, _ := x.
|
||||||
Where("uid=?", uid).
|
Where("uid=?", uid).
|
||||||
And("org_id=?", orgId).
|
And("org_id=?", orgID).
|
||||||
And("is_public=?", true).
|
And("is_public=?", true).
|
||||||
Get(new(OrgUser))
|
Get(new(OrgUser))
|
||||||
return has
|
return has
|
||||||
|
@ -311,7 +313,7 @@ func GetOwnedOrgsByUserID(userID int64) ([]*User, error) {
|
||||||
return getOwnedOrgsByUserID(sess, userID)
|
return getOwnedOrgsByUserID(sess, userID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOwnedOrganizationsByUserIDDesc returns a list of organizations are owned by
|
// GetOwnedOrgsByUserIDDesc returns a list of organizations are owned by
|
||||||
// given user ID, ordered descending by the given condition.
|
// given user ID, ordered descending by the given condition.
|
||||||
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
|
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
|
||||||
sess := x.NewSession()
|
sess := x.NewSession()
|
||||||
|
@ -374,7 +376,7 @@ func AddOrgUser(orgID, uid int64) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
ou := &OrgUser{
|
ou := &OrgUser{
|
||||||
Uid: uid,
|
UID: uid,
|
||||||
OrgID: orgID,
|
OrgID: orgID,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -512,7 +514,7 @@ func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
|
||||||
return teamIDs, nil
|
return teamIDs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetTeams returns all teams that belong to organization,
|
// GetUserTeams returns all teams that belong to user,
|
||||||
// and that the user has joined.
|
// and that the user has joined.
|
||||||
func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
|
func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
|
||||||
return org.getUserTeams(x, userID)
|
return org.getUserTeams(x, userID)
|
||||||
|
@ -560,7 +562,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
|
||||||
return repos, repoCount, nil
|
return repos, repoCount, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetUserRepositories returns mirror repositories of the organization
|
// GetUserMirrorRepositories returns mirror repositories of the user
|
||||||
// that the user with the given userID has access to.
|
// that the user with the given userID has access to.
|
||||||
func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
|
func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
|
||||||
teamIDs, err := org.GetUserTeamIDs(userID)
|
teamIDs, err := org.GetUserTeamIDs(userID)
|
||||||
|
|
Loading…
Reference in a new issue