Skip to content

Commit 289f819

Browse files
authored
Merge pull request #237 from strk/login_source-lint
Lint models/login_source.go
2 parents 2ccdcda + 1c3044b commit 289f819

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

models/login_source.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"code.gitea.io/gitea/modules/log"
2525
)
2626

27+
// LoginType represents an login type.
2728
type LoginType int
2829

2930
// Note: new type must append to the end of list to maintain compatibility.
@@ -36,13 +37,15 @@ const (
3637
LoginDLDAP // 5
3738
)
3839

40+
// LoginNames contains the name of LoginType values.
3941
var LoginNames = map[LoginType]string{
4042
LoginLDAP: "LDAP (via BindDN)",
4143
LoginDLDAP: "LDAP (simple auth)", // Via direct bind
4244
LoginSMTP: "SMTP",
4345
LoginPAM: "PAM",
4446
}
4547

48+
// SecurityProtocolNames contains the name of SecurityProtocol values.
4649
var SecurityProtocolNames = map[ldap.SecurityProtocol]string{
4750
ldap.SecurityProtocolUnencrypted: "Unencrypted",
4851
ldap.SecurityProtocolLDAPS: "LDAPS",
@@ -56,22 +59,28 @@ var (
5659
_ core.Conversion = &PAMConfig{}
5760
)
5861

62+
// LDAPConfig holds configuration for LDAP login source.
5963
type LDAPConfig struct {
6064
*ldap.Source
6165
}
6266

67+
// FromDB fills up a LDAPConfig from serialized format.
6368
func (cfg *LDAPConfig) FromDB(bs []byte) error {
6469
return json.Unmarshal(bs, &cfg)
6570
}
6671

72+
// ToDB exports a LDAPConfig to a serialized format.
6773
func (cfg *LDAPConfig) ToDB() ([]byte, error) {
6874
return json.Marshal(cfg)
6975
}
7076

77+
// SecurityProtocolName returns the name of configured security
78+
// protocol.
7179
func (cfg *LDAPConfig) SecurityProtocolName() string {
7280
return SecurityProtocolNames[cfg.SecurityProtocol]
7381
}
7482

83+
// SMTPConfig holds configuration for the SMTP login source.
7584
type SMTPConfig struct {
7685
Auth string
7786
Host string
@@ -81,22 +90,27 @@ type SMTPConfig struct {
8190
SkipVerify bool
8291
}
8392

93+
// FromDB fills up an SMTPConfig from serialized format.
8494
func (cfg *SMTPConfig) FromDB(bs []byte) error {
8595
return json.Unmarshal(bs, cfg)
8696
}
8797

98+
// ToDB exports an SMTPConfig to a serialized format.
8899
func (cfg *SMTPConfig) ToDB() ([]byte, error) {
89100
return json.Marshal(cfg)
90101
}
91102

103+
// PAMConfig holds configuration for the PAM login source.
92104
type PAMConfig struct {
93105
ServiceName string // pam service (e.g. system-auth)
94106
}
95107

108+
// FromDB fills up a PAMConfig from serialized format.
96109
func (cfg *PAMConfig) FromDB(bs []byte) error {
97110
return json.Unmarshal(bs, &cfg)
98111
}
99112

113+
// ToDB exports a PAMConfig to a serialized format.
100114
func (cfg *PAMConfig) ToDB() ([]byte, error) {
101115
return json.Marshal(cfg)
102116
}
@@ -115,13 +129,15 @@ type LoginSource struct {
115129
UpdatedUnix int64
116130
}
117131

118-
func (s *LoginSource) BeforeInsert() {
119-
s.CreatedUnix = time.Now().Unix()
120-
s.UpdatedUnix = s.CreatedUnix
132+
// BeforeInsert is invoked from XORM before inserting an object of this type.
133+
func (source *LoginSource) BeforeInsert() {
134+
source.CreatedUnix = time.Now().Unix()
135+
source.UpdatedUnix = source.CreatedUnix
121136
}
122137

123-
func (s *LoginSource) BeforeUpdate() {
124-
s.UpdatedUnix = time.Now().Unix()
138+
// BeforeUpdate is invoked from XORM before updating this object.
139+
func (source *LoginSource) BeforeUpdate() {
140+
source.UpdatedUnix = time.Now().Unix()
125141
}
126142

127143
// Cell2Int64 converts a xorm.Cell type to int64,
@@ -135,6 +151,7 @@ func Cell2Int64(val xorm.Cell) int64 {
135151
return (*val).(int64)
136152
}
137153

154+
// BeforeSet is invoked from XORM before setting the value of a field of this object.
138155
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
139156
switch colName {
140157
case "type":
@@ -151,41 +168,49 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
151168
}
152169
}
153170

154-
func (s *LoginSource) AfterSet(colName string, _ xorm.Cell) {
171+
// AfterSet is invoked from XORM after setting the value of a field of this object.
172+
func (source *LoginSource) AfterSet(colName string, _ xorm.Cell) {
155173
switch colName {
156174
case "created_unix":
157-
s.Created = time.Unix(s.CreatedUnix, 0).Local()
175+
source.Created = time.Unix(source.CreatedUnix, 0).Local()
158176
case "updated_unix":
159-
s.Updated = time.Unix(s.UpdatedUnix, 0).Local()
177+
source.Updated = time.Unix(source.UpdatedUnix, 0).Local()
160178
}
161179
}
162180

181+
// TypeName return name of this login source type.
163182
func (source *LoginSource) TypeName() string {
164183
return LoginNames[source.Type]
165184
}
166185

186+
// IsLDAP returns true of this source is of the LDAP type.
167187
func (source *LoginSource) IsLDAP() bool {
168188
return source.Type == LoginLDAP
169189
}
170190

191+
// IsDLDAP returns true of this source is of the DLDAP type.
171192
func (source *LoginSource) IsDLDAP() bool {
172193
return source.Type == LoginDLDAP
173194
}
174195

196+
// IsSMTP returns true of this source is of the SMTP type.
175197
func (source *LoginSource) IsSMTP() bool {
176198
return source.Type == LoginSMTP
177199
}
178200

201+
// IsPAM returns true of this source is of the PAM type.
179202
func (source *LoginSource) IsPAM() bool {
180203
return source.Type == LoginPAM
181204
}
182205

206+
// HasTLS returns true of this source supports TLS.
183207
func (source *LoginSource) HasTLS() bool {
184208
return ((source.IsLDAP() || source.IsDLDAP()) &&
185209
source.LDAP().SecurityProtocol > ldap.SecurityProtocolUnencrypted) ||
186210
source.IsSMTP()
187211
}
188212

213+
// UseTLS returns true of this source is configured to use TLS.
189214
func (source *LoginSource) UseTLS() bool {
190215
switch source.Type {
191216
case LoginLDAP, LoginDLDAP:
@@ -197,6 +222,8 @@ func (source *LoginSource) UseTLS() bool {
197222
return false
198223
}
199224

225+
// SkipVerify returns true if this source is configured to skip SSL
226+
// verification.
200227
func (source *LoginSource) SkipVerify() bool {
201228
switch source.Type {
202229
case LoginLDAP, LoginDLDAP:
@@ -208,17 +235,23 @@ func (source *LoginSource) SkipVerify() bool {
208235
return false
209236
}
210237

238+
// LDAP returns LDAPConfig for this source, if of LDAP type.
211239
func (source *LoginSource) LDAP() *LDAPConfig {
212240
return source.Cfg.(*LDAPConfig)
213241
}
214242

243+
// SMTP returns SMTPConfig for this source, if of SMTP type.
215244
func (source *LoginSource) SMTP() *SMTPConfig {
216245
return source.Cfg.(*SMTPConfig)
217246
}
218247

248+
// PAM returns PAMConfig for this source, if of PAM type.
219249
func (source *LoginSource) PAM() *PAMConfig {
220250
return source.Cfg.(*PAMConfig)
221251
}
252+
253+
// CreateLoginSource inserts a LoginSource in the DB if not already
254+
// existing with the given name.
222255
func CreateLoginSource(source *LoginSource) error {
223256
has, err := x.Get(&LoginSource{Name: source.Name})
224257
if err != nil {
@@ -231,6 +264,7 @@ func CreateLoginSource(source *LoginSource) error {
231264
return err
232265
}
233266

267+
// LoginSources returns a slice of all login sources found in DB.
234268
func LoginSources() ([]*LoginSource, error) {
235269
auths := make([]*LoginSource, 0, 5)
236270
return auths, x.Find(&auths)
@@ -248,11 +282,13 @@ func GetLoginSourceByID(id int64) (*LoginSource, error) {
248282
return source, nil
249283
}
250284

285+
// UpdateSource updates a LoginSource record in DB.
251286
func UpdateSource(source *LoginSource) error {
252287
_, err := x.Id(source.ID).AllCols().Update(source)
253288
return err
254289
}
255290

291+
// DeleteSource deletes a LoginSource record in DB.
256292
func DeleteSource(source *LoginSource) error {
257293
count, err := x.Count(&User{LoginSource: source.ID})
258294
if err != nil {
@@ -357,13 +393,16 @@ func (auth *smtpLoginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
357393
return nil, nil
358394
}
359395

396+
// SMTP authentication type names.
360397
const (
361398
SMTPPlain = "PLAIN"
362399
SMTPLogin = "LOGIN"
363400
)
364401

402+
// SMTPAuths contains available SMTP authentication type names.
365403
var SMTPAuths = []string{SMTPPlain, SMTPLogin}
366404

405+
// SMTPAuth performs an SMTP authentication.
367406
func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
368407
c, err := smtp.Dial(fmt.Sprintf("%s:%d", cfg.Host, cfg.Port))
369408
if err != nil {
@@ -487,6 +526,7 @@ func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMCon
487526
return user, CreateUser(user)
488527
}
489528

529+
// ExternalUserLogin attempts a login using external source types.
490530
func ExternalUserLogin(user *User, login, password string, source *LoginSource, autoRegister bool) (*User, error) {
491531
if !source.IsActived {
492532
return nil, ErrLoginSourceNotActived

0 commit comments

Comments
 (0)