Adding LDAP Syncronization (#3219)
Этот коммит содержится в:
коммит произвёл
Joram Wilander
родитель
87f357a54d
Коммит
ea3342aa6c
@@ -190,6 +190,9 @@ type LdapSettings struct {
|
||||
NicknameAttribute *string
|
||||
IdAttribute *string
|
||||
|
||||
// Syncronization
|
||||
SyncIntervalMinutes *int
|
||||
|
||||
// Advanced
|
||||
SkipCertificateVerification *bool
|
||||
QueryTimeout *int
|
||||
@@ -441,6 +444,11 @@ func (o *Config) SetDefaults() {
|
||||
*o.LdapSettings.LoginFieldName = ""
|
||||
}
|
||||
|
||||
if o.LdapSettings.SyncIntervalMinutes == nil {
|
||||
o.LdapSettings.SyncIntervalMinutes = new(int)
|
||||
*o.LdapSettings.SyncIntervalMinutes = 60
|
||||
}
|
||||
|
||||
if o.ServiceSettings.SessionLengthWebInDays == nil {
|
||||
o.ServiceSettings.SessionLengthWebInDays = new(int)
|
||||
*o.ServiceSettings.SessionLengthWebInDays = 30
|
||||
@@ -635,6 +643,10 @@ func (o *Config) IsValid() *AppError {
|
||||
return NewLocAppError("Config.IsValid", "model.config.is_valid.ldap_security.app_error", nil, "")
|
||||
}
|
||||
|
||||
if *o.LdapSettings.SyncIntervalMinutes <= 0 {
|
||||
return NewLocAppError("Config.IsValid", "model.config.is_valid.ldap_sync_interval.app_error", nil, "")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
91
model/job.go
Обычный файл
91
model/job.go
Обычный файл
@@ -0,0 +1,91 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type TaskFunc func()
|
||||
|
||||
type ScheduledTask struct {
|
||||
Name string `json:"name"`
|
||||
Interval time.Duration `json:"interval"`
|
||||
Recurring bool `json:"recurring"`
|
||||
function TaskFunc `json:",omitempty"`
|
||||
timer *time.Timer `json:",omitempty"`
|
||||
}
|
||||
|
||||
var tasks = make(map[string]*ScheduledTask)
|
||||
|
||||
func addTask(task *ScheduledTask) {
|
||||
tasks[task.Name] = task
|
||||
}
|
||||
|
||||
func removeTaskByName(name string) {
|
||||
delete(tasks, name)
|
||||
}
|
||||
|
||||
func getTaskByName(name string) *ScheduledTask {
|
||||
return tasks[name]
|
||||
}
|
||||
|
||||
func GetAllTasks() *map[string]*ScheduledTask {
|
||||
return &tasks
|
||||
}
|
||||
|
||||
func CreateTask(name string, function TaskFunc, timeToExecution time.Duration) *ScheduledTask {
|
||||
task := &ScheduledTask{
|
||||
Name: name,
|
||||
Interval: timeToExecution,
|
||||
Recurring: false,
|
||||
function: function,
|
||||
}
|
||||
|
||||
taskRunner := func() {
|
||||
go task.function()
|
||||
removeTaskByName(task.Name)
|
||||
}
|
||||
|
||||
task.timer = time.AfterFunc(timeToExecution, taskRunner)
|
||||
|
||||
addTask(task)
|
||||
|
||||
return task
|
||||
}
|
||||
|
||||
func CreateRecurringTask(name string, function TaskFunc, interval time.Duration) *ScheduledTask {
|
||||
task := &ScheduledTask{
|
||||
Name: name,
|
||||
Interval: interval,
|
||||
Recurring: true,
|
||||
function: function,
|
||||
}
|
||||
|
||||
taskRecurer := func() {
|
||||
go task.function()
|
||||
task.timer.Reset(task.Interval)
|
||||
}
|
||||
|
||||
task.timer = time.AfterFunc(interval, taskRecurer)
|
||||
|
||||
addTask(task)
|
||||
|
||||
return task
|
||||
}
|
||||
|
||||
func (task *ScheduledTask) Cancel() {
|
||||
task.timer.Stop()
|
||||
removeTaskByName(task.Name)
|
||||
}
|
||||
|
||||
func (task *ScheduledTask) String() string {
|
||||
return fmt.Sprintf(
|
||||
"%s\nInterval: %s\nRecurring: %t\n",
|
||||
task.Name,
|
||||
task.Interval.String(),
|
||||
task.Recurring,
|
||||
)
|
||||
}
|
||||
128
model/job_test.go
Обычный файл
128
model/job_test.go
Обычный файл
@@ -0,0 +1,128 @@
|
||||
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
|
||||
// See License.txt for license information.
|
||||
|
||||
package model
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCreateTask(t *testing.T) {
|
||||
TASK_NAME := "Test Task"
|
||||
TASK_TIME := time.Second * 3
|
||||
|
||||
testValue := 0
|
||||
testFunc := func() {
|
||||
testValue = 1
|
||||
}
|
||||
|
||||
task := CreateTask(TASK_NAME, testFunc, TASK_TIME)
|
||||
if testValue != 0 {
|
||||
t.Fatal("Unexpected execuition of task")
|
||||
}
|
||||
|
||||
time.Sleep(TASK_TIME + time.Second)
|
||||
|
||||
if testValue != 1 {
|
||||
t.Fatal("Task did not execute")
|
||||
}
|
||||
|
||||
if task.Name != TASK_NAME {
|
||||
t.Fatal("Bad name")
|
||||
}
|
||||
|
||||
if task.Interval != TASK_TIME {
|
||||
t.Fatal("Bad interval")
|
||||
}
|
||||
|
||||
if task.Recurring != false {
|
||||
t.Fatal("should not reccur")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRecurringTask(t *testing.T) {
|
||||
TASK_NAME := "Test Recurring Task"
|
||||
TASK_TIME := time.Second * 3
|
||||
|
||||
testValue := 0
|
||||
testFunc := func() {
|
||||
testValue += 1
|
||||
}
|
||||
|
||||
task := CreateRecurringTask(TASK_NAME, testFunc, TASK_TIME)
|
||||
if testValue != 0 {
|
||||
t.Fatal("Unexpected execuition of task")
|
||||
}
|
||||
|
||||
time.Sleep(TASK_TIME + time.Second)
|
||||
|
||||
if testValue != 1 {
|
||||
t.Fatal("Task did not execute")
|
||||
}
|
||||
|
||||
time.Sleep(TASK_TIME)
|
||||
|
||||
if testValue != 2 {
|
||||
t.Fatal("Task did not re-execute")
|
||||
}
|
||||
|
||||
if task.Name != TASK_NAME {
|
||||
t.Fatal("Bad name")
|
||||
}
|
||||
|
||||
if task.Interval != TASK_TIME {
|
||||
t.Fatal("Bad interval")
|
||||
}
|
||||
|
||||
if task.Recurring != true {
|
||||
t.Fatal("should reccur")
|
||||
}
|
||||
|
||||
task.Cancel()
|
||||
}
|
||||
|
||||
func TestCancelTask(t *testing.T) {
|
||||
TASK_NAME := "Test Task"
|
||||
TASK_TIME := time.Second * 3
|
||||
|
||||
testValue := 0
|
||||
testFunc := func() {
|
||||
testValue = 1
|
||||
}
|
||||
|
||||
task := CreateTask(TASK_NAME, testFunc, TASK_TIME)
|
||||
if testValue != 0 {
|
||||
t.Fatal("Unexpected execuition of task")
|
||||
}
|
||||
task.Cancel()
|
||||
|
||||
time.Sleep(TASK_TIME + time.Second)
|
||||
|
||||
if testValue != 0 {
|
||||
t.Fatal("Unexpected execuition of task")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetAllTasks(t *testing.T) {
|
||||
doNothing := func() {}
|
||||
|
||||
CreateTask("Task1", doNothing, time.Hour)
|
||||
CreateTask("Task2", doNothing, time.Second)
|
||||
CreateRecurringTask("Task3", doNothing, time.Second)
|
||||
task4 := CreateRecurringTask("Task4", doNothing, time.Second)
|
||||
|
||||
task4.Cancel()
|
||||
|
||||
time.Sleep(time.Second * 3)
|
||||
|
||||
tasks := *GetAllTasks()
|
||||
if len(tasks) != 2 {
|
||||
t.Fatal("Wrong number of tasks got: ", len(tasks))
|
||||
}
|
||||
for _, task := range tasks {
|
||||
if task.Name != "Task1" && task.Name != "Task3" {
|
||||
t.Fatal("Wrong tasks")
|
||||
}
|
||||
}
|
||||
}
|
||||
Ссылка в новой задаче
Block a user