|
|
@ -33,6 +33,7 @@ func (this *UserController) RegisterRoutes() map[string]func(writer http.Respons |
|
|
|
|
|
|
|
|
//每个Controller需要主动注册自己的路由。
|
|
|
//每个Controller需要主动注册自己的路由。
|
|
|
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/register"] = this.Wrap(this.Register, USER_ROLE_GUEST) |
|
|
routeMap["/api/user/register"] = this.Wrap(this.Register, USER_ROLE_GUEST) |
|
|
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) |
|
|
@ -41,10 +42,41 @@ func (this *UserController) RegisterRoutes() map[string]func(writer http.Respons |
|
|
routeMap["/api/user/reset/password"] = this.Wrap(this.ResetPassword, USER_ROLE_ADMINISTRATOR) |
|
|
routeMap["/api/user/reset/password"] = this.Wrap(this.ResetPassword, USER_ROLE_ADMINISTRATOR) |
|
|
routeMap["/api/user/page"] = this.Wrap(this.Page, USER_ROLE_ADMINISTRATOR) |
|
|
routeMap["/api/user/page"] = this.Wrap(this.Page, USER_ROLE_ADMINISTRATOR) |
|
|
routeMap["/api/user/toggle/status"] = this.Wrap(this.ToggleStatus, USER_ROLE_ADMINISTRATOR) |
|
|
routeMap["/api/user/toggle/status"] = this.Wrap(this.ToggleStatus, USER_ROLE_ADMINISTRATOR) |
|
|
|
|
|
routeMap["/api/user/transfiguration"] = this.Wrap(this.Transfiguration, USER_ROLE_ADMINISTRATOR) |
|
|
|
|
|
|
|
|
return routeMap |
|
|
return routeMap |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (this *UserController) innerLogin(writer http.ResponseWriter, request *http.Request, user *User) { |
|
|
|
|
|
|
|
|
|
|
|
//登录成功,设置Cookie。有效期30天。
|
|
|
|
|
|
expiration := time.Now() |
|
|
|
|
|
expiration = expiration.AddDate(0, 0, 30) |
|
|
|
|
|
|
|
|
|
|
|
//持久化用户的session.
|
|
|
|
|
|
session := &Session{ |
|
|
|
|
|
UserUuid: user.Uuid, |
|
|
|
|
|
Ip: util.GetIpAddress(request), |
|
|
|
|
|
ExpireTime: expiration, |
|
|
|
|
|
} |
|
|
|
|
|
session.UpdateTime = time.Now() |
|
|
|
|
|
session.CreateTime = time.Now() |
|
|
|
|
|
session = this.sessionDao.Create(session) |
|
|
|
|
|
|
|
|
|
|
|
//设置用户的cookie.
|
|
|
|
|
|
cookie := http.Cookie{ |
|
|
|
|
|
Name: core.COOKIE_AUTH_KEY, |
|
|
|
|
|
Path: "/", |
|
|
|
|
|
Value: session.Uuid, |
|
|
|
|
|
Expires: expiration} |
|
|
|
|
|
http.SetCookie(writer, &cookie) |
|
|
|
|
|
|
|
|
|
|
|
//更新用户上次登录时间和ip
|
|
|
|
|
|
user.LastTime = time.Now() |
|
|
|
|
|
user.LastIp = util.GetIpAddress(request) |
|
|
|
|
|
this.userDao.Save(user) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//使用用户名和密码进行登录。
|
|
|
//使用用户名和密码进行登录。
|
|
|
//参数:
|
|
|
//参数:
|
|
|
// @username:用户名
|
|
|
// @username:用户名
|
|
|
@ -69,33 +101,29 @@ func (this *UserController) Login(writer http.ResponseWriter, request *http.Requ |
|
|
panic(result.BadRequest("用户名或密码错误")) |
|
|
panic(result.BadRequest("用户名或密码错误")) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//登录成功,设置Cookie。有效期30天。
|
|
|
this.innerLogin(writer, request, user) |
|
|
expiration := time.Now() |
|
|
|
|
|
expiration = expiration.AddDate(0, 0, 30) |
|
|
|
|
|
|
|
|
|
|
|
//持久化用户的session.
|
|
|
return this.Success(user) |
|
|
session := &Session{ |
|
|
|
|
|
UserUuid: user.Uuid, |
|
|
|
|
|
Ip: util.GetIpAddress(request), |
|
|
|
|
|
ExpireTime: expiration, |
|
|
|
|
|
} |
|
|
} |
|
|
session.UpdateTime = time.Now() |
|
|
|
|
|
session.CreateTime = time.Now() |
|
|
|
|
|
session = this.sessionDao.Create(session) |
|
|
|
|
|
|
|
|
|
|
|
//设置用户的cookie.
|
|
|
//使用Authentication进行登录。
|
|
|
cookie := http.Cookie{ |
|
|
func (this *UserController) AuthenticationLogin(writer http.ResponseWriter, request *http.Request) *result.WebResult { |
|
|
Name: core.COOKIE_AUTH_KEY, |
|
|
|
|
|
Path: "/", |
|
|
|
|
|
Value: session.Uuid, |
|
|
|
|
|
Expires: expiration} |
|
|
|
|
|
http.SetCookie(writer, &cookie) |
|
|
|
|
|
|
|
|
|
|
|
//更新用户上次登录时间和ip
|
|
|
authentication := request.FormValue("authentication") |
|
|
user.LastTime = time.Now() |
|
|
if authentication == "" { |
|
|
user.LastIp = util.GetIpAddress(request) |
|
|
panic(result.BadRequest("authentication 必填")) |
|
|
this.userDao.Save(user) |
|
|
} |
|
|
|
|
|
session := this.sessionDao.FindByUuid(authentication) |
|
|
|
|
|
if session == nil { |
|
|
|
|
|
panic(result.BadRequest("authentication 错误")) |
|
|
|
|
|
} |
|
|
|
|
|
duration := session.ExpireTime.Sub(time.Now()) |
|
|
|
|
|
if duration <= 0 { |
|
|
|
|
|
panic(result.BadRequest("登录信息已过期")) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
user := this.userDao.CheckByUuid(session.UserUuid) |
|
|
|
|
|
this.innerLogin(writer, request, user) |
|
|
return this.Success(user) |
|
|
return this.Success(user) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -105,6 +133,11 @@ func (this *UserController) Register(writer http.ResponseWriter, request *http.R |
|
|
username := request.FormValue("username") |
|
|
username := request.FormValue("username") |
|
|
password := request.FormValue("password") |
|
|
password := request.FormValue("password") |
|
|
|
|
|
|
|
|
|
|
|
preference := this.preferenceService.Fetch() |
|
|
|
|
|
if !preference.AllowRegister { |
|
|
|
|
|
panic(result.Unauthorized("管理员已禁用自主注册!")) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, username); !m { |
|
|
if m, _ := regexp.MatchString(`^[0-9a-zA-Z_]+$`, username); !m { |
|
|
panic(`用户名必填,且只能包含字母,数字和'_''`) |
|
|
panic(`用户名必填,且只能包含字母,数字和'_''`) |
|
|
} |
|
|
} |
|
|
@ -115,11 +148,9 @@ func (this *UserController) Register(writer http.ResponseWriter, request *http.R |
|
|
|
|
|
|
|
|
//判断重名。
|
|
|
//判断重名。
|
|
|
if this.userDao.CountByUsername(username) > 0 { |
|
|
if this.userDao.CountByUsername(username) > 0 { |
|
|
panic(result.BadRequest("%s已经被其他用户占用。", username)) |
|
|
panic(result.BadRequest("%s已经被使用,请更换。", username)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
preference := this.preferenceService.Fetch() |
|
|
|
|
|
|
|
|
|
|
|
user := &User{ |
|
|
user := &User{ |
|
|
Role: USER_ROLE_USER, |
|
|
Role: USER_ROLE_USER, |
|
|
Username: username, |
|
|
Username: username, |
|
|
@ -130,6 +161,9 @@ func (this *UserController) Register(writer http.ResponseWriter, request *http.R |
|
|
|
|
|
|
|
|
user = this.userDao.Create(user) |
|
|
user = this.userDao.Create(user) |
|
|
|
|
|
|
|
|
|
|
|
//做一次登录操作
|
|
|
|
|
|
this.innerLogin(writer, request, user) |
|
|
|
|
|
|
|
|
return this.Success(user) |
|
|
return this.Success(user) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -291,6 +325,29 @@ func (this *UserController) ToggleStatus(writer http.ResponseWriter, request *ht |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//变身为指定用户。
|
|
|
|
|
|
func (this *UserController) Transfiguration(writer http.ResponseWriter, request *http.Request) *result.WebResult { |
|
|
|
|
|
|
|
|
|
|
|
uuid := request.FormValue("uuid") |
|
|
|
|
|
currentUser := this.userDao.CheckByUuid(uuid) |
|
|
|
|
|
|
|
|
|
|
|
//有效期10分钟
|
|
|
|
|
|
expiration := time.Now() |
|
|
|
|
|
expiration = expiration.Add(10 * time.Minute) |
|
|
|
|
|
|
|
|
|
|
|
//持久化用户的session.
|
|
|
|
|
|
session := &Session{ |
|
|
|
|
|
UserUuid: currentUser.Uuid, |
|
|
|
|
|
Ip: util.GetIpAddress(request), |
|
|
|
|
|
ExpireTime: expiration, |
|
|
|
|
|
} |
|
|
|
|
|
session.UpdateTime = time.Now() |
|
|
|
|
|
session.CreateTime = time.Now() |
|
|
|
|
|
session = this.sessionDao.Create(session) |
|
|
|
|
|
|
|
|
|
|
|
return this.Success(session.Uuid) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
//用户修改密码
|
|
|
//用户修改密码
|
|
|
func (this *UserController) ChangePassword(writer http.ResponseWriter, request *http.Request) *result.WebResult { |
|
|
func (this *UserController) ChangePassword(writer http.ResponseWriter, request *http.Request) *result.WebResult { |
|
|
|
|
|
|
|
|
|