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

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

View File

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

View File

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

View File

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

View File

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