You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.5 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  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 auth
  5. import (
  6. "net/url"
  7. "strings"
  8. "github.com/Unknwon/com"
  9. "github.com/go-macaron/binding"
  10. "gopkg.in/macaron.v1"
  11. "github.com/gogits/gogs/models"
  12. )
  13. // _______________________________________ _________.______________________ _______________.___.
  14. // \______ \_ _____/\______ \_____ \ / _____/| \__ ___/\_____ \\______ \__ | |
  15. // | _/| __)_ | ___// | \ \_____ \ | | | | / | \| _// | |
  16. // | | \| \ | | / | \/ \| | | | / | \ | \\____ |
  17. // |____|_ /_______ / |____| \_______ /_______ /|___| |____| \_______ /____|_ // ______|
  18. // \/ \/ \/ \/ \/ \/ \/
  19. type CreateRepoForm struct {
  20. Uid int64 `binding:"Required"`
  21. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  22. Private bool
  23. Description string `binding:"MaxSize(255)"`
  24. AutoInit bool
  25. Gitignores string
  26. License string
  27. Readme string
  28. }
  29. func (f *CreateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  30. return validate(errs, ctx.Data, f, ctx.Locale)
  31. }
  32. type MigrateRepoForm struct {
  33. CloneAddr string `json:"clone_addr" binding:"Required"`
  34. AuthUsername string `json:"auth_username"`
  35. AuthPassword string `json:"auth_password"`
  36. Uid int64 `json:"uid" binding:"Required"`
  37. RepoName string `json:"repo_name" binding:"Required;AlphaDashDot;MaxSize(100)"`
  38. Mirror bool `json:"mirror"`
  39. Private bool `json:"private"`
  40. Description string `json:"description" binding:"MaxSize(255)"`
  41. }
  42. func (f *MigrateRepoForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  43. return validate(errs, ctx.Data, f, ctx.Locale)
  44. }
  45. // ParseRemoteAddr checks if given remote address is valid,
  46. // and returns composed URL with needed username and passowrd.
  47. // It also checks if given user has permission when remote address
  48. // is actually a local path.
  49. func (f MigrateRepoForm) ParseRemoteAddr(user *models.User) (string, error) {
  50. remoteAddr := f.CloneAddr
  51. // Remote address can be HTTP/HTTPS/Git URL or local path.
  52. if strings.HasPrefix(remoteAddr, "http://") ||
  53. strings.HasPrefix(remoteAddr, "https://") ||
  54. strings.HasPrefix(remoteAddr, "git://") {
  55. u, err := url.Parse(remoteAddr)
  56. if err != nil {
  57. return "", models.ErrInvalidCloneAddr{IsURLError: true}
  58. }
  59. if len(f.AuthUsername)+len(f.AuthPassword) > 0 {
  60. u.User = url.UserPassword(f.AuthUsername, f.AuthPassword)
  61. }
  62. remoteAddr = u.String()
  63. } else if !user.CanImportLocal() {
  64. return "", models.ErrInvalidCloneAddr{IsPermissionDenied: true}
  65. } else if !com.IsDir(remoteAddr) {
  66. return "", models.ErrInvalidCloneAddr{IsInvalidPath: true}
  67. }
  68. return remoteAddr, nil
  69. }
  70. type RepoSettingForm struct {
  71. RepoName string `binding:"Required;AlphaDashDot;MaxSize(100)"`
  72. Description string `binding:"MaxSize(255)"`
  73. Website string `binding:"Url;MaxSize(100)"`
  74. Branch string
  75. Interval int
  76. Private bool
  77. }
  78. func (f *RepoSettingForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  79. return validate(errs, ctx.Data, f, ctx.Locale)
  80. }
  81. // __ __ ___. .__ .__ __
  82. // / \ / \ ____\_ |__ | |__ | |__ ____ | | __
  83. // \ \/\/ // __ \| __ \| | \| | \ / _ \| |/ /
  84. // \ /\ ___/| \_\ \ Y \ Y ( <_> ) <
  85. // \__/\ / \___ >___ /___| /___| /\____/|__|_ \
  86. // \/ \/ \/ \/ \/ \/
  87. type WebhookForm struct {
  88. Events string
  89. Create bool
  90. Push bool
  91. Active bool
  92. }
  93. func (f WebhookForm) PushOnly() bool {
  94. return f.Events == "push_only"
  95. }
  96. func (f WebhookForm) SendEverything() bool {
  97. return f.Events == "send_everything"
  98. }
  99. func (f WebhookForm) ChooseEvents() bool {
  100. return f.Events == "choose_events"
  101. }
  102. type NewWebhookForm struct {
  103. PayloadURL string `binding:"Required;Url"`
  104. ContentType int `binding:"Required"`
  105. Secret string
  106. WebhookForm
  107. }
  108. func (f *NewWebhookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  109. return validate(errs, ctx.Data, f, ctx.Locale)
  110. }
  111. type NewSlackHookForm struct {
  112. PayloadURL string `binding:"Required;Url`
  113. Channel string `binding:"Required"`
  114. Username string
  115. IconURL string
  116. Color string
  117. WebhookForm
  118. }
  119. func (f *NewSlackHookForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  120. return validate(errs, ctx.Data, f, ctx.Locale)
  121. }
  122. // .___
  123. // | | ______ ________ __ ____
  124. // | |/ ___// ___/ | \_/ __ \
  125. // | |\___ \ \___ \| | /\ ___/
  126. // |___/____ >____ >____/ \___ >
  127. // \/ \/ \/
  128. type CreateIssueForm struct {
  129. Title string `binding:"Required;MaxSize(255)"`
  130. LabelIDs string `form:"label_ids"`
  131. MilestoneID int64
  132. AssigneeID int64
  133. Content string
  134. Attachments []string
  135. }
  136. func (f *CreateIssueForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  137. return validate(errs, ctx.Data, f, ctx.Locale)
  138. }
  139. type CreateCommentForm struct {
  140. Content string
  141. Status string `binding:"OmitEmpty;In(reopen,close)"`
  142. Attachments []string
  143. }
  144. func (f *CreateCommentForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  145. return validate(errs, ctx.Data, f, ctx.Locale)
  146. }
  147. // _____ .__.__ __
  148. // / \ |__| | ____ _______/ |_ ____ ____ ____
  149. // / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
  150. // / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
  151. // \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
  152. // \/ \/ \/ \/ \/
  153. type CreateMilestoneForm struct {
  154. Title string `binding:"Required;MaxSize(50)"`
  155. Content string
  156. Deadline string
  157. }
  158. func (f *CreateMilestoneForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  159. return validate(errs, ctx.Data, f, ctx.Locale)
  160. }
  161. // .____ ___. .__
  162. // | | _____ \_ |__ ____ | |
  163. // | | \__ \ | __ \_/ __ \| |
  164. // | |___ / __ \| \_\ \ ___/| |__
  165. // |_______ (____ /___ /\___ >____/
  166. // \/ \/ \/ \/
  167. type CreateLabelForm struct {
  168. ID int64
  169. Title string `binding:"Required;MaxSize(50)" locale:"repo.issues.label_name"`
  170. Color string `binding:"Required;Size(7)" locale:"repo.issues.label_color"`
  171. }
  172. func (f *CreateLabelForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  173. return validate(errs, ctx.Data, f, ctx.Locale)
  174. }
  175. // __________ .__
  176. // \______ \ ____ | | ____ _____ ______ ____
  177. // | _// __ \| | _/ __ \\__ \ / ___// __ \
  178. // | | \ ___/| |_\ ___/ / __ \_\___ \\ ___/
  179. // |____|_ /\___ >____/\___ >____ /____ >\___ >
  180. // \/ \/ \/ \/ \/ \/
  181. type NewReleaseForm struct {
  182. TagName string `binding:"Required"`
  183. Target string `form:"tag_target" binding:"Required"`
  184. Title string `binding:"Required"`
  185. Content string
  186. Draft string
  187. Prerelease bool
  188. }
  189. func (f *NewReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  190. return validate(errs, ctx.Data, f, ctx.Locale)
  191. }
  192. type EditReleaseForm struct {
  193. Title string `form:"title" binding:"Required"`
  194. Content string `form:"content" binding:"Required"`
  195. Draft string `form:"draft"`
  196. Prerelease bool `form:"prerelease"`
  197. }
  198. func (f *EditReleaseForm) Validate(ctx *macaron.Context, errs binding.Errors) binding.Errors {
  199. return validate(errs, ctx.Data, f, ctx.Locale)
  200. }