auths.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package admin
  2. import (
  3. "fmt"
  4. "gitote/gitote/models"
  5. "gitote/gitote/pkg/auth/ldap"
  6. "gitote/gitote/pkg/context"
  7. "gitote/gitote/pkg/form"
  8. "gitote/gitote/pkg/setting"
  9. "github.com/Unknwon/com"
  10. "github.com/go-xorm/core"
  11. log "gopkg.in/clog.v1"
  12. )
  13. const (
  14. // AUTHS list page template
  15. AUTHS = "admin/auth/list"
  16. // AUTH_NEW page template
  17. AUTH_NEW = "admin/auth/new"
  18. // AUTH_EDIT page template
  19. AUTH_EDIT = "admin/auth/edit"
  20. )
  21. // Authentications shows authentication page
  22. func Authentications(c *context.Context) {
  23. c.Title("admin.authentication")
  24. c.PageIs("Admin")
  25. c.PageIs("AdminAuthentications")
  26. var err error
  27. c.Data["Sources"], err = models.LoginSources()
  28. if err != nil {
  29. c.ServerError("LoginSources", err)
  30. return
  31. }
  32. c.Data["Total"] = models.CountLoginSources()
  33. c.Success(AUTHS)
  34. }
  35. type dropdownItem struct {
  36. Name string
  37. Type interface{}
  38. }
  39. var (
  40. authSources = []dropdownItem{
  41. {models.LoginNames[models.LOGIN_LDAP], models.LOGIN_LDAP},
  42. {models.LoginNames[models.LOGIN_DLDAP], models.LOGIN_DLDAP},
  43. {models.LoginNames[models.LOGIN_SMTP], models.LOGIN_SMTP},
  44. {models.LoginNames[models.LOGIN_PAM], models.LOGIN_PAM},
  45. }
  46. securityProtocols = []dropdownItem{
  47. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED], ldap.SECURITY_PROTOCOL_UNENCRYPTED},
  48. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_LDAPS], ldap.SECURITY_PROTOCOL_LDAPS},
  49. {models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_START_TLS], ldap.SECURITY_PROTOCOL_START_TLS},
  50. }
  51. )
  52. // NewAuthSource shows create auth page
  53. func NewAuthSource(c *context.Context) {
  54. c.Title("admin.auths.new")
  55. c.PageIs("Admin")
  56. c.PageIs("AdminAuthentications")
  57. c.Data["type"] = models.LOGIN_LDAP
  58. c.Data["CurrentTypeName"] = models.LoginNames[models.LOGIN_LDAP]
  59. c.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SECURITY_PROTOCOL_UNENCRYPTED]
  60. c.Data["smtp_auth"] = "PLAIN"
  61. c.Data["is_active"] = true
  62. c.Data["is_default"] = true
  63. c.Data["AuthSources"] = authSources
  64. c.Data["SecurityProtocols"] = securityProtocols
  65. c.Data["SMTPAuths"] = models.SMTPAuths
  66. c.Success(AUTH_NEW)
  67. }
  68. func parseLDAPConfig(f form.Authentication) *models.LDAPConfig {
  69. return &models.LDAPConfig{
  70. Source: &ldap.Source{
  71. Host: f.Host,
  72. Port: f.Port,
  73. SecurityProtocol: ldap.SecurityProtocol(f.SecurityProtocol),
  74. SkipVerify: f.SkipVerify,
  75. BindDN: f.BindDN,
  76. UserDN: f.UserDN,
  77. BindPassword: f.BindPassword,
  78. UserBase: f.UserBase,
  79. AttributeUsername: f.AttributeUsername,
  80. AttributeName: f.AttributeName,
  81. AttributeSurname: f.AttributeSurname,
  82. AttributeMail: f.AttributeMail,
  83. AttributesInBind: f.AttributesInBind,
  84. Filter: f.Filter,
  85. GroupEnabled: f.GroupEnabled,
  86. GroupDN: f.GroupDN,
  87. GroupFilter: f.GroupFilter,
  88. GroupMemberUID: f.GroupMemberUID,
  89. UserUID: f.UserUID,
  90. AdminFilter: f.AdminFilter,
  91. },
  92. }
  93. }
  94. func parseSMTPConfig(f form.Authentication) *models.SMTPConfig {
  95. return &models.SMTPConfig{
  96. Auth: f.SMTPAuth,
  97. Host: f.SMTPHost,
  98. Port: f.SMTPPort,
  99. AllowedDomains: f.AllowedDomains,
  100. TLS: f.TLS,
  101. SkipVerify: f.SkipVerify,
  102. }
  103. }
  104. // NewAuthSourcePost creates a new auth source
  105. func NewAuthSourcePost(c *context.Context, f form.Authentication) {
  106. c.Title("admin.auths.new")
  107. c.PageIs("Admin")
  108. c.PageIs("AdminAuthentications")
  109. c.Data["CurrentTypeName"] = models.LoginNames[models.LoginType(f.Type)]
  110. c.Data["CurrentSecurityProtocol"] = models.SecurityProtocolNames[ldap.SecurityProtocol(f.SecurityProtocol)]
  111. c.Data["AuthSources"] = authSources
  112. c.Data["SecurityProtocols"] = securityProtocols
  113. c.Data["SMTPAuths"] = models.SMTPAuths
  114. hasTLS := false
  115. var config core.Conversion
  116. switch models.LoginType(f.Type) {
  117. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  118. config = parseLDAPConfig(f)
  119. hasTLS = ldap.SecurityProtocol(f.SecurityProtocol) > ldap.SECURITY_PROTOCOL_UNENCRYPTED
  120. case models.LOGIN_SMTP:
  121. config = parseSMTPConfig(f)
  122. hasTLS = true
  123. case models.LOGIN_PAM:
  124. config = &models.PAMConfig{
  125. ServiceName: f.PAMServiceName,
  126. }
  127. default:
  128. c.Error(400)
  129. return
  130. }
  131. c.Data["HasTLS"] = hasTLS
  132. if c.HasError() {
  133. c.Success(AUTH_NEW)
  134. return
  135. }
  136. if err := models.CreateLoginSource(&models.LoginSource{
  137. Type: models.LoginType(f.Type),
  138. Name: f.Name,
  139. IsActived: f.IsActive,
  140. IsDefault: f.IsDefault,
  141. Cfg: config,
  142. }); err != nil {
  143. if models.IsErrLoginSourceAlreadyExist(err) {
  144. c.Data["Err_Name"] = true
  145. c.RenderWithErr(c.Tr("admin.auths.login_source_exist", err.(models.ErrLoginSourceAlreadyExist).Name), AUTH_NEW, f)
  146. } else {
  147. c.ServerError("CreateSource", err)
  148. }
  149. return
  150. }
  151. log.Trace("Authentication created by admin(%s): %s", c.User.Name, f.Name)
  152. c.Flash.Success(c.Tr("admin.auths.new_success", f.Name))
  153. c.Redirect(setting.AppSubURL + "/admin/auths")
  154. }
  155. // EditAuthSource shows edit auth source page
  156. func EditAuthSource(c *context.Context) {
  157. c.Title("admin.auths.edit")
  158. c.PageIs("Admin")
  159. c.PageIs("AdminAuthentications")
  160. c.Data["SecurityProtocols"] = securityProtocols
  161. c.Data["SMTPAuths"] = models.SMTPAuths
  162. source, err := models.GetLoginSourceByID(c.ParamsInt64(":authid"))
  163. if err != nil {
  164. c.ServerError("GetLoginSourceByID", err)
  165. return
  166. }
  167. c.Data["Source"] = source
  168. c.Data["HasTLS"] = source.HasTLS()
  169. c.Success(AUTH_EDIT)
  170. }
  171. // EditAuthSourcePost edits an auth source
  172. func EditAuthSourcePost(c *context.Context, f form.Authentication) {
  173. c.Title("admin.auths.edit")
  174. c.PageIs("Admin")
  175. c.PageIs("AdminAuthentications")
  176. c.Data["SMTPAuths"] = models.SMTPAuths
  177. source, err := models.GetLoginSourceByID(c.ParamsInt64(":authid"))
  178. if err != nil {
  179. c.ServerError("GetLoginSourceByID", err)
  180. return
  181. }
  182. c.Data["Source"] = source
  183. c.Data["HasTLS"] = source.HasTLS()
  184. if c.HasError() {
  185. c.Success(AUTH_EDIT)
  186. return
  187. }
  188. var config core.Conversion
  189. switch models.LoginType(f.Type) {
  190. case models.LOGIN_LDAP, models.LOGIN_DLDAP:
  191. config = parseLDAPConfig(f)
  192. case models.LOGIN_SMTP:
  193. config = parseSMTPConfig(f)
  194. case models.LOGIN_PAM:
  195. config = &models.PAMConfig{
  196. ServiceName: f.PAMServiceName,
  197. }
  198. default:
  199. c.Error(400)
  200. return
  201. }
  202. source.Name = f.Name
  203. source.IsActived = f.IsActive
  204. source.IsDefault = f.IsDefault
  205. source.Cfg = config
  206. if err := models.UpdateLoginSource(source); err != nil {
  207. c.ServerError("UpdateLoginSource", err)
  208. return
  209. }
  210. log.Trace("Authentication changed by admin '%s': %d", c.User.Name, source.ID)
  211. c.Flash.Success("Authentication setting has been updated successfully.")
  212. c.Redirect(setting.AppSubURL + "/admin/auths/" + com.ToStr(f.ID))
  213. }
  214. // DeleteAuthSource deletes an auth source
  215. func DeleteAuthSource(c *context.Context) {
  216. source, err := models.GetLoginSourceByID(c.ParamsInt64(":authid"))
  217. if err != nil {
  218. c.ServerError("GetLoginSourceByID", err)
  219. return
  220. }
  221. if err = models.DeleteSource(source); err != nil {
  222. if models.IsErrLoginSourceInUse(err) {
  223. c.Flash.Error("This authentication is still used by some users, please delete or convert these users to another login type first.")
  224. } else {
  225. c.Flash.Error(fmt.Sprintf("DeleteSource: %v", err))
  226. }
  227. c.JSONSuccess(map[string]interface{}{
  228. "redirect": setting.AppSubURL + "/admin/auths/" + c.Params(":authid"),
  229. })
  230. return
  231. }
  232. log.Trace("Authentication deleted by admin(%s): %d", c.User.Name, source.ID)
  233. c.Flash.Success("Authentication has been deleted successfully!")
  234. c.JSONSuccess(map[string]interface{}{
  235. "redirect": setting.AppSubURL + "/admin/auths",
  236. })
  237. }