wide/session/users.go

85 lines
1.8 KiB
Go
Raw Normal View History

2014-09-17 10:35:48 +04:00
package session
// TODO: 这个包目前没有使用,只是开了个头 :p
2014-08-31 14:50:38 +04:00
import (
"encoding/json"
2014-09-12 13:10:58 +04:00
"net/http"
"os"
2014-08-31 14:50:38 +04:00
"github.com/b3log/wide/conf"
2014-09-01 16:50:51 +04:00
"github.com/b3log/wide/util"
2014-08-31 14:50:38 +04:00
"github.com/golang/glog"
)
const (
USER_EXISTS = "user exists"
USER_CREATED = "user created"
USER_CREATE_FAILED = "user create failed"
)
2014-09-12 13:10:58 +04:00
// 添加用户.
2014-08-31 14:50:38 +04:00
func AddUser(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-08-31 14:50:38 +04:00
decoder := json.NewDecoder(r.Body)
var args map[string]interface{}
if err := decoder.Decode(&args); err != nil {
glog.Error(err)
2014-09-01 16:50:51 +04:00
data["succ"] = false
2014-08-31 14:50:38 +04:00
return
}
username := args["username"].(string)
password := args["password"].(string)
msg := addUser(username, password)
if USER_CREATED != msg {
data["succ"] = false
data["msg"] = msg
}
}
2014-09-12 13:10:58 +04:00
// 初始化用户 git 仓库.
2014-08-31 14:50:38 +04:00
func InitGitRepos(w http.ResponseWriter, r *http.Request) {
2014-09-01 16:50:51 +04:00
data := map[string]interface{}{"succ": true}
defer util.RetJSON(w, r, data)
2014-09-17 06:11:18 +04:00
session, _ := HTTPSession.Get(r, "wide-session")
2014-08-31 14:50:38 +04:00
username := session.Values["username"].(string)
2014-09-13 12:50:18 +04:00
userRepos := conf.Wide.GetUserWorkspace(username) + string(os.PathSeparator) + "src"
2014-08-31 14:50:38 +04:00
2014-09-13 12:50:18 +04:00
glog.Info(userRepos)
2014-08-31 14:50:38 +04:00
2014-09-13 12:50:18 +04:00
// TODO: git clone
2014-08-31 14:50:38 +04:00
}
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
}
}
2014-09-13 12:50:18 +04:00
// FIXME: 新建用户时保存工作空间
newUser := conf.User{Name: username, Password: password, Workspace: ""}
2014-08-31 14:50:38 +04:00
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
}