user.go 3.5 KB

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