user.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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/render"
  9. "github.com/martini-contrib/sessions"
  10. "github.com/gogits/validation"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/utils/log"
  13. )
  14. func Profile(r render.Render) {
  15. r.HTML(200, "user/profile", map[string]interface{}{
  16. "Title": "Username",
  17. })
  18. return
  19. }
  20. func SignIn(req *http.Request, r render.Render, session sessions.Session) {
  21. var (
  22. errString string
  23. account string
  24. )
  25. if req.Method == "POST" {
  26. account = req.FormValue("account")
  27. user, err := models.LoginUserPlain(account, req.FormValue("passwd"))
  28. if err == nil {
  29. // login success
  30. session.Set("userId", user.Id)
  31. session.Set("userName", user.Name)
  32. r.Redirect("/")
  33. return
  34. }
  35. // login fail
  36. errString = fmt.Sprintf("%v", err)
  37. }
  38. r.HTML(200, "user/signin", map[string]interface{}{
  39. "Title": "Log In",
  40. "Error": errString,
  41. "Account": account,
  42. })
  43. }
  44. func SignUp(req *http.Request, r render.Render) {
  45. if req.Method == "GET" {
  46. r.HTML(200, "user/signup", map[string]interface{}{
  47. "Title": "Sign Up",
  48. })
  49. return
  50. }
  51. u := &models.User{
  52. Name: req.FormValue("username"),
  53. Email: req.FormValue("email"),
  54. Passwd: req.FormValue("passwd"),
  55. }
  56. valid := validation.Validation{}
  57. ok, err := valid.Valid(u)
  58. if err != nil {
  59. log.Error("user.SignUp -> valid user: %v", err)
  60. return
  61. }
  62. if !ok {
  63. for _, err := range valid.Errors {
  64. log.Warn("user.SignUp -> valid user: %v", err)
  65. }
  66. return
  67. }
  68. err = models.RegisterUser(u)
  69. if err != nil {
  70. if err != nil {
  71. r.HTML(200, "base/error", map[string]interface{}{
  72. "Error": fmt.Sprintf("%v", err),
  73. })
  74. return
  75. }
  76. }
  77. r.Redirect("/")
  78. }
  79. func Delete(req *http.Request, r render.Render) {
  80. if req.Method == "GET" {
  81. r.HTML(200, "user/delete", map[string]interface{}{
  82. "Title": "Delete user",
  83. })
  84. return
  85. }
  86. u := &models.User{}
  87. err := models.DeleteUser(u)
  88. r.HTML(403, "status/403", map[string]interface{}{
  89. "Title": fmt.Sprintf("%v", err),
  90. })
  91. }