mirror of
https://github.com/goharbor/harbor.git
synced 2024-12-26 02:28:06 +01:00
Merge pull request #11 from ywk253100/golint_refactor
golint refactor for controller and service
This commit is contained in:
commit
6b31b740a2
@ -124,7 +124,7 @@ func (ra *RepositoryAPI) GetTags() {
|
||||
var tags []string
|
||||
|
||||
repoName := ra.GetString("repo_name")
|
||||
result, err := svc_utils.RegistryApiGet(svc_utils.BuildRegistryUrl(repoName, "tags", "list"), ra.username)
|
||||
result, err := svc_utils.RegistryAPIGet(svc_utils.BuildRegistryURL(repoName, "tags", "list"), ra.username)
|
||||
if err != nil {
|
||||
beego.Error("Failed to get repo tags, repo name:", repoName, ", error: ", err)
|
||||
ra.RenderError(http.StatusInternalServerError, "Failed to get repo tags")
|
||||
@ -143,7 +143,7 @@ func (ra *RepositoryAPI) GetManifests() {
|
||||
|
||||
item := models.RepoItem{}
|
||||
|
||||
result, err := svc_utils.RegistryApiGet(svc_utils.BuildRegistryUrl(repoName, "manifests", tag), ra.username)
|
||||
result, err := svc_utils.RegistryAPIGet(svc_utils.BuildRegistryURL(repoName, "manifests", tag), ra.username)
|
||||
if err != nil {
|
||||
beego.Error("Failed to get manifests for repo, repo name:", repoName, ", tag:", tag, ", error:", err)
|
||||
ra.RenderError(http.StatusInternalServerError, "Internal Server Error")
|
||||
|
@ -42,14 +42,14 @@ type langType struct {
|
||||
}
|
||||
|
||||
const (
|
||||
DEFAULT_LANG = "en-US"
|
||||
defaultLang = "en-US"
|
||||
)
|
||||
|
||||
var supportLanguages map[string]langType
|
||||
|
||||
func (b *BaseController) Prepare() {
|
||||
|
||||
var lang string = ""
|
||||
var lang string
|
||||
al := b.Ctx.Request.Header.Get("Accept-Language")
|
||||
|
||||
if len(al) > 4 {
|
||||
@ -60,7 +60,7 @@ func (b *BaseController) Prepare() {
|
||||
}
|
||||
|
||||
if _, exist := supportLanguages[lang]; exist == false { //Check if support the request language.
|
||||
lang = DEFAULT_LANG //Set default language if not supported.
|
||||
lang = defaultLang //Set default language if not supported.
|
||||
}
|
||||
|
||||
sessionLang := b.GetSession("lang")
|
||||
@ -88,8 +88,8 @@ func (b *BaseController) Prepare() {
|
||||
b.Data["CurLang"] = curLang.Name
|
||||
b.Data["RestLangs"] = restLangs
|
||||
|
||||
sessionUserId := b.GetSession("userId")
|
||||
if sessionUserId != nil {
|
||||
sessionUserID := b.GetSession("userId")
|
||||
if sessionUserID != nil {
|
||||
b.Data["Username"] = b.GetSession("username")
|
||||
}
|
||||
authMode := os.Getenv("AUTH_MODE")
|
||||
|
@ -31,15 +31,15 @@ type ItemDetailController struct {
|
||||
|
||||
func (idc *ItemDetailController) Get() {
|
||||
|
||||
projectId, _ := idc.GetInt64("project_id")
|
||||
projectID, _ := idc.GetInt64("project_id")
|
||||
|
||||
if projectId <= 0 {
|
||||
beego.Error("Invalid project id:", projectId)
|
||||
if projectID <= 0 {
|
||||
beego.Error("Invalid project id:", projectID)
|
||||
idc.Redirect("/signIn", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
project, err := dao.GetProjectById(projectId)
|
||||
project, err := dao.GetProjectById(projectID)
|
||||
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetProjectById:", err)
|
||||
@ -51,19 +51,19 @@ func (idc *ItemDetailController) Get() {
|
||||
return
|
||||
}
|
||||
|
||||
sessionUserId := idc.GetSession("userId")
|
||||
sessionUserID := idc.GetSession("userId")
|
||||
|
||||
if project.Public != 1 && sessionUserId == nil {
|
||||
if project.Public != 1 && sessionUserID == nil {
|
||||
idc.Redirect("/signIn?uri="+url.QueryEscape(idc.Ctx.Input.URI()), http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
if sessionUserId != nil {
|
||||
if sessionUserID != nil {
|
||||
|
||||
idc.Data["Username"] = idc.GetSession("username")
|
||||
idc.Data["UserId"] = sessionUserId.(int)
|
||||
idc.Data["UserId"] = sessionUserID.(int)
|
||||
|
||||
roleList, err := dao.GetUserProjectRoles(models.User{UserId: sessionUserId.(int)}, projectId)
|
||||
roleList, err := dao.GetUserProjectRoles(models.User{UserId: sessionUserID.(int)}, projectID)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUserProjectRoles:", err)
|
||||
idc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
|
@ -33,8 +33,8 @@ type ChangePasswordController struct {
|
||||
}
|
||||
|
||||
func (cpc *ChangePasswordController) Get() {
|
||||
sessionUserId := cpc.GetSession("userId")
|
||||
if sessionUserId == nil {
|
||||
sessionUserID := cpc.GetSession("userId")
|
||||
if sessionUserID == nil {
|
||||
cpc.Redirect("/signIn", http.StatusFound)
|
||||
return
|
||||
}
|
||||
@ -42,43 +42,43 @@ func (cpc *ChangePasswordController) Get() {
|
||||
cpc.ForwardTo("page_title_change_password", "change-password")
|
||||
}
|
||||
|
||||
func (cpc *CommonController) UpdatePassword() {
|
||||
func (cc *CommonController) UpdatePassword() {
|
||||
|
||||
sessionUserId := cpc.GetSession("userId")
|
||||
sessionUserID := cc.GetSession("userId")
|
||||
|
||||
if sessionUserId == nil {
|
||||
if sessionUserID == nil {
|
||||
beego.Warning("User does not login.")
|
||||
cpc.CustomAbort(http.StatusUnauthorized, "please_login_first")
|
||||
cc.CustomAbort(http.StatusUnauthorized, "please_login_first")
|
||||
}
|
||||
|
||||
oldPassword := cpc.GetString("old_password")
|
||||
oldPassword := cc.GetString("old_password")
|
||||
if oldPassword == "" {
|
||||
beego.Error("Old password is blank")
|
||||
cpc.CustomAbort(http.StatusBadRequest, "Old password is blank")
|
||||
cc.CustomAbort(http.StatusBadRequest, "Old password is blank")
|
||||
}
|
||||
|
||||
queryUser := models.User{UserId: sessionUserId.(int), Password: oldPassword}
|
||||
queryUser := models.User{UserId: sessionUserID.(int), Password: oldPassword}
|
||||
user, err := dao.CheckUserPassword(queryUser)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in CheckUserPassword:", err)
|
||||
cpc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
|
||||
if user == nil {
|
||||
beego.Warning("Password input is not correct")
|
||||
cpc.CustomAbort(http.StatusForbidden, "old_password_is_not_correct")
|
||||
cc.CustomAbort(http.StatusForbidden, "old_password_is_not_correct")
|
||||
}
|
||||
|
||||
password := cpc.GetString("password")
|
||||
password := cc.GetString("password")
|
||||
if password != "" {
|
||||
updateUser := models.User{UserId: sessionUserId.(int), Password: password, Salt: user.Salt}
|
||||
updateUser := models.User{UserId: sessionUserID.(int), Password: password, Salt: user.Salt}
|
||||
err = dao.ChangeUserPassword(updateUser, oldPassword)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in ChangeUserPassword:", err)
|
||||
cpc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
} else {
|
||||
cpc.CustomAbort(http.StatusBadRequest, "please_input_new_password")
|
||||
cc.CustomAbort(http.StatusBadRequest, "please_input_new_password")
|
||||
}
|
||||
}
|
||||
|
||||
@ -88,79 +88,79 @@ type ForgotPasswordController struct {
|
||||
|
||||
type MessageDetail struct {
|
||||
Hint string
|
||||
Url string
|
||||
Uuid string
|
||||
URL string
|
||||
UUID string
|
||||
}
|
||||
|
||||
func (fpc *ForgotPasswordController) Get() {
|
||||
fpc.ForwardTo("page_title_forgot_password", "forgot-password")
|
||||
}
|
||||
|
||||
func (fpc *CommonController) SendEmail() {
|
||||
func (cc *CommonController) SendEmail() {
|
||||
|
||||
email := fpc.GetString("email")
|
||||
email := cc.GetString("email")
|
||||
|
||||
pass, _ := regexp.MatchString(`^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$`, email)
|
||||
|
||||
if !pass {
|
||||
fpc.CustomAbort(http.StatusBadRequest, "email_content_illegal")
|
||||
cc.CustomAbort(http.StatusBadRequest, "email_content_illegal")
|
||||
} else {
|
||||
|
||||
queryUser := models.User{Email: email}
|
||||
exist, err := dao.UserExists(queryUser, "email")
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in UserExists:", err)
|
||||
fpc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if !exist {
|
||||
fpc.CustomAbort(http.StatusNotFound, "email_does_not_exist")
|
||||
cc.CustomAbort(http.StatusNotFound, "email_does_not_exist")
|
||||
}
|
||||
|
||||
messageTemplate, err := template.ParseFiles("views/reset-password-mail.tpl")
|
||||
if err != nil {
|
||||
beego.Error("Parse email template file failed:", err)
|
||||
fpc.CustomAbort(http.StatusInternalServerError, err.Error())
|
||||
cc.CustomAbort(http.StatusInternalServerError, err.Error())
|
||||
}
|
||||
|
||||
message := new(bytes.Buffer)
|
||||
|
||||
harborUrl := os.Getenv("HARBOR_URL")
|
||||
if harborUrl == "" {
|
||||
harborUrl = "localhost"
|
||||
harborURL := os.Getenv("HARBOR_URL")
|
||||
if harborURL == "" {
|
||||
harborURL = "localhost"
|
||||
}
|
||||
uuid, err := dao.GenerateRandomString()
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GenerateRandomString:", err)
|
||||
fpc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
err = messageTemplate.Execute(message, MessageDetail{
|
||||
Hint: fpc.Tr("reset_email_hint"),
|
||||
Url: harborUrl,
|
||||
Uuid: uuid,
|
||||
Hint: cc.Tr("reset_email_hint"),
|
||||
URL: harborURL,
|
||||
UUID: uuid,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
beego.Error("message template error:", err)
|
||||
fpc.CustomAbort(http.StatusInternalServerError, "internal_error")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "internal_error")
|
||||
}
|
||||
|
||||
config, err := beego.AppConfig.GetSection("mail")
|
||||
if err != nil {
|
||||
beego.Error("Can not load app.conf:", err)
|
||||
fpc.CustomAbort(http.StatusInternalServerError, "internal_error")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "internal_error")
|
||||
}
|
||||
|
||||
mail := utils.Mail{
|
||||
From: config["from"],
|
||||
To: []string{email},
|
||||
Subject: fpc.Tr("reset_email_subject"),
|
||||
Subject: cc.Tr("reset_email_subject"),
|
||||
Message: message.String()}
|
||||
|
||||
err = mail.SendMail()
|
||||
|
||||
if err != nil {
|
||||
beego.Error("send email failed:", err)
|
||||
fpc.CustomAbort(http.StatusInternalServerError, "send_email_failed")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "send_email_failed")
|
||||
}
|
||||
|
||||
user := models.User{ResetUuid: uuid, Email: email}
|
||||
@ -176,14 +176,14 @@ type ResetPasswordController struct {
|
||||
|
||||
func (rpc *ResetPasswordController) Get() {
|
||||
|
||||
resetUuid := rpc.GetString("reset_uuid")
|
||||
if resetUuid == "" {
|
||||
resetUUID := rpc.GetString("reset_uuid")
|
||||
if resetUUID == "" {
|
||||
beego.Error("Reset uuid is blank.")
|
||||
rpc.Redirect("/", http.StatusFound)
|
||||
return
|
||||
}
|
||||
|
||||
queryUser := models.User{ResetUuid: resetUuid}
|
||||
queryUser := models.User{ResetUuid: resetUUID}
|
||||
user, err := dao.GetUser(queryUser)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUser:", err)
|
||||
@ -198,34 +198,34 @@ func (rpc *ResetPasswordController) Get() {
|
||||
}
|
||||
}
|
||||
|
||||
func (rpc *CommonController) ResetPassword() {
|
||||
func (cc *CommonController) ResetPassword() {
|
||||
|
||||
resetUuid := rpc.GetString("reset_uuid")
|
||||
if resetUuid == "" {
|
||||
rpc.CustomAbort(http.StatusBadRequest, "Reset uuid is blank.")
|
||||
resetUUID := cc.GetString("reset_uuid")
|
||||
if resetUUID == "" {
|
||||
cc.CustomAbort(http.StatusBadRequest, "Reset uuid is blank.")
|
||||
}
|
||||
|
||||
queryUser := models.User{ResetUuid: resetUuid}
|
||||
queryUser := models.User{ResetUuid: resetUUID}
|
||||
user, err := dao.GetUser(queryUser)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in GetUser:", err)
|
||||
rpc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
if user == nil {
|
||||
beego.Error("User does not exist")
|
||||
rpc.CustomAbort(http.StatusBadRequest, "User does not exist")
|
||||
cc.CustomAbort(http.StatusBadRequest, "User does not exist")
|
||||
}
|
||||
|
||||
password := rpc.GetString("password")
|
||||
password := cc.GetString("password")
|
||||
|
||||
if password != "" {
|
||||
user.Password = password
|
||||
err = dao.ResetUserPassword(*user)
|
||||
if err != nil {
|
||||
beego.Error("Error occurred in ResetUserPassword:", err)
|
||||
rpc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
cc.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
||||
}
|
||||
} else {
|
||||
rpc.CustomAbort(http.StatusBadRequest, "password_is_required")
|
||||
cc.CustomAbort(http.StatusBadRequest, "password_is_required")
|
||||
}
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ func authenticate(principal, password string) bool {
|
||||
}
|
||||
if user == nil {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ type NotificationHandler struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
const MEDIA_TYPE_MANIFEST = "application/vnd.docker.distribution.manifest.v1+json"
|
||||
const MediaTypeManifest = "application/vnd.docker.distribution.manifest.v1+json"
|
||||
|
||||
func (n *NotificationHandler) Post() {
|
||||
var notification models.Notification
|
||||
@ -43,7 +43,7 @@ func (n *NotificationHandler) Post() {
|
||||
}
|
||||
var username, action, repo, project string
|
||||
for _, e := range notification.Events {
|
||||
if e.Target.MediaType == MEDIA_TYPE_MANIFEST && strings.HasPrefix(e.Request.UserAgent, "docker") {
|
||||
if e.Target.MediaType == MediaTypeManifest && strings.HasPrefix(e.Request.UserAgent, "docker") {
|
||||
username = e.Actor.Name
|
||||
action = e.Action
|
||||
repo = e.Target.Repository
|
||||
|
@ -57,45 +57,45 @@ func FilterAccess(username string, authenticated bool, a *token.ResourceActions)
|
||||
|
||||
if a.Type == "registry" && a.Name == "catalog" {
|
||||
return
|
||||
} else {
|
||||
//clear action list to assign to new acess element after perm check.
|
||||
a.Actions = []string{}
|
||||
if a.Type == "repository" {
|
||||
if strings.Contains(a.Name, "/") { //Only check the permission when the requested image has a namespace, i.e. project
|
||||
projectName := a.Name[0:strings.LastIndex(a.Name, "/")]
|
||||
var permission string
|
||||
var err error
|
||||
if authenticated {
|
||||
if username == "admin" {
|
||||
exist, err := dao.ProjectExists(projectName)
|
||||
if err != nil {
|
||||
log.Printf("Error occurred in CheckExistProject: %v", err)
|
||||
return
|
||||
}
|
||||
if exist {
|
||||
permission = "RW"
|
||||
} else {
|
||||
permission = ""
|
||||
log.Printf("project %s does not exist, set empty permission for admin", projectName)
|
||||
}
|
||||
}
|
||||
|
||||
//clear action list to assign to new acess element after perm check.
|
||||
a.Actions = []string{}
|
||||
if a.Type == "repository" {
|
||||
if strings.Contains(a.Name, "/") { //Only check the permission when the requested image has a namespace, i.e. project
|
||||
projectName := a.Name[0:strings.LastIndex(a.Name, "/")]
|
||||
var permission string
|
||||
var err error
|
||||
if authenticated {
|
||||
if username == "admin" {
|
||||
exist, err := dao.ProjectExists(projectName)
|
||||
if err != nil {
|
||||
log.Printf("Error occurred in CheckExistProject: %v", err)
|
||||
return
|
||||
}
|
||||
if exist {
|
||||
permission = "RW"
|
||||
} else {
|
||||
permission, err = dao.GetPermission(username, projectName)
|
||||
if err != nil {
|
||||
log.Printf("Error occurred in GetPermission: %v", err)
|
||||
return
|
||||
}
|
||||
permission = ""
|
||||
log.Printf("project %s does not exist, set empty permission for admin", projectName)
|
||||
}
|
||||
} else {
|
||||
permission, err = dao.GetPermission(username, projectName)
|
||||
if err != nil {
|
||||
log.Printf("Error occurred in GetPermission: %v", err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if strings.Contains(permission, "W") {
|
||||
a.Actions = append(a.Actions, "push")
|
||||
}
|
||||
if strings.Contains(permission, "R") || dao.IsProjectPublic(projectName) {
|
||||
a.Actions = append(a.Actions, "pull")
|
||||
}
|
||||
}
|
||||
if strings.Contains(permission, "W") {
|
||||
a.Actions = append(a.Actions, "push")
|
||||
}
|
||||
if strings.Contains(permission, "R") || dao.IsProjectPublic(projectName) {
|
||||
a.Actions = append(a.Actions, "pull")
|
||||
}
|
||||
}
|
||||
log.Printf("current access, type: %s, name:%s, actions:%v \n", a.Type, a.Name, a.Actions)
|
||||
}
|
||||
log.Printf("current access, type: %s, name:%s, actions:%v \n", a.Type, a.Name, a.Actions)
|
||||
}
|
||||
|
||||
//For the UI process to call, so it won't establish a https connection from UI to proxy.
|
||||
|
@ -38,7 +38,7 @@ func init() {
|
||||
}
|
||||
|
||||
func RefreshCatalogCache() error {
|
||||
result, err := RegistryApiGet(BuildRegistryUrl("_catalog"), "")
|
||||
result, err := RegistryAPIGet(BuildRegistryURL("_catalog"), "")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func BuildRegistryUrl(segments ...string) string {
|
||||
func BuildRegistryURL(segments ...string) string {
|
||||
registryURL := os.Getenv("REGISTRY_URL")
|
||||
if registryURL == "" {
|
||||
registryURL = "http://localhost:5000"
|
||||
@ -40,7 +40,7 @@ func BuildRegistryUrl(segments ...string) string {
|
||||
return url
|
||||
}
|
||||
|
||||
func RegistryApiGet(url, username string) ([]byte, error) {
|
||||
func RegistryAPIGet(url, username string) ([]byte, error) {
|
||||
response, err := http.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
Loading…
Reference in New Issue
Block a user