Add the feature of creating user.
This commit is contained in:
@ -8,7 +8,7 @@ EXE_PATH=$TANK_DIR/tank
|
|||||||
|
|
||||||
if [ -f "$EXE_PATH" ]; then
|
if [ -f "$EXE_PATH" ]; then
|
||||||
nohup $EXE_PATH >/dev/null 2>&1 &
|
nohup $EXE_PATH >/dev/null 2>&1 &
|
||||||
echo 'Start tank successfully! http://127.0.0.1:6010'
|
echo 'Start tank successfully! Default value http://127.0.0.1:6010'
|
||||||
else
|
else
|
||||||
echo 'Cannot find $EXE_PATH.'
|
echo 'Cannot find $EXE_PATH.'
|
||||||
exit 1
|
exit 1
|
||||||
|
@ -40,6 +40,7 @@ func (this *UserController) RegisterRoutes() map[string]func(writer http.Respons
|
|||||||
routeMap["/api/user/login"] = this.Wrap(this.Login, USER_ROLE_GUEST)
|
routeMap["/api/user/login"] = this.Wrap(this.Login, USER_ROLE_GUEST)
|
||||||
routeMap["/api/user/authentication/login"] = this.Wrap(this.AuthenticationLogin, USER_ROLE_GUEST)
|
routeMap["/api/user/authentication/login"] = this.Wrap(this.AuthenticationLogin, USER_ROLE_GUEST)
|
||||||
routeMap["/api/user/register"] = this.Wrap(this.Register, USER_ROLE_GUEST)
|
routeMap["/api/user/register"] = this.Wrap(this.Register, USER_ROLE_GUEST)
|
||||||
|
routeMap["/api/user/create"] = this.Wrap(this.Create, USER_ROLE_ADMINISTRATOR)
|
||||||
routeMap["/api/user/edit"] = this.Wrap(this.Edit, USER_ROLE_USER)
|
routeMap["/api/user/edit"] = this.Wrap(this.Edit, USER_ROLE_USER)
|
||||||
routeMap["/api/user/detail"] = this.Wrap(this.Detail, USER_ROLE_USER)
|
routeMap["/api/user/detail"] = this.Wrap(this.Detail, USER_ROLE_USER)
|
||||||
routeMap["/api/user/logout"] = this.Wrap(this.Logout, USER_ROLE_GUEST)
|
routeMap["/api/user/logout"] = this.Wrap(this.Logout, USER_ROLE_GUEST)
|
||||||
@ -169,6 +170,67 @@ func (this *UserController) Register(writer http.ResponseWriter, request *http.R
|
|||||||
return this.Success(user)
|
return this.Success(user)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (this *UserController) Create(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||||
|
|
||||||
|
username := request.FormValue("username")
|
||||||
|
password := request.FormValue("password")
|
||||||
|
role := request.FormValue("role")
|
||||||
|
sizeLimitStr := request.FormValue("sizeLimit")
|
||||||
|
totalSizeLimitStr := request.FormValue("totalSizeLimit")
|
||||||
|
|
||||||
|
//only admin can edit user's sizeLimit
|
||||||
|
var sizeLimit int64 = 0
|
||||||
|
if sizeLimitStr == "" {
|
||||||
|
panic("user's limit size is required")
|
||||||
|
} else {
|
||||||
|
intSizeLimit, err := strconv.Atoi(sizeLimitStr)
|
||||||
|
if err != nil {
|
||||||
|
this.PanicError(err)
|
||||||
|
}
|
||||||
|
sizeLimit = int64(intSizeLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
var totalSizeLimit int64 = 0
|
||||||
|
if totalSizeLimitStr == "" {
|
||||||
|
panic("user's total limit size is required")
|
||||||
|
} else {
|
||||||
|
intTotalSizeLimit, err := strconv.Atoi(totalSizeLimitStr)
|
||||||
|
if err != nil {
|
||||||
|
this.PanicError(err)
|
||||||
|
}
|
||||||
|
totalSizeLimit = int64(intTotalSizeLimit)
|
||||||
|
}
|
||||||
|
|
||||||
|
if m, _ := regexp.MatchString(USERNAME_PATTERN, username); !m {
|
||||||
|
panic(result.BadRequestI18n(request, i18n.UsernameError))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(password) < 6 {
|
||||||
|
panic(result.BadRequestI18n(request, i18n.UserPasswordLengthError))
|
||||||
|
}
|
||||||
|
|
||||||
|
if this.userDao.CountByUsername(username) > 0 {
|
||||||
|
panic(result.BadRequestI18n(request, i18n.UsernameExist, username))
|
||||||
|
}
|
||||||
|
|
||||||
|
if role != USER_ROLE_USER && role != USER_ROLE_ADMINISTRATOR {
|
||||||
|
role = USER_ROLE_USER
|
||||||
|
}
|
||||||
|
|
||||||
|
user := &User{
|
||||||
|
Username: username,
|
||||||
|
Password: util.GetBcrypt(password),
|
||||||
|
Role: role,
|
||||||
|
SizeLimit: sizeLimit,
|
||||||
|
TotalSizeLimit: totalSizeLimit,
|
||||||
|
Status: USER_STATUS_OK,
|
||||||
|
}
|
||||||
|
|
||||||
|
user = this.userDao.Create(user)
|
||||||
|
|
||||||
|
return this.Success(user)
|
||||||
|
}
|
||||||
|
|
||||||
func (this *UserController) Edit(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
func (this *UserController) Edit(writer http.ResponseWriter, request *http.Request) *result.WebResult {
|
||||||
|
|
||||||
uuid := request.FormValue("uuid")
|
uuid := request.FormValue("uuid")
|
||||||
|
@ -123,6 +123,11 @@ func (this *TankConfig) ReadFromConfigFile() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//use default server port
|
||||||
|
if this.item.ServerPort != 0 {
|
||||||
|
this.serverPort = this.item.ServerPort
|
||||||
|
}
|
||||||
|
|
||||||
//check the integrity
|
//check the integrity
|
||||||
itemValidate := this.item.validate()
|
itemValidate := this.item.validate()
|
||||||
if !itemValidate {
|
if !itemValidate {
|
||||||
@ -139,11 +144,6 @@ func (this *TankConfig) ReadFromConfigFile() {
|
|||||||
}
|
}
|
||||||
util.MakeDirAll(this.matterPath)
|
util.MakeDirAll(this.matterPath)
|
||||||
|
|
||||||
//use default server port
|
|
||||||
if this.item.ServerPort != 0 {
|
|
||||||
this.serverPort = this.item.ServerPort
|
|
||||||
}
|
|
||||||
|
|
||||||
this.mysqlUrl = util.GetMysqlUrl(this.item.MysqlPort, this.item.MysqlHost, this.item.MysqlSchema, this.item.MysqlUsername, this.item.MysqlPassword)
|
this.mysqlUrl = util.GetMysqlUrl(this.item.MysqlPort, this.item.MysqlHost, this.item.MysqlSchema, this.item.MysqlUsername, this.item.MysqlPassword)
|
||||||
this.installed = true
|
this.installed = true
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user