user.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return SignedInId(session) > 0
  24. }
  25. func SignedInId(session sessions.Session) int64 {
  26. userId := session.Get("userId")
  27. if userId == nil {
  28. return 0
  29. }
  30. if s, ok := userId.(int64); ok {
  31. return s
  32. }
  33. return 0
  34. }
  35. func SignedInName(session sessions.Session) string {
  36. userName := session.Get("userName")
  37. if userName == nil {
  38. return ""
  39. }
  40. if s, ok := userName.(string); ok {
  41. return s
  42. }
  43. return ""
  44. }
  45. func SignedInUser(session sessions.Session) *models.User {
  46. id := SignedInId(session)
  47. if id <= 0 {
  48. return nil
  49. }
  50. user, err := models.GetUserById(id)
  51. if err != nil {
  52. return nil
  53. }
  54. return user
  55. }
  56. func SignIn(req *http.Request, r render.Render, session sessions.Session) {
  57. var (
  58. errString string
  59. account string
  60. )
  61. if req.Method == "POST" {
  62. account = req.FormValue("account")
  63. user, err := models.LoginUserPlain(account, req.FormValue("passwd"))
  64. if err == nil {
  65. // login success
  66. session.Set("userId", user.Id)
  67. session.Set("userName", user.Name)
  68. r.Redirect("/")
  69. return
  70. }
  71. // login fail
  72. errString = fmt.Sprintf("%v", err)
  73. }
  74. r.HTML(200, "user/signin", map[string]interface{}{
  75. "Title": "Log In",
  76. "Error": errString,
  77. "Account": account,
  78. })
  79. }
  80. func SignUp(form auth.RegisterForm, data base.TmplData, req *http.Request, r render.Render) {
  81. data["Title"] = "Sign Up"
  82. if req.Method == "GET" {
  83. r.HTML(200, "user/signup", data)
  84. return
  85. }
  86. if hasErr, ok := data["HasError"]; ok && hasErr.(bool) {
  87. r.HTML(200, "user/signup", data)
  88. return
  89. }
  90. //Front-end should do double check of password.
  91. u := &models.User{
  92. Name: form.Username,
  93. Email: form.Email,
  94. Passwd: form.Password,
  95. }
  96. if err := models.RegisterUser(u); err != nil {
  97. if err.Error() == models.ErrUserAlreadyExist.Error() {
  98. data["HasError"] = true
  99. data["Err_Username"] = true
  100. data["ErrorMsg"] = "Username has been already taken"
  101. auth.AssignForm(form, data)
  102. r.HTML(200, "user/signup", data)
  103. return
  104. }
  105. log.Error("user.SignUp: %v", err)
  106. r.HTML(500, "status/500", nil)
  107. return
  108. }
  109. r.Redirect("/user/login")
  110. }
  111. func Delete(req *http.Request, r render.Render) {
  112. if req.Method == "GET" {
  113. r.HTML(200, "user/delete", map[string]interface{}{
  114. "Title": "Delete user",
  115. })
  116. return
  117. }
  118. u := &models.User{}
  119. err := models.DeleteUser(u)
  120. r.HTML(403, "status/403", map[string]interface{}{
  121. "Title": fmt.Sprintf("%v", err),
  122. })
  123. }