aboutsummaryrefslogtreecommitdiffstats
path: root/models/ldap.go
blob: 1da5b87540fe463c25d7685b6828a6b88f1ec8e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright github.com/juju2013. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package models

import (
	"strings"

	"github.com/gogits/gogs/modules/auth/ldap"
	"github.com/gogits/gogs/modules/log"
)

// Query if name/passwd can login against the LDAP direcotry pool
// Create a local user if success
// Return the same LoginUserPlain semantic
func LoginUserLdap(name, passwd string) (*User, error) {
	mail, logged := ldap.LoginUser(name, passwd)
	if !logged {
		// user not in LDAP, do nothing
		return nil, ErrUserNotExist
	}
	// fake a local user creation
	user := User{
		LowerName: strings.ToLower(name),
		Name:      strings.ToLower(name),
		LoginType: 389,
		IsActive:  true,
		Passwd:    passwd,
		Email:     mail}
	_, err := RegisterUser(&user)
	if err != nil {
		log.Debug("LDAP local user %s found (%s) ", name, err)
	}
	// simulate local user login
	localUser, err2 := GetUserByName(user.Name)
	return localUser, err2
}