refined processes of self-registration feature.

This commit is contained in:
kunw 2016-03-31 20:15:36 +08:00
parent 0a0ee13d3f
commit a9352f88df
5 changed files with 36 additions and 21 deletions

View File

@ -40,6 +40,8 @@ func (c *CommonController) Render() error {
type BaseController struct {
beego.Controller
i18n.Locale
SelfRegistration bool
IsAdminLoginedUser bool
}
type langType struct {
@ -52,8 +54,6 @@ const (
)
var supportLanguages map[string]langType
var enableAddUserByAdmin bool
var isAdminLoginedUser bool
// Prepare extracts the language information from request and populate data for rendering templates.
func (b *BaseController) Prepare() {
@ -111,21 +111,21 @@ func (b *BaseController) Prepare() {
selfRegistration := strings.ToLower(os.Getenv("SELF_REGISTRATION"))
if selfRegistration == "off" {
enableAddUserByAdmin = true
if selfRegistration == "on" {
b.SelfRegistration = true
}
if sessionUserID != nil {
var err error
isAdminLoginedUser, err = dao.IsAdminRole(sessionUserID)
b.IsAdminLoginedUser, err = dao.IsAdminRole(sessionUserID)
if err != nil {
log.Errorf("Error occurred in IsAdminRole:%v", err)
b.CustomAbort(http.StatusInternalServerError, "Internal error.")
}
}
b.Data["IsAdminLoginedUser"] = isAdminLoginedUser
b.Data["EnableAddUserByAdmin"] = enableAddUserByAdmin
b.Data["IsAdminLoginedUser"] = b.IsAdminLoginedUser
b.Data["SelfRegistration"] = b.SelfRegistration
}

View File

@ -34,25 +34,39 @@ type RegisterController struct {
// Get renders the Sign In page, it only works if the auth mode is set to db_auth
func (rc *RegisterController) Get() {
if enableAddUserByAdmin && !isAdminLoginedUser {
log.Error("Self registration can only be used by admin user.\n")
if !rc.BaseController.SelfRegistration {
log.Error("Registration can only be used by admin user when self-registrion is off.\n")
rc.Redirect("/signIn", http.StatusFound)
}
pageTitleKey := "page_title_registration"
if isAdminLoginedUser {
pageTitleKey = "page_title_add_user"
}
authMode := os.Getenv("AUTH_MODE")
if authMode == "" || authMode == "db_auth" {
rc.ForwardTo(pageTitleKey, "register")
rc.ForwardTo("page_title_registration", "register")
} else {
rc.Redirect("/signIn", http.StatusFound)
}
}
// AddUserController handles request for adding user with an admin role user
type AddUserController struct {
BaseController
}
// Get renders the Sign In page, it only works if the auth mode is set to db_auth
func (ac *AddUserController) Get() {
if !ac.BaseController.IsAdminLoginedUser {
ac.Redirect("/signIn", http.StatusFound)
}
authMode := os.Getenv("AUTH_MODE")
if authMode == "" || authMode == "db_auth" {
ac.ForwardTo("page_title_add_user", "register")
} else {
ac.Redirect("/signIn", http.StatusFound)
}
}
// SignUp insert data into DB based on data in form.
func (rc *CommonController) SignUp() {
@ -61,8 +75,8 @@ func (rc *CommonController) SignUp() {
rc.CustomAbort(http.StatusForbidden, "")
}
if enableAddUserByAdmin && !isAdminLoginedUser {
log.Error("Self registration can only be used by admin user.\n")
if !(rc.BaseController.SelfRegistration || rc.BaseController.IsAdminLoginedUser) {
log.Error("Registration can only be used by admin role user when self-registration is off.\n")
rc.CustomAbort(http.StatusForbidden, "")
}

View File

@ -41,6 +41,7 @@ func init() {
beego.Router("/", &controllers.IndexController{})
beego.Router("/signIn", &controllers.SignInController{})
beego.Router("/register", &controllers.RegisterController{})
beego.Router("/addUser", &controllers.AddUserController{})
beego.Router("/forgotPassword", &controllers.ForgotPasswordController{})
beego.Router("/resetPassword", &controllers.ResetPasswordController{})
beego.Router("/changePassword", &controllers.ChangePasswordController{})

View File

@ -55,7 +55,7 @@ jQuery(function(){
if(isAdminLoginedUser == "true") {
document.location = "/registry/project";
}else{
document.location = "/signIn";
document.location = "/signIn";
}
}
});

View File

@ -58,7 +58,7 @@
{{ end }}
{{ if eq .AuthMode "db_auth" }}
{{ if eq .IsAdminLoginedUser true }}
<li><a id="aSelfSignUp" href="/register" target="_blank"><span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;{{i18n .Lang "add_user"}}</a></li>
<li><a id="aAddUser" href="/addUser" target="_blank"><span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;{{i18n .Lang "add_user"}}</a></li>
{{ end }}
{{ end}}
<li><a id="aLogout" href="#"><span class="glyphicon glyphicon-log-in"></span>&nbsp;&nbsp;{{i18n .Lang "log_out"}}</a></li>
@ -69,7 +69,7 @@
{{ else if eq .AuthMode "db_auth" }}
<div class="input-group">
&nbsp;<button type="button" class="btn btn-default" id="btnSignIn">{{i18n .Lang "sign_in"}}</button>
{{ if eq .EnableAddUserByAdmin false }}
{{ if eq .SelfRegistration true }}
&nbsp;<button type="button" class="btn btn-success" id="btnSignUp">{{i18n .Lang "sign_up"}}</button>
{{ end }}
</div>