From ea3342aa6c93579d41de3f6005c12a201b21ee6e Mon Sep 17 00:00:00 2001 From: Christopher Speller Date: Fri, 3 Jun 2016 09:33:59 -0400 Subject: Adding LDAP Syncronization (#3219) --- model/config.go | 12 +++++ model/job.go | 91 ++++++++++++++++++++++++++++++++++++++ model/job_test.go | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 231 insertions(+) create mode 100644 model/job.go create mode 100644 model/job_test.go (limited to 'model') diff --git a/model/config.go b/model/config.go index 08b00b90f..7e810be02 100644 --- a/model/config.go +++ b/model/config.go @@ -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 } diff --git a/model/job.go b/model/job.go new file mode 100644 index 000000000..bcae7a830 --- /dev/null +++ b/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, + ) +} diff --git a/model/job_test.go b/model/job_test.go new file mode 100644 index 000000000..2a307de1e --- /dev/null +++ b/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") + } + } +} -- cgit v1.2.3-1-g7c22