Merge pull request #264 from Bwko/lint/org.go

Lint models/org.go
This commit is contained in:
Thomas Boerger 2016-11-27 12:18:20 +01:00 committed by GitHub
commit 5efdccd1d8
2 changed files with 37 additions and 19 deletions

View File

@ -13,9 +13,12 @@ import (
"path"
"strings"
// Needed for the MySQL driver
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
// Needed for the Postgresql driver
_ "github.com/lib/pq"
"code.gitea.io/gitea/models/migrations"
@ -45,16 +48,22 @@ func sessionRelease(sess *xorm.Session) {
}
var (
x *xorm.Engine
tables []interface{}
x *xorm.Engine
tables []interface{}
// HasEngine specifies if we have a xorm.Engine
HasEngine bool
// DbCfg holds the database settings
DbCfg struct {
Type, Host, Name, User, Passwd, Path, SSLMode string
}
// EnableSQLite3 use SQLite3
EnableSQLite3 bool
EnableTiDB bool
// EnableTiDB enable TiDB
EnableTiDB bool
)
func init() {
@ -69,12 +78,13 @@ func init() {
new(Team), new(OrgUser), new(TeamUser), new(TeamRepo),
new(Notice), new(EmailAddress))
gonicNames := []string{"SSL"}
gonicNames := []string{"SSL", "UID"}
for _, name := range gonicNames {
core.LintGonicMapper[name] = true
}
}
// LoadConfigs loads the database settings
func LoadConfigs() {
sec := setting.Cfg.Section("database")
DbCfg.Type = sec.Key("DB_TYPE").String()
@ -115,7 +125,7 @@ func parsePostgreSQLHostPort(info string) (string, string) {
func getEngine() (*xorm.Engine, error) {
connStr := ""
var Param string = "?"
var Param = "?"
if strings.Contains(DbCfg.Name, Param) {
Param = "&"
}
@ -159,6 +169,7 @@ func getEngine() (*xorm.Engine, error) {
return xorm.NewEngine(DbCfg.Type, connStr)
}
// NewTestEngine sets a new test xorm.Engine
func NewTestEngine(x *xorm.Engine) (err error) {
x, err = getEngine()
if err != nil {
@ -169,6 +180,7 @@ func NewTestEngine(x *xorm.Engine) (err error) {
return x.StoreEngine("InnoDB").Sync2(tables...)
}
// SetEngine sets the xorm.Engine
func SetEngine() (err error) {
x, err = getEngine()
if err != nil {
@ -191,6 +203,7 @@ func SetEngine() (err error) {
return nil
}
// NewEngine initializes a new xorm.Engine
func NewEngine() (err error) {
if err = SetEngine(); err != nil {
return err
@ -211,6 +224,7 @@ func NewEngine() (err error) {
return nil
}
// Statistic contains the database statistics
type Statistic struct {
Counter struct {
User, Org, PublicKey,
@ -222,6 +236,7 @@ type Statistic struct {
}
}
// GetStatistic returns the database statistics
func GetStatistic() (stats Statistic) {
stats.Counter.User = CountUsers()
stats.Counter.Org = CountOrganizations()
@ -248,6 +263,7 @@ func GetStatistic() (stats Statistic) {
return
}
// Ping tests if database is alive
func Ping() error {
return x.Ping()
}

View File

@ -15,7 +15,9 @@ import (
)
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")
)
@ -68,7 +70,7 @@ func (org *User) GetMembers() error {
var ids = make([]int64, len(ous))
for i, ou := range ous {
ids[i] = ou.Uid
ids[i] = ou.UID
}
org.Members, err = GetUsersByIDs(ids)
return err
@ -127,7 +129,7 @@ func CreateOrganization(org, owner *User) (err error) {
// Add initial creator to organization and owner team.
if _, err = sess.Insert(&OrgUser{
Uid: owner.ID,
UID: owner.ID,
OrgID: org.ID,
IsOwner: true,
NumTeams: 1,
@ -235,7 +237,7 @@ func DeleteOrganization(org *User) (err error) {
// OrgUser represents an organization-user relation.
type OrgUser struct {
ID int64 `xorm:"pk autoincr"`
Uid int64 `xorm:"INDEX UNIQUE(s)"`
UID int64 `xorm:"INDEX UNIQUE(s)"`
OrgID int64 `xorm:"INDEX UNIQUE(s)"`
IsPublic bool
IsOwner bool
@ -243,29 +245,29 @@ type OrgUser struct {
}
// 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.
Where("is_owner=?", true).
And("uid=?", uid).
And("org_id=?", orgId).
And("org_id=?", orgID).
Get(new(OrgUser))
return has
}
// IsOrganizationMember returns true if given user is member of organization.
func IsOrganizationMember(orgId, uid int64) bool {
func IsOrganizationMember(orgID, uid int64) bool {
has, _ := x.
Where("uid=?", uid).
And("org_id=?", orgId).
And("org_id=?", orgID).
Get(new(OrgUser))
return has
}
// IsPublicMembership returns true if given user public his/her membership.
func IsPublicMembership(orgId, uid int64) bool {
func IsPublicMembership(orgID, uid int64) bool {
has, _ := x.
Where("uid=?", uid).
And("org_id=?", orgId).
And("org_id=?", orgID).
And("is_public=?", true).
Get(new(OrgUser))
return has
@ -311,7 +313,7 @@ func GetOwnedOrgsByUserID(userID int64) ([]*User, error) {
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.
func GetOwnedOrgsByUserIDDesc(userID int64, desc string) ([]*User, error) {
sess := x.NewSession()
@ -374,7 +376,7 @@ func AddOrgUser(orgID, uid int64) error {
}
ou := &OrgUser{
Uid: uid,
UID: uid,
OrgID: orgID,
}
@ -512,7 +514,7 @@ func (org *User) GetUserTeamIDs(userID int64) ([]int64, error) {
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.
func (org *User) GetUserTeams(userID int64) ([]*Team, error) {
return org.getUserTeams(x, userID)
@ -560,7 +562,7 @@ func (org *User) GetUserRepositories(userID int64, page, pageSize int) ([]*Repos
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.
func (org *User) GetUserMirrorRepositories(userID int64) ([]*Repository, error) {
teamIDs, err := org.GetUserTeamIDs(userID)