aboutsummaryrefslogtreecommitdiff
path: root/models/login.go
blob: 21e1ce686ae9b2b535f1b82b54c9fba642448774 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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 (
	"encoding/json"
	"errors"
	"time"

	"github.com/go-xorm/core"
	"github.com/go-xorm/xorm"

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

// Login types.
const (
	LT_PLAIN = iota + 1
	LT_LDAP
	LT_SMTP
)

var (
	ErrAuthenticationAlreadyExist = errors.New("Authentication already exist")
	ErrAuthenticationNotExist     = errors.New("Authentication does not exist")
	ErrAuthenticationUserUsed     = errors.New("Authentication has been used by some users")
)

var LoginTypes = map[int]string{
	LT_LDAP: "LDAP",
	LT_SMTP: "SMTP",
}

var _ core.Conversion = &LDAPConfig{}

type LDAPConfig struct {
	ldap.Ldapsource
}

// implement
func (cfg *LDAPConfig) FromDB(bs []byte) error {
	return json.Unmarshal(bs, &cfg.Ldapsource)
}

func (cfg *LDAPConfig) ToDB() ([]byte, error) {
	return json.Marshal(cfg.Ldapsource)
}

type LoginSource struct {
	Id        int64
	Type      int
	Name      string          `xorm:"unique"`
	IsActived bool            `xorm:"not null default false"`
	Cfg       core.Conversion `xorm:"TEXT"`
	Created   time.Time       `xorm:"created"`
	Updated   time.Time       `xorm:"updated"`
}

func (source *LoginSource) TypeString() string {
	return LoginTypes[source.Type]
}

func (source *LoginSource) LDAP() *LDAPConfig {
	return source.Cfg.(*LDAPConfig)
}

// for xorm callback
func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
	if colName == "type" {
		ty := (*val).(int64)
		switch ty {
		case LT_LDAP:
			source.Cfg = new(LDAPConfig)
		}
	}
}

func GetAuths() ([]*LoginSource, error) {
	var auths = make([]*LoginSource, 0)
	err := orm.Find(&auths)
	return auths, err
}

func GetLoginSourceById(id int64) (*LoginSource, error) {
	source := new(LoginSource)
	has, err := orm.Id(id).Get(source)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, ErrAuthenticationNotExist
	}
	return source, nil
}

func AddLDAPSource(name string, cfg *LDAPConfig) error {
	_, err := orm.Insert(&LoginSource{Type: LT_LDAP,
		Name:      name,
		IsActived: true,
		Cfg:       cfg,
	})
	return err
}

func UpdateLDAPSource(source *LoginSource) error {
	_, err := orm.AllCols().Id(source.Id).Update(source)
	return err
}

func DelLoginSource(source *LoginSource) error {
	cnt, err := orm.Count(&User{LoginSource: source.Id})
	if err != nil {
		return err
	}
	if cnt > 0 {
		return ErrAuthenticationUserUsed
	}
	_, err = orm.Id(source.Id).Delete(&LoginSource{})
	return err
}