2016-02-01 12:59:10 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2016-02-26 11:54:14 +01:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2016-02-24 07:31:52 +01:00
|
|
|
"net/http"
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
"github.com/vmware/harbor/auth"
|
2016-02-01 12:59:10 +01:00
|
|
|
"github.com/vmware/harbor/models"
|
2016-03-30 14:49:43 +02:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-02-01 12:59:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// IndexController handles request to /
|
2016-02-01 12:59:10 +01:00
|
|
|
type IndexController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Get renders the index page.
|
2016-02-01 12:59:10 +01:00
|
|
|
func (c *IndexController) Get() {
|
|
|
|
c.Data["Username"] = c.GetSession("username")
|
|
|
|
c.ForwardTo("page_title_index", "index")
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// SignInController handles request to /signIn
|
2016-02-01 12:59:10 +01:00
|
|
|
type SignInController struct {
|
|
|
|
BaseController
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Get renders Sign In page.
|
2016-02-01 12:59:10 +01:00
|
|
|
func (sic *SignInController) Get() {
|
|
|
|
sic.ForwardTo("page_title_sign_in", "sign-in")
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Login handles login request from UI.
|
2016-02-01 12:59:10 +01:00
|
|
|
func (c *CommonController) Login() {
|
|
|
|
principal := c.GetString("principal")
|
|
|
|
password := c.GetString("password")
|
|
|
|
|
2016-04-15 07:17:32 +02:00
|
|
|
user, err := auth.Login(models.AuthModel{
|
|
|
|
Principal: principal,
|
|
|
|
Password: password,
|
|
|
|
})
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-30 14:49:43 +02:00
|
|
|
log.Errorf("Error occurred in UserLogin: %v", err)
|
2016-03-08 05:33:06 +01:00
|
|
|
c.CustomAbort(http.StatusUnauthorized, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if user == nil {
|
2016-02-24 07:31:52 +01:00
|
|
|
c.CustomAbort(http.StatusUnauthorized, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 03:15:01 +01:00
|
|
|
c.SetSession("userId", user.UserID)
|
2016-02-01 12:59:10 +01:00
|
|
|
c.SetSession("username", user.Username)
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// SwitchLanguage handles UI request to switch between different languages and re-render template based on language.
|
2016-02-01 12:59:10 +01:00
|
|
|
func (c *CommonController) SwitchLanguage() {
|
|
|
|
lang := c.GetString("lang")
|
|
|
|
if lang == "en-US" || lang == "zh-CN" {
|
|
|
|
c.SetSession("lang", lang)
|
|
|
|
c.Data["Lang"] = lang
|
|
|
|
}
|
2016-02-24 07:31:52 +01:00
|
|
|
c.Redirect(c.Ctx.Request.Header.Get("Referer"), http.StatusFound)
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Logout handles UI request to logout.
|
2016-02-01 12:59:10 +01:00
|
|
|
func (c *CommonController) Logout() {
|
|
|
|
c.DestroySession()
|
|
|
|
}
|