wide/user/users.go

79 lines
1.7 KiB
Go
Raw Normal View History

2014-08-31 14:50:38 +04:00
package user
import (
"encoding/json"
"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"
"net/http"
2014-09-05 07:07:29 +04:00
"os"
2014-08-31 14:50:38 +04:00
)
const (
USER_EXISTS = "user exists"
USER_CREATED = "user created"
USER_CREATE_FAILED = "user create failed"
)
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
}
}
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-08-31 14:50:38 +04:00
session, _ := Session.Get(r, "wide-session")
username := session.Values["username"].(string)
2014-09-05 07:07:29 +04:00
userRepos := conf.Wide.UserWorkspaces + string(os.PathSeparator) + username + string(os.PathSeparator) + "src"
2014-08-31 14:50:38 +04:00
// TODO: git clone
2014-09-05 07:07:29 +04:00
glog.Infof("Git Cloned from [%s] to [%s]", conf.Wide.Workspace+string(os.PathSeparator)+"src", userRepos)
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
}
}
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
}