auths.go 7.1 KB

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