mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-21 23:21:26 +01:00
added self-registration feature.
This commit is contained in:
parent
99e277471a
commit
a8a009c7ac
@ -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.
|
||||
|
@ -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"))
|
||||
|
@ -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
|
||||
|
@ -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": "项目"
|
||||
|
@ -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 = 修改密码
|
||||
|
@ -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";
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -16,7 +16,11 @@
|
||||
<div class="col-sm-4"></div>
|
||||
<div class="col-sm-4">
|
||||
<div class="page-header">
|
||||
{{ if eq .EnableAddUserByAdmin true }}
|
||||
<h1>{{i18n .Lang "add_user" }}</h1>
|
||||
{{ else }}
|
||||
<h1>{{i18n .Lang "registration"}}</h1>
|
||||
{{ end }}
|
||||
</div>
|
||||
<form class="form">
|
||||
<div class="alert alert-danger" role="alert" id="divErrMsg"></div>
|
||||
@ -62,7 +66,13 @@
|
||||
</div>
|
||||
<div class="form-group has-feedback">
|
||||
<div class="text-center">
|
||||
<button type="button" class="btn btn-default" id="btnPageSignUp">{{i18n .Lang "sign_up"}}</button>
|
||||
<button type="button" class="btn btn-default" id="btnPageSignUp">
|
||||
{{ if eq .EnableAddUserByAdmin true }}
|
||||
{{i18n .Lang "add_user" }}
|
||||
{{ else }}
|
||||
{{i18n .Lang "sign_up"}}
|
||||
{{ end }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
@ -13,6 +13,7 @@
|
||||
limitations under the License.
|
||||
-->
|
||||
<input type="hidden" id="currentLanguage" value="{{.Lang}}">
|
||||
<input type="hidden" id="enableAddUserByAdmin" value="{{.EnableAddUserByAdmin}}">
|
||||
<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">
|
||||
@ -55,6 +56,9 @@
|
||||
<li><a id="aChangePassword" href="/changePassword" target="_blank"><span class="glyphicon glyphicon-pencil"></span> {{i18n .Lang "change_password"}}</a></li>
|
||||
<li role="separator" class="divider"></li>
|
||||
{{ end }}
|
||||
{{ if eq .EnableAddUserByAdmin true }}
|
||||
<li><a id="aSelfSignUp" href="/register" target="_blank"><span class="glyphicon glyphicon-plus"></span> {{i18n .Lang "add_user"}}</a></li>
|
||||
{{ end}}
|
||||
<li><a id="aLogout" href="#"><span class="glyphicon glyphicon-log-in"></span> {{i18n .Lang "log_out"}}</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
@ -63,7 +67,9 @@
|
||||
{{ else if eq .AuthMode "db_auth" }}
|
||||
<div class="input-group">
|
||||
<button type="button" class="btn btn-default" id="btnSignIn">{{i18n .Lang "sign_in"}}</button>
|
||||
{{ if eq .EnableAddUserByAdmin false }}
|
||||
<button type="button" class="btn btn-success" id="btnSignUp">{{i18n .Lang "sign_up"}}</button>
|
||||
{{ end }}
|
||||
</div>
|
||||
{{ else }}
|
||||
<div class="input-group">
|
||||
|
Loading…
Reference in New Issue
Block a user