2017-04-12 08:27:57 -04:00
|
|
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
2017-01-25 09:32:42 -05:00
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package app
|
|
|
|
|
|
|
|
|
|
import (
|
2018-04-27 12:49:45 -07:00
|
|
|
"fmt"
|
2017-01-25 09:32:42 -05:00
|
|
|
"net/http"
|
|
|
|
|
|
2018-04-27 12:49:45 -07:00
|
|
|
"github.com/mattermost/mattermost-server/mlog"
|
2017-09-06 23:05:10 -07:00
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
"github.com/mattermost/mattermost-server/utils"
|
2017-01-25 09:32:42 -05:00
|
|
|
)
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
func (a *App) SyncLdap() {
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.Go(func() {
|
2017-10-31 08:37:34 -07:00
|
|
|
|
2018-02-06 17:25:49 -06:00
|
|
|
if license := a.License(); license != nil && *license.Features.LDAP && *a.Config().LdapSettings.EnableSync {
|
2017-09-19 18:31:35 -05:00
|
|
|
if ldapI := a.Ldap; ldapI != nil {
|
2017-09-25 13:30:33 -07:00
|
|
|
ldapI.StartSynchronizeJob(false)
|
2017-01-25 09:32:42 -05:00
|
|
|
} else {
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Error(fmt.Sprintf("%v", model.NewAppError("SyncLdap", "ent.ldap.disabled.app_error", nil, "", http.StatusNotImplemented).Error()))
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
}
|
2017-10-03 10:53:53 -05:00
|
|
|
})
|
2017-01-25 09:32:42 -05:00
|
|
|
}
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
func (a *App) TestLdap() *model.AppError {
|
2018-02-06 17:25:49 -06:00
|
|
|
license := a.License()
|
|
|
|
|
if ldapI := a.Ldap; ldapI != nil && license != nil && *license.Features.LDAP && (*a.Config().LdapSettings.Enable || *a.Config().LdapSettings.EnableSync) {
|
2017-01-25 09:32:42 -05:00
|
|
|
if err := ldapI.RunTest(); err != nil {
|
|
|
|
|
err.StatusCode = 500
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2017-09-01 16:42:02 +01:00
|
|
|
err := model.NewAppError("TestLdap", "ent.ldap.disabled.app_error", nil, "", http.StatusNotImplemented)
|
2017-01-25 09:32:42 -05:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2017-04-10 08:19:49 -04:00
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
// GetLdapGroup retrieves a single LDAP group by the given LDAP group id.
|
|
|
|
|
func (a *App) GetLdapGroup(ldapGroupID string) (*model.Group, *model.AppError) {
|
|
|
|
|
var group *model.Group
|
|
|
|
|
|
|
|
|
|
if a.Ldap != nil {
|
|
|
|
|
var err *model.AppError
|
|
|
|
|
group, err = a.Ldap.GetGroup(ldapGroupID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ae := model.NewAppError("GetLdapGroup", "ent.ldap.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
mlog.Error(fmt.Sprintf("%v", ae.Error()))
|
|
|
|
|
return nil, ae
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return group, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GetAllLdapGroupsPage retrieves all LDAP groups under the configured base DN using the default or configured group
|
|
|
|
|
// filter.
|
2019-05-10 11:47:21 -04:00
|
|
|
func (a *App) GetAllLdapGroupsPage(page int, perPage int, opts model.LdapGroupSearchOpts) ([]*model.Group, int, *model.AppError) {
|
2019-01-10 15:17:31 -05:00
|
|
|
var groups []*model.Group
|
|
|
|
|
var total int
|
|
|
|
|
|
|
|
|
|
if a.Ldap != nil {
|
|
|
|
|
var err *model.AppError
|
2019-03-14 15:43:52 -04:00
|
|
|
groups, total, err = a.Ldap.GetAllGroupsPage(page, perPage, opts)
|
2019-01-10 15:17:31 -05:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, 0, err
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
ae := model.NewAppError("GetAllLdapGroupsPage", "ent.ldap.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
mlog.Error(fmt.Sprintf("%v", ae.Error()))
|
|
|
|
|
return nil, 0, ae
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groups, total, nil
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 09:46:09 -07:00
|
|
|
func (a *App) SwitchEmailToLdap(email, password, code, ldapLoginId, ldapPassword string) (string, *model.AppError) {
|
2018-02-06 17:25:49 -06:00
|
|
|
if a.License() != nil && !*a.Config().ServiceSettings.ExperimentalEnableAuthenticationTransfer {
|
2017-11-28 11:46:48 -08:00
|
|
|
return "", model.NewAppError("emailToLdap", "api.user.email_to_ldap.not_available.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
user, err := a.GetUserByEmail(email)
|
2017-04-10 08:19:49 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
if err := a.CheckPasswordAndAllCriteria(user, password, code); err != nil {
|
2017-04-10 08:19:49 -04:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
if err := a.RevokeAllSessions(user.Id); err != nil {
|
2017-04-10 08:19:49 -04:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
ldapInterface := a.Ldap
|
2017-04-10 08:19:49 -04:00
|
|
|
if ldapInterface == nil {
|
|
|
|
|
return "", model.NewAppError("SwitchEmailToLdap", "api.user.email_to_ldap.not_available.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 09:46:09 -07:00
|
|
|
if err := ldapInterface.SwitchToLdap(user.Id, ldapLoginId, ldapPassword); err != nil {
|
2017-04-10 08:19:49 -04:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.Go(func() {
|
2018-02-22 18:23:32 -06:00
|
|
|
if err := a.SendSignInChangeEmail(user.Email, "AD/LDAP", user.Locale, a.GetSiteURL()); err != nil {
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Error(err.Error())
|
2017-04-10 08:19:49 -04:00
|
|
|
}
|
2017-10-03 10:53:53 -05:00
|
|
|
})
|
2017-04-10 08:19:49 -04:00
|
|
|
|
|
|
|
|
return "/login?extra=signin_change", nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
func (a *App) SwitchLdapToEmail(ldapPassword, code, email, newPassword string) (string, *model.AppError) {
|
2018-02-06 17:25:49 -06:00
|
|
|
if a.License() != nil && !*a.Config().ServiceSettings.ExperimentalEnableAuthenticationTransfer {
|
2017-11-28 11:46:48 -08:00
|
|
|
return "", model.NewAppError("ldapToEmail", "api.user.ldap_to_email.not_available.app_error", nil, "", http.StatusForbidden)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
user, err := a.GetUserByEmail(email)
|
2017-04-10 08:19:49 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if user.AuthService != model.USER_AUTH_SERVICE_LDAP {
|
|
|
|
|
return "", model.NewAppError("SwitchLdapToEmail", "api.user.ldap_to_email.not_ldap_account.app_error", nil, "", http.StatusBadRequest)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
ldapInterface := a.Ldap
|
2017-04-10 08:19:49 -04:00
|
|
|
if ldapInterface == nil || user.AuthData == nil {
|
|
|
|
|
return "", model.NewAppError("SwitchLdapToEmail", "api.user.ldap_to_email.not_available.app_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 09:46:09 -07:00
|
|
|
if err := ldapInterface.CheckPasswordAuthData(*user.AuthData, ldapPassword); err != nil {
|
2017-04-10 08:19:49 -04:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-19 18:31:35 -05:00
|
|
|
if err := a.CheckUserMfa(user, code); err != nil {
|
2017-04-10 08:19:49 -04:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
if err := a.UpdatePassword(user, newPassword); err != nil {
|
2017-04-10 08:19:49 -04:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-06 17:12:54 -05:00
|
|
|
if err := a.RevokeAllSessions(user.Id); err != nil {
|
2017-04-10 08:19:49 -04:00
|
|
|
return "", err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T := utils.GetUserTranslations(user.Locale)
|
|
|
|
|
|
2018-11-07 10:20:07 -08:00
|
|
|
a.Srv.Go(func() {
|
2018-02-22 18:23:32 -06:00
|
|
|
if err := a.SendSignInChangeEmail(user.Email, T("api.templates.signin_change_email.body.method_email"), user.Locale, a.GetSiteURL()); err != nil {
|
2018-04-27 12:49:45 -07:00
|
|
|
mlog.Error(err.Error())
|
2017-04-10 08:19:49 -04:00
|
|
|
}
|
2017-10-03 10:53:53 -05:00
|
|
|
})
|
2017-04-10 08:19:49 -04:00
|
|
|
|
|
|
|
|
return "/login?extra=signin_change", nil
|
|
|
|
|
}
|