aboutsummaryrefslogtreecommitdiff
path: root/test/handler.go
blob: 7b3e480efffdafb8a2f5a587d6ef4e8d660a9664 (plain) (blame)
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
123
124
125
126
127
128
package main

import (
	"fmt"
	"math/rand"
	"os"

	ldap "github.com/go-ldap/ldap/v3"
	"github.com/sirupsen/logrus"
)

const maxlength_generateName, minlength_generateName = 25, 3

const bindusername = "cn=admin,dc=deuxfleurs,dc=fr"
const adresse = "127.0.0.1"
const port = 1389

var logging = logrus.New()

const seed = 654258

var R = rand.New(rand.NewSource(seed))

var bindpassword = "sf7yO52NCuE"

//Handler just to facilite the print error
func PrintError(LDAPError error) {
	if LDAPError != nil {
		logging.Fatal(LDAPError)
	}
}

//Generate an unique name, which store in all_names
func (inst *instance) GenerateName() (name string) {
	alphabet := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
		"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y",
		"Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "é", "è", "ê", "ë", "à", "@", "â", "ä", "û", "ü", "ù", "$", "£", "%", "ø", "€"}
	length := R.Intn(maxlength_generateName) + minlength_generateName

	//Check if this name not exist already
	//Lock thhis variable because she is hared with other goroutine
	allNames.mu.Lock()
	for only_one := true; only_one; _, only_one = allNames.cn[name] {
		//Create the name
		for i := 0; i < length; i++ {
			name += alphabet[R.Intn(len(alphabet))]
		}

	}
	//Add the new name in the map to store this one
	allNames.cn[name] = struct{}{}
	allNames.mu.Unlock()
	logging.Debug(fmt.Sprintf("Name generated: %s.", name))
	return
}

//Transform attributes in map format to the struct attributes
func MapAttToStruct(att map[string][]string) []attributes {
	resultat := []attributes{}
	for key, value := range att {
		logging.Debug(fmt.Sprintf("Transform: key: %s, values: %s to attributes struct.\n", key, value))
		resultat = append(resultat, attributes{
			Name: key,
			Data: value,
		})
	}
	return resultat
}

func Connect() (*ldap.Conn, error) {
	l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", adresse, port))
	if err != nil {
		return nil, err
	}

	if key, ok := os.LookupEnv("BOTTIN_DEFAULT_ADMIN_PW"); ok {
		bindpassword = key
	}
	//l.Debug.Enable(true)
	err = l.Bind(bindusername, bindpassword)
	logging.Debug("Connection succesful")
	return l, err
}

//Handler to get only attributes names
func getNamesAtt(dat data_DN) []string {
	resultat := []string{}
	for _, values := range dat.Attributes {
		resultat = append(resultat, values.Name)
	}
	return resultat
}

//Handler to compare slice string
func CompareSliceString(string1, string2 []string) bool {
	if len(string1) != len(string2) {
		return false
	} else {
		for index := range string1 {
			if string1[index] != string2[index] {
				return false
			}
		}
	}
	return true
}

//Handler to remove an element in slice string
func DeleteElementSliceString(s string, sSlice []string) []string {

	for index, value := range sSlice {
		if value == s {
			sSlice[index] = sSlice[len(sSlice)-1]
			return sSlice[:len(sSlice)-1]
		}
	}
	return sSlice
}

//Get attributes entry values bottin bug
func GetAttributeValuesBottin(ent *ldap.Entry, name string) (res []string) {
	for _, val := range ent.Attributes {
		if val.Name == name {
			res = append(res, val.Values...)
		}
	}
	return
}