Skip to content

Lint models/org.go #264

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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() {
Expand All @@ -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()
Expand Down Expand Up @@ -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 = "&"
}
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -211,6 +224,7 @@ func NewEngine() (err error) {
return nil
}

// Statistic contains the database statistics
type Statistic struct {
Counter struct {
User, Org, PublicKey,
Expand All @@ -222,6 +236,7 @@ type Statistic struct {
}
}

// GetStatistic returns the database statistics
func GetStatistic() (stats Statistic) {
stats.Counter.User = CountUsers()
stats.Counter.Org = CountOrganizations()
Expand All @@ -248,6 +263,7 @@ func GetStatistic() (stats Statistic) {
return
}

// Ping tests if database is alive
func Ping() error {
return x.Ping()
}
Expand Down
30 changes: 16 additions & 14 deletions models/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -235,37 +237,37 @@ 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)"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default GonicMapper cannot recognize UID as uid in database. I think you have to add UID to the map in https://github.com/go-gitea/gitea/blob/master/models/models.go#L72

OrgID int64 `xorm:"INDEX UNIQUE(s)"`
IsPublic bool
IsOwner bool
NumTeams int
}

// 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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -374,7 +376,7 @@ func AddOrgUser(orgID, uid int64) error {
}

ou := &OrgUser{
Uid: uid,
UID: uid,
OrgID: orgID,
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down