新建用户

This commit is contained in:
Liang Ding 2014-08-31 18:50:38 +08:00
parent d0d454aa03
commit fef4a30801
6 changed files with 129 additions and 14 deletions

View File

@ -12,8 +12,9 @@ import (
"strings"
)
type user struct {
type User struct {
Name string
Password string
}
type conf struct {
@ -28,12 +29,35 @@ type conf struct {
RuntimeMode string
Repos string
UserRepos string
Users []user
Users []User
}
var Wide conf
var rawWide conf
func init() {
func Save() bool {
// 可变部分
rawWide.Users = Wide.Users
// 原始配置文件内容
bytes, err := json.MarshalIndent(rawWide, "", " ")
if nil != err {
glog.Error(err)
return false
}
if err = ioutil.WriteFile("conf/wide.json", bytes, 0644); nil != err {
glog.Error(err)
return false
}
return true
}
func Load() {
bytes, _ := ioutil.ReadFile("conf/wide.json")
err := json.Unmarshal(bytes, &Wide)
@ -43,6 +67,9 @@ func init() {
os.Exit(-1)
}
// 保存未经变量替换处理的原始配置文件,用于写回时
json.Unmarshal(bytes, &rawWide)
ip, err := util.Net.LocalIP()
if err != nil {
glog.Error(err)

View File

@ -10,9 +10,10 @@
"RuntimeMode": "dev",
"Repos": "{pwd}/data/repos/src",
"UserRepos": "{pwd}/data/user_repos/{user}/src",
"Users": [{
"Name": "daniel"
}, {
"Name": "vanessa"
}]
"Users": [
{
"Name": "admin",
"Password": "admin"
}
]
}

@ -1 +0,0 @@
Subproject commit 5ce253230630fedff4a65ef2fc6d5ae7bebfe6fa

View File

@ -16,7 +16,7 @@ import (
func GetFiles(w http.ResponseWriter, r *http.Request) {
session, _ := user.Session.Get(r, "wide-session")
username := session.Values["name"].(string)
username := session.Values["username"].(string)
userRepos := strings.Replace(conf.Wide.UserRepos, "{user}", username, -1)

View File

@ -21,11 +21,11 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
session, _ := user.Session.Get(r, "wide-session")
if session.IsNew {
// TODO: 以 daniel 作为用户登录
// TODO: 以 admin 作为用户登录
name := conf.Wide.Users[0].Name
glog.Infof("[%s] logged in", name)
session.Values["name"] = name
session.Values["username"] = name
session.Values["id"] = strconv.Itoa(rand.Int())
}
@ -44,6 +44,8 @@ func indexHandler(w http.ResponseWriter, r *http.Request) {
}
func main() {
conf.Load()
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", indexHandler)
@ -65,6 +67,9 @@ func main() {
http.HandleFunc("/autocomplete", editor.AutocompleteHandler)
http.HandleFunc("/user/new", user.AddUser)
http.HandleFunc("/user/repos/init", user.InitGitRepos)
glog.Infof("Wide is running [%s]", conf.Wide.Server)
err := http.ListenAndServe(conf.Wide.Server, nil)

83
user/users.go Normal file
View File

@ -0,0 +1,83 @@
package user
import (
"encoding/json"
"github.com/b3log/wide/conf"
"github.com/golang/glog"
"net/http"
"strings"
)
const (
USER_EXISTS = "user exists"
USER_CREATED = "user created"
USER_CREATE_FAILED = "user create failed"
)
func AddUser(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
http.Error(w, err.Error(), 500)
return
}
username := args["username"].(string)
password := args["password"].(string)
data := map[string]interface{}{"succ": true}
msg := addUser(username, password)
if USER_CREATED != msg {
data["succ"] = false
data["msg"] = msg
}
ret, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
func InitGitRepos(w http.ResponseWriter, r *http.Request) {
session, _ := Session.Get(r, "wide-session")
username := session.Values["username"].(string)
userRepos := strings.Replace(conf.Wide.UserRepos, "{user}", username, -1)
data := map[string]interface{}{"succ": true}
// TODO: git clone
glog.Infof("Git Cloned from [%s] to [%s]", conf.Wide.Repos, userRepos)
ret, _ := json.Marshal(data)
w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
func addUser(username, password string) string {
// TODO: https://github.com/b3log/wide/issues/23
conf.Load()
// XXX: 新建用户校验增强
for _, user := range conf.Wide.Users {
if user.Name == username {
return USER_EXISTS
}
}
newUser := conf.User{Name: username, Password: password}
conf.Wide.Users = append(conf.Wide.Users, newUser)
if !conf.Save() {
return USER_CREATE_FAILED
}
glog.Infof("Created a user [%s]", username)
return USER_CREATED
}