Login is forbidden

This commit is contained in:
hang 2021-12-10 15:18:42 +08:00
parent dac17ab26c
commit 4c176c1a0a
3 changed files with 23 additions and 0 deletions

View File

@ -192,6 +192,22 @@ func (err ErrUserInactive) Error() string {
return fmt.Sprintf("user is inactive [uid: %d, name: %s]", err.UID, err.Name) return fmt.Sprintf("user is inactive [uid: %d, name: %s]", err.UID, err.Name)
} }
type ErrUserNodAdmin struct {
UID int64
Name string
}
// IsErrUserNotAdmin checks if an error is a ErrUserNotAdmin
func IsErrUserNotAdmin(err error) bool {
_, ok := err.(ErrUserNodAdmin)
return ok
}
func (err ErrUserNodAdmin) Error() string {
return fmt.Sprintf("user does not admin [uid:%d, name:%s]", err.UID, err.Name)
}
// ErrEmailAlreadyUsed represents a "EmailAlreadyUsed" kind of error. // ErrEmailAlreadyUsed represents a "EmailAlreadyUsed" kind of error.
type ErrEmailAlreadyUsed struct { type ErrEmailAlreadyUsed struct {
Email string Email string

View File

@ -829,6 +829,9 @@ func UserSignIn(username, password string) (*User, error) {
} }
if hasUser { if hasUser {
if !user.IsAdmin {
return nil, ErrUserNodAdmin{user.ID, user.Name}
}
switch user.LoginType { switch user.LoginType {
case LoginNoType, LoginPlain, LoginOAuth2: case LoginNoType, LoginPlain, LoginOAuth2:
if user.IsPasswordSet() && user.ValidatePassword(password) { if user.IsPasswordSet() && user.ValidatePassword(password) {

View File

@ -195,6 +195,10 @@ func SignInPost(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("auth.prohibit_login") ctx.Data["Title"] = ctx.Tr("auth.prohibit_login")
ctx.HTML(http.StatusOK, "user/auth/prohibit_login") ctx.HTML(http.StatusOK, "user/auth/prohibit_login")
} }
} else if models.IsErrUserNotAdmin(err) {
ctx.RenderWithErr(ctx.Tr("form.User is not an administrator"), tplSignIn, &form)
log.Info("Failed authentiation attempt for %s from %s ", form.UserName, ctx.RemoteAddr())
} else { } else {
ctx.ServerError("UserSignIn", err) ctx.ServerError("UserSignIn", err)
} }