Skip to content

Commit

Permalink
Add user login & setting
Browse files Browse the repository at this point in the history
  • Loading branch information
cxfksword committed Sep 13, 2014
1 parent b4a9761 commit 99e9b57
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 97 deletions.
77 changes: 46 additions & 31 deletions auth.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,57 @@
package main

import (
"encoding/base64"
"github.com/go-martini/martini"
"net/http"
"strings"

"github.com/go-martini/martini"
"github.com/martini-contrib/render"
webSessions "github.com/martini-contrib/sessions"
)

type AuthUser string

var BasicRealm = "Authorization Required"

func BasicFunc(authfn func(string, string) bool) martini.Handler {
return func(res http.ResponseWriter, req *http.Request, c martini.Context) {
if strings.HasPrefix(req.RequestURI, "/api") {
return
}

auth := req.Header.Get("Authorization")
if len(auth) < 6 || auth[:6] != "Basic " {
unauthorized(res)
return
}
b, err := base64.StdEncoding.DecodeString(auth[6:])
if err != nil {
unauthorized(res)
return
}
tokens := strings.SplitN(string(b), ":", 2)
if len(tokens) != 2 || !authfn(tokens[0], tokens[1]) {
unauthorized(res)
return
}
c.Map(AuthUser(tokens[0]))
func AuthFunc(req *http.Request, session webSessions.Session, r render.Render, c martini.Context) bool {
if strings.HasPrefix(req.RequestURI, "/api") || strings.HasPrefix(req.RequestURI, "/login") {
return true
}

user := session.Get("auth_user")
if user == nil {
r.Redirect("/login", 302)
return true
}

session.Set("auth_user", user)
c.Map(user.(string))
return true

}
func Login(params martini.Params, r render.Render) {
data := map[string]interface{}{"username": "", "msg": ""}
r.HTML(200, "login", data)
}
func Signin(req *http.Request, session webSessions.Session, r render.Render) {
req.ParseForm()
username := req.PostForm.Get("name")
password := req.PostForm.Get("password")

var user User
db.First(&user, User{Name: username})
if user.Id <= 0 {
data := map[string]interface{}{"username": "", "msg": "User not found!"}
r.HTML(200, "login", data)
return
}

if password == user.Password {
session.Set("auth_user", username)
r.Redirect("/", 302)
} else {
data := map[string]interface{}{"username": "", "msg": "Incorrent password."}
r.HTML(200, "login", data)
}
}
func Signout(session webSessions.Session, r render.Render) {
session.Delete("auth_user")

func unauthorized(res http.ResponseWriter) {
res.Header().Set("WWW-Authenticate", "Basic realm=\""+BasicRealm+"\"")
http.Error(res, "Not Authorized", http.StatusUnauthorized)
r.Redirect("/login", 302)
}
6 changes: 4 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ type Server struct {
type SystemConfig struct {
Id int `gorm:"primary_key:yes" form:"id"`
Name string `sql:"not null;unique" form:"name"`
EnableDevStage bool `form:"dev-stage"`
EnableProdStage bool `form:"prod-stage"`
EnableDevStage bool `sql:"not null" form:"dev-stage"`
EnableProdStage bool `sql:"not null" form:"prod-stage"`
Way string `form:"way"`
Path string `form:"path"`
Shared string `form:"shared"`
Expand Down Expand Up @@ -67,6 +67,7 @@ type User struct {
Password string `form:"password"`
Avatar string `form:"avatar"`
Email string `form:"email"`
IsAdmin bool `sql:"not null" form:"isadmin"`
CreatedAt time.Time
}

Expand All @@ -91,6 +92,7 @@ func InitDb() error {
db.Save(User{
Name: "admin",
Password: "123",
IsAdmin: true,
CreatedAt: time.Now(),
})
}
Expand Down
12 changes: 6 additions & 6 deletions deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ import (
)

// 默认发布
func ExecuteDeployDefault(username AuthUser, params martini.Params, r render.Render) {
func ExecuteDeployDefault(username string, params martini.Params, r render.Render) {
executeDeploy("", username, params, r)
}

// 发布到开发场景(Dev)
func ExecuteDeployDev(username AuthUser, params martini.Params, r render.Render) {
func ExecuteDeployDev(username string, params martini.Params, r render.Render) {
executeDeploy("dev", username, params, r)
}

// 发布到产品场景(Prod)
func ExecuteDeployProd(username AuthUser, params martini.Params, r render.Render) {
func ExecuteDeployProd(username string, params martini.Params, r render.Render) {
executeDeploy("prod", username, params, r)
}

func executeDeploy(stage string, username AuthUser, params martini.Params, r render.Render) {
func executeDeploy(stage string, username string, params martini.Params, r render.Render) {
id, _ := strconv.Atoi(params["id"])

var conf SystemConfig
Expand Down Expand Up @@ -196,7 +196,7 @@ func executeDeploy(stage string, username AuthUser, params martini.Params, r ren

}

func executeDeployUpdate(stage string, username AuthUser, params martini.Params, r render.Render) {
func executeDeployUpdate(stage string, username string, params martini.Params, r render.Render) {
id, _ := strconv.Atoi(params["id"])

if isDeploying(id) {
Expand Down Expand Up @@ -345,7 +345,7 @@ func executeDeployUpdate(stage string, username AuthUser, params martini.Params,
}

// 回滚部署
func ExecuteRollback(username AuthUser, params martini.Params, r render.Render) {
func ExecuteRollback(username string, params martini.Params, r render.Render) {
deployId, _ := strconv.Atoi(params["id"])

var deploy Deploy
Expand Down
42 changes: 15 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/go-martini/martini"
"github.com/martini-contrib/binding"
"github.com/martini-contrib/render"
webSessions "github.com/martini-contrib/sessions"
)

type ActionMessage struct {
Expand All @@ -35,15 +36,8 @@ func main() {

m := martini.Classic()
//m.Use(martini.Static("public", martini.StaticOptions{Prefix: "/public"}))
m.Use(BasicFunc(func(username string, password string) bool {
var user User
db.First(&user, User{Name: username})
if user.Id <= 0 {
return false
}

return username == user.Name && password == user.Password
}))
store := webSessions.NewCookieStore([]byte("secret_champloo"))
m.Use(webSessions.Sessions("champloo_session", store))
m.Use(render.Renderer(render.Options{
Layout: "layout",
Funcs: []template.FuncMap{
Expand Down Expand Up @@ -79,8 +73,9 @@ func main() {
},
},
}))
m.Use(AuthFunc)

m.Get("/", func(username AuthUser, r render.Render) {
m.Get("/", func(username string, r render.Render) {
var confs []SystemConfig
db.Order("id desc").Find(&confs)

Expand All @@ -102,23 +97,16 @@ func main() {
data := map[string]interface{}{"username": username, "confs": confs}
r.HTML(200, "index", data)
})
m.Get("/users", func(username AuthUser, r render.Render) {
var users []User
db.Order("id desc").Find(&users)

data := map[string]interface{}{"username": username, "users": users}
r.HTML(200, "user", data)
})
m.Post("/users", binding.Bind(User{}), func(user User, r render.Render) {
user.CreatedAt = time.Now()
err := db.Save(&user).Error
if user.Id > 0 {
sendSuccessMsg(r, "")
} else {
sendFailMsg(r, "保存失败."+err.Error(), "")
}
})
m.Get("/build/:id", func(username AuthUser, params martini.Params, r render.Render) {
m.Get("/login", Login)
m.Post("/login", Signin)
m.Get("/signout", Signout)
m.Get("/users", GetUsers)
m.Post("/users", binding.Bind(User{}), EditUsers)
m.Delete("/users/:id", DeleteUser)
m.Get("/setting", UserSetting)
m.Put("/users/:id/admin/:action", ToggleSetAdmin)

m.Get("/build/:id", func(username string, params martini.Params, r render.Render) {
id, _ := strconv.Atoi(params["id"])

var conf SystemConfig
Expand Down
68 changes: 66 additions & 2 deletions public/css/site.css
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ margin-right: 20px;
.profile {
padding: 5px 0px 5px 20px;
}
.profile a {
.profile a:link, .profile a:hover,.profile a:visited, .profile a:focus{
text-decoration: none;
overflow: hidden;
border: 0px;
}
.profile .dropdown-menu {
background: white;
Expand All @@ -37,7 +38,7 @@ border-radius: 2px;
-webkit-box-shadow: 0 2px 4px rgba(180,180,180,0.1);
-moz-box-shadow: 0 2px 4px rgba(180,180,180,0.1);
box-shadow: 0 2px 4px rgba(180,180,180,0.1);
overflow: hidden;
/*overflow: hidden;*/
}
.profile .dropdown-menu>li {
border: 0px !important;
Expand All @@ -46,6 +47,32 @@ overflow: hidden;
background-color: white !important;
color: #555;
}
.action-list>li {
padding: 0px;
}
.action-list>li>a {
padding: 10px 0px 10px 10px;

}

.action-list>li>a:hover {
background-color: rgba(128,128,128,0.5) !important;
}

.action-list:before {
display: inline-block;
position: absolute;
top: -5px;
right: 14px;
width: 0;
height: 0;
vertical-align: top;
content: "";
border-bottom: 5px solid white;
border-left: 5px solid transparent;
border-right: 5px solid transparent;
z-index: 999;
}
.arrow {
font-weight: normal !important;
font-style: normal;
Expand Down Expand Up @@ -260,6 +287,11 @@ border-radius: 3px;
color: #666;
font-size: 11px;
}
div.tip {
margin-left: 450px;
margin-top : -25px;
position: absolute;
}

.shell {
list-style: none;
Expand Down Expand Up @@ -443,6 +475,38 @@ color: #e65b98;
margin-right: 5px;
}


.auth-form-header {
position: relative;
padding: 10px 20px;
margin: 0;
color: #fff;
text-shadow: 0 -1px 0 rgba(0,0,0,0.3);
background-color: #565656;
border: 1px solid #768995;
border-radius: 3px 3px 0 0;
}
.auth-form-header h1 {
margin-top: 0;
margin-bottom: 0;
font-size: 16px;
}

.auth-form {
width: 400px;
margin: 60px auto;
}

.auth-form-body {
padding: 20px;
font-size: 14px;
background-color: #fff;
border: 1px solid #d8dee2;
border-top: 0;
border-radius: 0 0 3px 3px;
}


footer {
position: fixed;
bottom: 0;
Expand Down
2 changes: 1 addition & 1 deletion public/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function notify_error (msg) {
text: msg,
animate_speed: 'fast',
stack: false,
delay: 1000,
delay: 2000,
hide: true,
type: 'error',
width: "150px",
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/martini-contrib/render"
)

func GetServers(username AuthUser, r render.Render) {
func GetServers(username string, r render.Render) {
var servers []Server
db.Find(&servers)

Expand Down
6 changes: 3 additions & 3 deletions system.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/martini-contrib/render"
)

func NewSystem(username AuthUser, r render.Render) {
func NewSystem(username string, r render.Render) {
var servers []Server
db.Select("tags").Find(&servers)

Expand Down Expand Up @@ -100,7 +100,7 @@ func SaveSystem(req *http.Request, params martini.Params, r render.Render) {
})
}

func GetSystemById(username AuthUser, params martini.Params, r render.Render) {
func GetSystemById(username string, params martini.Params, r render.Render) {
id, _ := strconv.Atoi(params["id"])

var servers []Server
Expand Down Expand Up @@ -135,7 +135,7 @@ func GetSystemById(username AuthUser, params martini.Params, r render.Render) {
r.HTML(200, "config", data)
}

func ToggleStarSystem(username AuthUser, params martini.Params, r render.Render) {
func ToggleStarSystem(username string, params martini.Params, r render.Render) {
id, _ := strconv.Atoi(params["id"])

var user User
Expand Down
Loading

0 comments on commit 99e9b57

Please sign in to comment.