Skip to content

Commit 68a6579

Browse files
luhaixununknwon
authored andcommitted
login_source: add default authentication switch (gogs#5338)
* Add default Authentication Switch. * adjust the code accroding to reviews * gogs#1. Remove redudant logic. gogs#2, Fix a bug in "Edit" panel. * Remove unused logic * Fix local authentication files are not flushed. * refactor according to review.
1 parent 798798f commit 68a6579

File tree

8 files changed

+103
-20
lines changed

8 files changed

+103
-20
lines changed

models/login_source.go

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ type LoginSource struct {
133133
Type LoginType
134134
Name string `xorm:"UNIQUE"`
135135
IsActived bool `xorm:"NOT NULL DEFAULT false"`
136+
IsDefault bool `xorm:"DEFAULT false"`
136137
Cfg core.Conversion `xorm:"TEXT"`
137138

138139
Created time.Time `xorm:"-" json:"-"`
@@ -257,7 +258,12 @@ func CreateLoginSource(source *LoginSource) error {
257258
}
258259

259260
_, err = x.Insert(source)
260-
return err
261+
if err != nil {
262+
return err
263+
} else if source.IsDefault {
264+
return ResetNonDefaultLoginSources(source)
265+
}
266+
return nil
261267
}
262268

263269
// LoginSources returns all login sources defined.
@@ -291,23 +297,48 @@ func GetLoginSourceByID(id int64) (*LoginSource, error) {
291297
return source, nil
292298
}
293299

300+
// ResetNonDefaultLoginSources clean other default source flag
301+
func ResetNonDefaultLoginSources(source *LoginSource) error {
302+
// update changes to DB
303+
if _, err := x.NotIn("id", []int64{source.ID}).Cols("is_default").Update(&LoginSource{IsDefault: false}); err != nil {
304+
return err
305+
}
306+
// write changes to local authentications
307+
for i := range localLoginSources.sources {
308+
if localLoginSources.sources[i].LocalFile != nil && localLoginSources.sources[i].ID != source.ID {
309+
localLoginSources.sources[i].LocalFile.SetGeneral("is_default", "false")
310+
if err := localLoginSources.sources[i].LocalFile.SetConfig(source.Cfg); err != nil {
311+
return fmt.Errorf("LocalFile.SetConfig: %v", err)
312+
} else if err = localLoginSources.sources[i].LocalFile.Save(); err != nil {
313+
return fmt.Errorf("LocalFile.Save: %v", err)
314+
}
315+
}
316+
}
317+
// flush memory so that web page can show the same behaviors
318+
localLoginSources.UpdateLoginSource(source)
319+
return nil
320+
}
321+
294322
// UpdateLoginSource updates information of login source to database or local file.
295323
func UpdateLoginSource(source *LoginSource) error {
296324
if source.LocalFile == nil {
297-
_, err := x.Id(source.ID).AllCols().Update(source)
298-
return err
325+
if _, err := x.Id(source.ID).AllCols().Update(source); err != nil {
326+
return err
327+
} else {
328+
return ResetNonDefaultLoginSources(source)
329+
}
330+
299331
}
300332

301333
source.LocalFile.SetGeneral("name", source.Name)
302334
source.LocalFile.SetGeneral("is_activated", com.ToStr(source.IsActived))
335+
source.LocalFile.SetGeneral("is_default", com.ToStr(source.IsDefault))
303336
if err := source.LocalFile.SetConfig(source.Cfg); err != nil {
304337
return fmt.Errorf("LocalFile.SetConfig: %v", err)
305338
} else if err = source.LocalFile.Save(); err != nil {
306339
return fmt.Errorf("LocalFile.Save: %v", err)
307340
}
308-
309-
localLoginSources.UpdateLoginSource(source)
310-
return nil
341+
return ResetNonDefaultLoginSources(source)
311342
}
312343

313344
func DeleteSource(source *LoginSource) error {
@@ -361,7 +392,6 @@ func (s *LocalLoginSources) ActivatedList() []*LoginSource {
361392
if !s.sources[i].IsActived {
362393
continue
363394
}
364-
365395
source := &LoginSource{}
366396
*source = *s.sources[i]
367397
list = append(list, source)
@@ -394,7 +424,8 @@ func (s *LocalLoginSources) UpdateLoginSource(source *LoginSource) {
394424
for i := range s.sources {
395425
if s.sources[i].ID == source.ID {
396426
*s.sources[i] = *source
397-
break
427+
} else if source.IsDefault {
428+
s.sources[i].IsDefault = false
398429
}
399430
}
400431
}
@@ -429,6 +460,7 @@ func LoadAuthSources() {
429460
ID: s.Key("id").MustInt64(),
430461
Name: s.Key("name").String(),
431462
IsActived: s.Key("is_activated").MustBool(),
463+
IsDefault: s.Key("is_default").MustBool(),
432464
LocalFile: &AuthSourceFile{
433465
abspath: fpath,
434466
file: authSource,

pkg/form/auth.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type Authentication struct {
3232
GroupMemberUID string
3333
UserUID string
3434
IsActive bool
35+
IsDefault bool
3536
SMTPAuth string
3637
SMTPHost string
3738
SMTPPort int

routes/admin/auths.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func NewAuthSource(c *context.Context) {
6969
c.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED]
7070
c.Data["smtp_auth"] = "PLAIN"
7171
c.Data["is_active"] = true
72+
c.Data["is_default"] = true
7273
c.Data["AuthSources"] = authSources
7374
c.Data["SecurityProtocols"] = securityProtocols
7475
c.Data["SMTPAuths"] = models.SMTPAuths
@@ -152,6 +153,7 @@ func NewAuthSourcePost(c *context.Context, f form.Authentication) {
152153
Type: models.LoginType(f.Type),
153154
Name: f.Name,
154155
IsActived: f.IsActive,
156+
IsDefault: f.IsDefault,
155157
Cfg: config,
156158
}); err != nil {
157159
if models.IsErrLoginSourceAlreadyExist(err) {
@@ -225,11 +227,13 @@ func EditAuthSourcePost(c *context.Context, f form.Authentication) {
225227

226228
source.Name = f.Name
227229
source.IsActived = f.IsActive
230+
source.IsDefault = f.IsDefault
228231
source.Cfg = config
229232
if err := models.UpdateLoginSource(source); err != nil {
230233
c.ServerError("UpdateLoginSource", err)
231234
return
232235
}
236+
233237
log.Trace("Authentication changed by admin '%s': %d", c.User.Name, source.ID)
234238

235239
c.Flash.Success(c.Tr("admin.auths.update_success"))

routes/user/auth.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,15 @@ func Login(c *context.Context) {
113113
return
114114
}
115115
c.Data["LoginSources"] = loginSources
116-
116+
for i := range loginSources {
117+
if loginSources[i].IsDefault {
118+
c.Data["DefaultSource"] = *loginSources[i]
119+
c.Data["login_source"] = loginSources[i].ID
120+
newLoginSources := append(loginSources[:i], loginSources[i+1:]...)
121+
c.Data["LoginSources"] = newLoginSources
122+
break
123+
}
124+
}
117125
c.Success(LOGIN)
118126
}
119127

@@ -173,6 +181,15 @@ func LoginPost(c *context.Context, f form.SignIn) {
173181
default:
174182
c.ServerError("UserLogin", err)
175183
}
184+
for i := range loginSources {
185+
if loginSources[i].IsDefault {
186+
c.Data["DefaultSource"] = *loginSources[i]
187+
c.Data["login_source"] = loginSources[i].ID
188+
newLoginSources := append(loginSources[:i], loginSources[i+1:]...)
189+
c.Data["LoginSources"] = newLoginSources
190+
break
191+
}
192+
}
176193
return
177194
}
178195

templates/admin/auth/edit.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,12 @@
186186
<input name="is_active" type="checkbox" {{if .Source.IsActived}}checked{{end}}>
187187
</div>
188188
</div>
189-
189+
<div class="inline field">
190+
<div class="ui checkbox">
191+
<label><strong>{{.i18n.Tr "admin.auths.default_auth"}}</strong></label>
192+
<input name="is_default" type="checkbox" {{if .Source.IsDefault}}checked{{end}}>
193+
</div>
194+
</div>
190195
<div class="field">
191196
<button class="ui green button">{{.i18n.Tr "admin.auths.update"}}</button>
192197
{{if not .Source.LocalFile}}

templates/admin/auth/list.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<th>{{.i18n.Tr "admin.auths.name"}}</th>
1717
<th>{{.i18n.Tr "admin.auths.type"}}</th>
1818
<th>{{.i18n.Tr "admin.auths.enabled"}}</th>
19+
<th>{{.i18n.Tr "admin.auths.default"}}</th>
1920
<th>{{.i18n.Tr "admin.auths.updated"}}</th>
2021
<th>{{.i18n.Tr "admin.users.created"}}</th>
2122
<th>{{.i18n.Tr "admin.users.edit"}}</th>
@@ -28,6 +29,7 @@
2829
<td><a href="{{AppSubURL}}/admin/auths/{{.ID}}">{{.Name}}</a></td>
2930
<td>{{.TypeName}}</td>
3031
<td><i class="fa fa{{if .IsActived}}-check{{end}}-square-o"></i></td>
32+
<td><i class="fa fa{{if .IsDefault}}-check{{end}}-square-o"></i></td>
3133
<td><span class="poping up" data-content="{{DateFmtLong .Updated}}" data-variation="tiny">{{DateFmtShort .Updated}}</span></td>
3234
<td>
3335
{{if .Created.IsZero}}

templates/admin/auth/new.tmpl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,13 +176,21 @@
176176
<input name="skip_verify" type="checkbox" {{if .skip_verify}}checked{{end}}>
177177
</div>
178178
</div>
179+
179180
<div class="inline field">
180181
<div class="ui checkbox">
181182
<label><strong>{{.i18n.Tr "admin.auths.activated"}}</strong></label>
182183
<input name="is_active" type="checkbox" {{if .is_active}}checked{{end}}>
183184
</div>
184185
</div>
185186

187+
<div class="inline field">
188+
<div class="ui checkbox">
189+
<label><strong>{{.i18n.Tr "admin.auths.default_auth"}}</strong></label>
190+
<input name="is_default" type="checkbox" {{if .is_default}}checked{{end}}>
191+
</div>
192+
</div>
193+
186194
<div class="field">
187195
<button class="ui green button">{{.i18n.Tr "admin.auths.new"}}</button>
188196
</div>

templates/user/auth/login.tmpl

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,30 @@
2222
<label for="password">{{.i18n.Tr "auth.auth_source"}}</label>
2323
<div class="ui selection dropdown">
2424
<input type="hidden" id="login_source" name="login_source" value="{{.login_source}}" required>
25-
<span class="text">
26-
{{.i18n.Tr "auth.local"}}
27-
</span>
28-
<i class="dropdown icon"></i>
29-
<div class="menu">
30-
<div class="item" data-value="0">{{.i18n.Tr "auth.local"}}</div>
31-
{{range .LoginSources}}
32-
<div class="item" data-value="{{.ID}}">{{.Name}}</div>
33-
{{end}}
34-
</div>
25+
{{if .DefaultSource}}
26+
<span class="text">
27+
{{.DefaultSource.Name}}
28+
</span>
29+
<i class="dropdown icon"></i>
30+
<div class="menu">
31+
<div class="item" data-value="{{.DefaultSource.ID}}">{{.DefaultSource.Name}}</div>
32+
<div class="item" data-value="0">{{.i18n.Tr "auth.local"}}</div>
33+
{{range .LoginSources}}
34+
<div class="item" data-value="{{.ID}}">{{.Name}}</div>
35+
{{end}}
36+
</div>
37+
{{else}}
38+
<span class="text">
39+
{{.i18n.Tr "auth.local"}}
40+
</span>
41+
<i class="dropdown icon"></i>
42+
<div class="menu">
43+
<div class="item" data-value="0">{{.i18n.Tr "auth.local"}}</div>
44+
{{range .LoginSources}}
45+
<div class="item" data-value="{{.ID}}">{{.Name}}</div>
46+
{{end}}
47+
</div>
48+
{{end}}
3549
</div>
3650
</div>
3751
{{end}}

0 commit comments

Comments
 (0)