-
Notifications
You must be signed in to change notification settings - Fork 0
/
session_controller.go
73 lines (58 loc) · 1.95 KB
/
session_controller.go
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
package controllers
import (
"fmt"
"log"
"github.com/mhogar/amber/common"
"github.com/mhogar/amber/models"
"github.com/google/uuid"
)
type CoreSessionController struct {
AuthController AuthController
}
func (c CoreSessionController) CreateSession(CRUD SessionControllerCRUD, username string, password string) (*models.Session, common.CustomError) {
//authenticate the user
user, cerr := c.AuthController.AuthenticateUserWithPassword(CRUD, username, password)
if cerr.Type != common.ErrorTypeNone {
return nil, cerr
}
//create a new session
session := models.CreateNewSession(username, user.Rank)
//save the session
err := CRUD.SaveSession(session)
if err != nil {
log.Println(common.ChainError("error saving session", err))
return nil, common.InternalError()
}
return session, common.NoError()
}
func (c CoreSessionController) DeleteSession(CRUD SessionControllerCRUD, id uuid.UUID) common.CustomError {
//delete the session
res, err := CRUD.DeleteSession(id)
if err != nil {
log.Println(common.ChainError("error deleting session", err))
return common.InternalError()
}
//verify sesion was actually found
if !res {
return common.ClientError(fmt.Sprintf("session with id %s not found", id.String()))
}
return common.NoError()
}
func (c CoreSessionController) DeleteAllUserSessions(CRUD SessionControllerCRUD, username string) common.CustomError {
//delete the sessions
err := CRUD.DeleteAllUserSessions(username)
if err != nil {
log.Println(common.ChainError("error deleting all user sessions", err))
return common.InternalError()
}
return common.NoError()
}
func (c CoreSessionController) DeleteAllOtherUserSessions(CRUD SessionControllerCRUD, username string, id uuid.UUID) common.CustomError {
//delete the sessions
err := CRUD.DeleteAllOtherUserSessions(username, id)
if err != nil {
log.Println(common.ChainError("error deleting all other user sessions", err))
return common.InternalError()
}
return common.NoError()
}