From a8a009c7ac31657447d1a34b2dcf33be19ae3285 Mon Sep 17 00:00:00 2001 From: kunw Date: Wed, 30 Mar 2016 18:02:38 +0800 Subject: [PATCH 01/21] added self-registration feature. --- controllers/base.go | 19 +++++++++++++++++++ controllers/register.go | 27 +++++++++++++++++++++++++-- static/i18n/locale_en-US.ini | 2 ++ static/i18n/locale_messages.js | 12 ++++++++++++ static/i18n/locale_zh-CN.ini | 2 ++ static/resources/js/register.js | 26 ++++++++++++++++---------- views/register.tpl | 12 +++++++++++- views/segment/header-content.tpl | 6 ++++++ 8 files changed, 93 insertions(+), 13 deletions(-) diff --git a/controllers/base.go b/controllers/base.go index 836aa1382..5767e715c 100644 --- a/controllers/base.go +++ b/controllers/base.go @@ -22,6 +22,7 @@ import ( "github.com/astaxie/beego" "github.com/beego/i18n" + "github.com/vmware/harbor/models" ) // CommonController handles request from UI that doesn't expect a page, such as /login /logout ... @@ -50,6 +51,7 @@ const ( ) var supportLanguages map[string]langType +var enableAddUserByAdmin bool // Prepare extracts the language information from request and populate data for rendering templates. func (b *BaseController) Prepare() { @@ -96,12 +98,29 @@ func (b *BaseController) Prepare() { sessionUserID := b.GetSession("userId") if sessionUserID != nil { b.Data["Username"] = b.GetSession("username") + b.Data["UserId"] = sessionUserID.(int) } authMode := os.Getenv("AUTH_MODE") if authMode == "" { authMode = "db_auth" } b.Data["AuthMode"] = authMode + + var selfRegistration = strings.ToLower(os.Getenv("SELF_REGISTRATION")) + + switch selfRegistration { + case "on": + if sessionUserID != nil && sessionUserID.(int) != models.SYSADMIN { + enableAddUserByAdmin = false + } else { + enableAddUserByAdmin = true + } + case "": + enableAddUserByAdmin = false + } + + b.Data["EnableAddUserByAdmin"] = enableAddUserByAdmin + } // ForwardTo setup layout and template for content for a page. diff --git a/controllers/register.go b/controllers/register.go index 105819752..6c9c2d92d 100644 --- a/controllers/register.go +++ b/controllers/register.go @@ -23,6 +23,8 @@ import ( "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" + "github.com/vmware/harbor/utils/log" + "github.com/astaxie/beego" ) @@ -33,16 +35,37 @@ 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() { + + pageTitleKey := "page_title_registration" + + if enableAddUserByAdmin { + sessionUserID := rc.GetSession("userId") + if sessionUserID == nil || sessionUserID.(int) != models.SYSADMIN { + log.Error("Self registration can only be used by admin user.\n") + rc.Redirect("/signIn", http.StatusFound) + } + pageTitleKey = "page_title_add_user" + } + authMode := os.Getenv("AUTH_MODE") if authMode == "" || authMode == "db_auth" { - rc.ForwardTo("page_title_registration", "register") + rc.ForwardTo(pageTitleKey, "register") } else { - rc.Redirect("/signIn", http.StatusNotFound) + rc.Redirect("/signIn", http.StatusFound) } } // SignUp insert data into DB based on data in form. func (rc *CommonController) SignUp() { + + if enableAddUserByAdmin { + sessionUserID := rc.GetSession("userId") + if sessionUserID == nil || sessionUserID.(int) != models.SYSADMIN { + log.Error("Self registration can only be used by admin user.\n") + rc.Redirect("/signIn", http.StatusFound) + } + } + username := strings.TrimSpace(rc.GetString("username")) email := strings.TrimSpace(rc.GetString("email")) realname := strings.TrimSpace(rc.GetString("realname")) diff --git a/static/i18n/locale_en-US.ini b/static/i18n/locale_en-US.ini index 2473d2181..7a44eddda 100644 --- a/static/i18n/locale_en-US.ini +++ b/static/i18n/locale_en-US.ini @@ -3,6 +3,7 @@ page_title_sign_in = Sign In - Harbor page_title_project = Project - Harbor page_title_item_details = Details - Harbor page_title_registration = Sign Up - Harbor +page_title_add_user = Add User - Harbor page_title_forgot_password = Forgot Password - Harbor title_forgot_password = Forgot Password page_title_reset_password = Reset Password - Harbor @@ -12,6 +13,7 @@ title_change_password = Change Password page_title_search = Search - Harbor sign_in = Sign In sign_up = Sign Up +add_user = Add User log_out = Log Out search_placeholder = projects or repositories change_password = Change Password diff --git a/static/i18n/locale_messages.js b/static/i18n/locale_messages.js index 4b30bf847..93fdcaabb 100644 --- a/static/i18n/locale_messages.js +++ b/static/i18n/locale_messages.js @@ -177,6 +177,10 @@ var global_messages = { "en-US": "Sign Up", "zh-CN": "注册" }, + "title_add_user": { + "en-US": "Add User", + "zh-CN": "新增用户" + }, "registered_successfully": { "en-US": "Signed up successfully.", "zh-CN": "注册成功。" @@ -185,6 +189,14 @@ var global_messages = { "en-US": "Failed to sign up.", "zh-CN": "注册失败。" }, + "added_user_successfully": { + "en-US": "Added user successfully.", + "zh-CN": "新增用户成功。" + }, + "added_user_failed": { + "en-US": "Added user failed.", + "zh-CN": "新增用户失败。" + }, "projects" : { "en-US": "Projects", "zh-CN": "项目" diff --git a/static/i18n/locale_zh-CN.ini b/static/i18n/locale_zh-CN.ini index f28369d9c..9e49c12ae 100644 --- a/static/i18n/locale_zh-CN.ini +++ b/static/i18n/locale_zh-CN.ini @@ -3,6 +3,7 @@ page_title_sign_in = 登录 - Harbor page_title_project = 项目 - Harbor page_title_item_details = 详细信息 - Harbor page_title_registration = 注册 - Harbor +page_title_add_user = 新增用户 - Harbor page_title_forgot_password = 忘记密码 - Harbor title_forgot_password = 忘记密码 page_title_reset_password = 重置密码 - Harbor @@ -12,6 +13,7 @@ title_change_password = 修改密码 page_title_search = 搜索 - Harbor sign_in = 登录 sign_up = 注册 +add_user = 新增用户 log_out = 注销 search_placeholder = 项目或镜像名称 change_password = 修改密码 diff --git a/static/resources/js/register.js b/static/resources/js/register.js index cbd133671..61b5e09d5 100644 --- a/static/resources/js/register.js +++ b/static/resources/js/register.js @@ -30,12 +30,14 @@ jQuery(function(){ $("#btnPageSignUp").on("click", function(){ validateOptions.Validate(function() { - var username = $.trim($("#Username").val()); - var email = $.trim($("#Email").val()); - var password = $.trim($("#Password").val()); - var confirmedPassword = $.trim($("#ConfirmedPassword").val()); - var realname = $.trim($("#Realname").val()); - var comment = $.trim($("#Comment").val()); + var username = $.trim($("#Username").val()); + var email = $.trim($("#Email").val()); + var password = $.trim($("#Password").val()); + var confirmedPassword = $.trim($("#ConfirmedPassword").val()); + var realname = $.trim($("#Realname").val()); + var comment = $.trim($("#Comment").val()); + var enableAddUserByAdmin = $("#enableAddUserByAdmin").val(); + $.ajax({ url : '/signUp', data:{username: username, password: password, realname: realname, comment: comment, email: email}, @@ -47,10 +49,14 @@ jQuery(function(){ if(xhr && xhr.status == 200){ $("#dlgModal") .dialogModal({ - "title": i18n.getMessage("title_sign_up"), - "content": i18n.getMessage("registered_successfully"), - "callback": function(){ - document.location = "/signIn"; + "title": enableAddUserByAdmin == "true" ? i18n.getMessage("title_add_user") : i18n.getMessage("title_sign_up"), + "content": enableAddUserByAdmin == "true" ? i18n.getMessage("added_user_successfully") : i18n.getMessage("registered_successfully"), + "callback": function(){ + if(enableAddUserByAdmin == "true") { + document.location = "/registry/project"; + }else{ + document.location = "/signIn"; + } } }); } diff --git a/views/register.tpl b/views/register.tpl index 88bd9615f..7455c19fd 100644 --- a/views/register.tpl +++ b/views/register.tpl @@ -16,7 +16,11 @@
@@ -62,7 +66,13 @@
- +
diff --git a/views/segment/header-content.tpl b/views/segment/header-content.tpl index 1c6e147ea..14874fbe2 100644 --- a/views/segment/header-content.tpl +++ b/views/segment/header-content.tpl @@ -13,6 +13,7 @@ limitations under the License. --> +