user.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package user
  5. import (
  6. "fmt"
  7. "net/http"
  8. //"github.com/martini-contrib/binding"
  9. "github.com/martini-contrib/render"
  10. "github.com/martini-contrib/sessions"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/auth"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/utils/log"
  15. )
  16. func Profile(r render.Render) {
  17. r.HTML(200, "user/profile", map[string]interface{}{
  18. "Title": "Username",
  19. })
  20. return
  21. }
  22. func IsSignedIn(session sessions.Session) bool {
  23. id := session.Get("userId")
  24. if id == nil {
  25. return false
  26. }
  27. if s, ok := id.(int64); ok && s > 0 {
  28. return true
  29. }
  30. return false
  31. }
  32. func SignedInName(session sessions.Session) string {
  33. userName := session.Get("userName")
  34. if userName == nil {
  35. return ""
  36. }
  37. if s, ok := userName.(string); ok {
  38. return s
  39. }
  40. return ""
  41. }
  42. func SignIn(req *http.Request, r render.Render, session sessions.Session) {
  43. var (
  44. errString string
  45. account string
  46. )
  47. if req.Method == "POST" {
  48. account = req.FormValue("account")
  49. user, err := models.LoginUserPlain(account, req.FormValue("passwd"))
  50. if err == nil {
  51. // login success
  52. session.Set("userId", user.Id)
  53. session.Set("userName", user.Name)
  54. r.Redirect("/")
  55. return
  56. }
  57. // login fail
  58. errString = fmt.Sprintf("%v", err)
  59. }
  60. r.HTML(200, "user/signin", map[string]interface{}{
  61. "Title": "Log In",
  62. "Error": errString,
  63. "Account": account,
  64. })
  65. }
  66. func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r render.Render) {
  67. data["Title"] = "Sign Up"
  68. if req.Method == "GET" {
  69. r.HTML(200, "user/signup", data)
  70. return
  71. }
  72. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  73. r.HTML(200, "user/signup", data)
  74. return
  75. }
  76. //Front-end should do double check of password.
  77. u := &models.User{
  78. Name: form.Username,
  79. Email: form.Email,
  80. Passwd: form.Password,
  81. }
  82. if err := models.RegisterUser(u); err != nil {
  83. if err.Error() == models.ErrUserAlreadyExist.Error() {
  84. data["HasError"] = true
  85. data["Err_Username"] = true
  86. data["ErrorMsg"] = "Username has been already taken"
  87. auth.AssignForm(form, data)
  88. r.HTML(200, "user/signup", data)
  89. return
  90. }
  91. log.Error("user.SignUp: %v", err)
  92. r.HTML(500, "status/500", nil)
  93. return
  94. }
  95. r.Redirect("/user/login")
  96. }
  97. func Delete(req *http.Request, r render.Render) {
  98. if req.Method == "GET" {
  99. r.HTML(200, "user/delete", map[string]interface{}{
  100. "Title": "Delete user",
  101. })
  102. return
  103. }
  104. u := &models.User{}
  105. err := models.DeleteUser(u)
  106. r.HTML(403, "status/403", map[string]interface{}{
  107. "Title": fmt.Sprintf("%v", err),
  108. })
  109. }