refine some structures and words.

This commit is contained in:
kunw 2016-04-01 15:39:04 +08:00
parent 649607bd18
commit 26f7ad4fb1
5 changed files with 43 additions and 46 deletions

View File

@ -40,8 +40,9 @@ func (c *CommonController) Render() error {
type BaseController struct {
beego.Controller
i18n.Locale
SelfRegistration bool
IsAdminLoginedUser bool
SelfRegistration bool
IsAdmin bool
AuthMode string
}
type langType struct {
@ -97,17 +98,12 @@ func (b *BaseController) Prepare() {
b.Data["CurLang"] = curLang.Name
b.Data["RestLangs"] = restLangs
sessionUserID := b.GetSession("userId")
if sessionUserID != nil {
b.Data["Username"] = b.GetSession("username")
b.Data["UserId"] = sessionUserID.(int)
}
authMode := os.Getenv("AUTH_MODE")
authMode := strings.ToLower(os.Getenv("AUTH_MODE"))
if authMode == "" {
authMode = "db_auth"
}
b.Data["AuthMode"] = authMode
b.AuthMode = authMode
b.Data["AuthMode"] = b.AuthMode
selfRegistration := strings.ToLower(os.Getenv("SELF_REGISTRATION"))
@ -115,16 +111,20 @@ func (b *BaseController) Prepare() {
b.SelfRegistration = true
}
sessionUserID := b.GetSession("userId")
if sessionUserID != nil {
b.Data["Username"] = b.GetSession("username")
b.Data["UserId"] = sessionUserID.(int)
var err error
b.IsAdminLoginedUser, err = dao.IsAdminRole(sessionUserID)
b.IsAdmin, err = dao.IsAdminRole(sessionUserID.(int))
if err != nil {
log.Errorf("Error occurred in IsAdminRole:%v", err)
b.CustomAbort(http.StatusInternalServerError, "Internal error.")
}
}
b.Data["IsAdminLoginedUser"] = b.IsAdminLoginedUser
b.Data["IsAdmin"] = b.IsAdmin
b.Data["SelfRegistration"] = b.SelfRegistration
}

View File

@ -17,7 +17,6 @@ package controllers
import (
"net/http"
"os"
"strings"
"github.com/vmware/harbor/dao"
@ -34,13 +33,12 @@ 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 !rc.BaseController.SelfRegistration {
log.Error("Registration can only be used by admin user when self-registrion is off.\n")
if !rc.SelfRegistration {
log.Error("Registration is disabled when self-registration is off.\n")
rc.Redirect("/signIn", http.StatusFound)
}
authMode := os.Getenv("AUTH_MODE")
if authMode == "" || authMode == "db_auth" {
if rc.AuthMode == "db_auth" {
rc.ForwardTo("page_title_registration", "register")
} else {
rc.Redirect("/signIn", http.StatusFound)
@ -55,12 +53,12 @@ type AddUserController struct {
// 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 {
if !ac.IsAdmin {
log.Error("Add user can only be used by admin role user.\n")
ac.Redirect("/signIn", http.StatusFound)
}
authMode := os.Getenv("AUTH_MODE")
if authMode == "" || authMode == "db_auth" {
if ac.AuthMode == "db_auth" {
ac.ForwardTo("page_title_add_user", "register")
} else {
ac.Redirect("/signIn", http.StatusFound)
@ -68,37 +66,36 @@ func (ac *AddUserController) Get() {
}
// SignUp insert data into DB based on data in form.
func (rc *CommonController) SignUp() {
func (cc *CommonController) SignUp() {
authMode := os.Getenv("AUTH_MODE")
if !(authMode == "" || authMode == "db_auth") {
rc.CustomAbort(http.StatusForbidden, "")
if !(cc.AuthMode == "db_auth") {
cc.CustomAbort(http.StatusForbidden, "")
}
if !(rc.BaseController.SelfRegistration || rc.BaseController.IsAdminLoginedUser) {
if !(cc.SelfRegistration || cc.IsAdmin) {
log.Error("Registration can only be used by admin role user when self-registration is off.\n")
rc.CustomAbort(http.StatusForbidden, "")
cc.CustomAbort(http.StatusForbidden, "")
}
username := strings.TrimSpace(rc.GetString("username"))
email := strings.TrimSpace(rc.GetString("email"))
realname := strings.TrimSpace(rc.GetString("realname"))
password := strings.TrimSpace(rc.GetString("password"))
comment := strings.TrimSpace(rc.GetString("comment"))
username := strings.TrimSpace(cc.GetString("username"))
email := strings.TrimSpace(cc.GetString("email"))
realname := strings.TrimSpace(cc.GetString("realname"))
password := strings.TrimSpace(cc.GetString("password"))
comment := strings.TrimSpace(cc.GetString("comment"))
user := models.User{Username: username, Email: email, Realname: realname, Password: password, Comment: comment}
_, err := dao.Register(user)
if err != nil {
log.Errorf("Error occurred in Register: %v", err)
rc.CustomAbort(http.StatusInternalServerError, "Internal error.")
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
}
}
// UserExists checks if user exists when user input value in sign in form.
func (rc *CommonController) UserExists() {
target := rc.GetString("target")
value := rc.GetString("value")
func (cc *CommonController) UserExists() {
target := cc.GetString("target")
value := cc.GetString("value")
user := models.User{}
switch target {
@ -111,8 +108,8 @@ func (rc *CommonController) UserExists() {
exist, err := dao.UserExists(user, target)
if err != nil {
log.Errorf("Error occurred in UserExists: %v", err)
rc.CustomAbort(http.StatusInternalServerError, "Internal error.")
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
}
rc.Data["json"] = exist
rc.ServeJSON()
cc.Data["json"] = exist
cc.ServeJSON()
}

View File

@ -36,7 +36,7 @@ jQuery(function(){
var confirmedPassword = $.trim($("#ConfirmedPassword").val());
var realname = $.trim($("#Realname").val());
var comment = $.trim($("#Comment").val());
var isAdminLoginedUser = $("#isAdminLoginedUser").val();
var isAdmin = $("#isAdmin").val();
$.ajax({
url : '/signUp',
@ -49,10 +49,10 @@ jQuery(function(){
if(xhr && xhr.status == 200){
$("#dlgModal")
.dialogModal({
"title": isAdminLoginedUser == "true" ? i18n.getMessage("title_add_user") : i18n.getMessage("title_sign_up"),
"content": isAdminLoginedUser == "true" ? i18n.getMessage("added_user_successfully") : i18n.getMessage("registered_successfully"),
"title": isAdmin == "true" ? i18n.getMessage("title_add_user") : i18n.getMessage("title_sign_up"),
"content": isAdmin == "true" ? i18n.getMessage("added_user_successfully") : i18n.getMessage("registered_successfully"),
"callback": function(){
if(isAdminLoginedUser == "true") {
if(isAdmin == "true") {
document.location = "/registry/project";
}else{
document.location = "/signIn";

View File

@ -16,7 +16,7 @@
<div class="col-sm-4"></div>
<div class="col-sm-4">
<div class="page-header">
{{ if eq .IsAdminLoginedUser true }}
{{ if eq .IsAdmin true }}
<h1>{{i18n .Lang "add_user" }}</h1>
{{ else }}
<h1>{{i18n .Lang "registration"}}</h1>
@ -67,7 +67,7 @@
<div class="form-group has-feedback">
<div class="text-center">
<button type="button" class="btn btn-default" id="btnPageSignUp">
{{ if eq .IsAdminLoginedUser true }}
{{ if eq .IsAdmin true }}
{{i18n .Lang "add_user" }}
{{ else }}
{{i18n .Lang "sign_up"}}

View File

@ -13,7 +13,7 @@
limitations under the License.
-->
<input type="hidden" id="currentLanguage" value="{{.Lang}}">
<input type="hidden" id="isAdminLoginedUser" value="{{.IsAdminLoginedUser}}">
<input type="hidden" id="isAdmin" value="{{.IsAdmin}}">
<nav class="navbar navbar-default" role="navigation" style="margin-bottom: 0;">
<div class="navbar-header">
<button aria-controls="navbar" aria-expanded="false" data-target="#navbar" data-toggle="collapse" class="navbar-toggle collapsed" type="button">
@ -57,7 +57,7 @@
<li role="separator" class="divider"></li>
{{ end }}
{{ if eq .AuthMode "db_auth" }}
{{ if eq .IsAdminLoginedUser true }}
{{ if eq .IsAdmin true }}
<li><a id="aAddUser" href="/addUser" target="_blank"><span class="glyphicon glyphicon-plus"></span>&nbsp;&nbsp;{{i18n .Lang "add_user"}}</a></li>
{{ end }}
{{ end}}