github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/app/ldap.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"net/http"
     8  
     9  	l4g "github.com/alecthomas/log4go"
    10  	"github.com/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/utils"
    12  )
    13  
    14  func (a *App) SyncLdap() {
    15  	a.Go(func() {
    16  
    17  		if utils.IsLicensed() && *utils.License().Features.LDAP && *a.Config().LdapSettings.EnableSync {
    18  			if ldapI := a.Ldap; ldapI != nil {
    19  				ldapI.StartSynchronizeJob(false)
    20  			} else {
    21  				l4g.Error("%v", model.NewAppError("SyncLdap", "ent.ldap.disabled.app_error", nil, "", http.StatusNotImplemented).Error())
    22  			}
    23  		}
    24  	})
    25  }
    26  
    27  func (a *App) TestLdap() *model.AppError {
    28  	if ldapI := a.Ldap; ldapI != nil && utils.IsLicensed() && *utils.License().Features.LDAP && (*a.Config().LdapSettings.Enable || *a.Config().LdapSettings.EnableSync) {
    29  		if err := ldapI.RunTest(); err != nil {
    30  			err.StatusCode = 500
    31  			return err
    32  		}
    33  	} else {
    34  		err := model.NewAppError("TestLdap", "ent.ldap.disabled.app_error", nil, "", http.StatusNotImplemented)
    35  		return err
    36  	}
    37  
    38  	return nil
    39  }
    40  
    41  func (a *App) SwitchEmailToLdap(email, password, code, ldapId, ldapPassword string) (string, *model.AppError) {
    42  	if utils.IsLicensed() && !*a.Config().ServiceSettings.ExperimentalEnableAuthenticationTransfer {
    43  		return "", model.NewAppError("emailToLdap", "api.user.email_to_ldap.not_available.app_error", nil, "", http.StatusForbidden)
    44  	}
    45  
    46  	user, err := a.GetUserByEmail(email)
    47  	if err != nil {
    48  		return "", err
    49  	}
    50  
    51  	if err := a.CheckPasswordAndAllCriteria(user, password, code); err != nil {
    52  		return "", err
    53  	}
    54  
    55  	if err := a.RevokeAllSessions(user.Id); err != nil {
    56  		return "", err
    57  	}
    58  
    59  	ldapInterface := a.Ldap
    60  	if ldapInterface == nil {
    61  		return "", model.NewAppError("SwitchEmailToLdap", "api.user.email_to_ldap.not_available.app_error", nil, "", http.StatusNotImplemented)
    62  	}
    63  
    64  	if err := ldapInterface.SwitchToLdap(user.Id, ldapId, ldapPassword); err != nil {
    65  		return "", err
    66  	}
    67  
    68  	a.Go(func() {
    69  		if err := a.SendSignInChangeEmail(user.Email, "AD/LDAP", user.Locale, utils.GetSiteURL()); err != nil {
    70  			l4g.Error(err.Error())
    71  		}
    72  	})
    73  
    74  	return "/login?extra=signin_change", nil
    75  }
    76  
    77  func (a *App) SwitchLdapToEmail(ldapPassword, code, email, newPassword string) (string, *model.AppError) {
    78  	if utils.IsLicensed() && !*a.Config().ServiceSettings.ExperimentalEnableAuthenticationTransfer {
    79  		return "", model.NewAppError("ldapToEmail", "api.user.ldap_to_email.not_available.app_error", nil, "", http.StatusForbidden)
    80  	}
    81  
    82  	user, err := a.GetUserByEmail(email)
    83  	if err != nil {
    84  		return "", err
    85  	}
    86  
    87  	if user.AuthService != model.USER_AUTH_SERVICE_LDAP {
    88  		return "", model.NewAppError("SwitchLdapToEmail", "api.user.ldap_to_email.not_ldap_account.app_error", nil, "", http.StatusBadRequest)
    89  	}
    90  
    91  	ldapInterface := a.Ldap
    92  	if ldapInterface == nil || user.AuthData == nil {
    93  		return "", model.NewAppError("SwitchLdapToEmail", "api.user.ldap_to_email.not_available.app_error", nil, "", http.StatusNotImplemented)
    94  	}
    95  
    96  	if err := ldapInterface.CheckPassword(*user.AuthData, ldapPassword); err != nil {
    97  		return "", err
    98  	}
    99  
   100  	if err := a.CheckUserMfa(user, code); err != nil {
   101  		return "", err
   102  	}
   103  
   104  	if err := a.UpdatePassword(user, newPassword); err != nil {
   105  		return "", err
   106  	}
   107  
   108  	if err := a.RevokeAllSessions(user.Id); err != nil {
   109  		return "", err
   110  	}
   111  
   112  	T := utils.GetUserTranslations(user.Locale)
   113  
   114  	a.Go(func() {
   115  		if err := a.SendSignInChangeEmail(user.Email, T("api.templates.signin_change_email.body.method_email"), user.Locale, utils.GetSiteURL()); err != nil {
   116  			l4g.Error(err.Error())
   117  		}
   118  	})
   119  
   120  	return "/login?extra=signin_change", nil
   121  }