2017-05-03 08:02:29 +02:00
|
|
|
// Package with supporting functionality to run the microservice
|
2017-04-07 11:56:28 +02:00
|
|
|
package runtime
|
2017-04-05 19:03:44 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/genofire/hs_master-kss-monolith/lib/log"
|
2017-05-12 11:50:51 +02:00
|
|
|
"sync"
|
2017-04-05 19:03:44 +02:00
|
|
|
)
|
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// URL to the microservice which manages permissions
|
2017-04-07 13:13:37 +02:00
|
|
|
var PermissionURL string
|
2017-04-05 19:03:44 +02:00
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// Type of permission
|
2017-04-05 19:03:44 +02:00
|
|
|
type Permission int
|
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// Some permissions (the real permissions need to come from the permission microservice)
|
2017-04-05 19:03:44 +02:00
|
|
|
const (
|
2017-05-03 08:02:29 +02:00
|
|
|
// permission to add goods to the stock
|
|
|
|
// e.g. if a good is received and now available to sell
|
2017-04-05 19:03:44 +02:00
|
|
|
PermissionCreateGood = 1
|
2017-05-03 08:02:29 +02:00
|
|
|
|
|
|
|
// permission to delete goods from the stock
|
2017-05-15 10:22:24 +02:00
|
|
|
// e.g. if a good becomes fouled and has to be removed
|
2017-04-05 19:03:44 +02:00
|
|
|
PermissionDeleteGood = 2
|
|
|
|
)
|
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// Struct that holds the information for a permission cache
|
2017-04-05 19:03:44 +02:00
|
|
|
type permissionMicroServiceCache struct {
|
|
|
|
LastCheck time.Time
|
|
|
|
session string
|
|
|
|
permissions map[Permission]boolMicroServiceCache
|
2017-05-12 11:50:51 +02:00
|
|
|
sync.Mutex
|
2017-04-05 19:03:44 +02:00
|
|
|
}
|
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// Function to check, if a user has a permission
|
2017-04-05 19:03:44 +02:00
|
|
|
func (c *permissionMicroServiceCache) HasPermission(p Permission) (bool, error) {
|
|
|
|
c.LastCheck = time.Now()
|
2017-05-12 11:50:51 +02:00
|
|
|
c.Lock()
|
|
|
|
defer c.Unlock()
|
2017-04-05 19:03:44 +02:00
|
|
|
if cache, ok := c.permissions[p]; ok {
|
2017-04-05 20:23:29 +02:00
|
|
|
before := time.Now().Add(-CacheConfig.After.Duration)
|
|
|
|
if before.After(cache.LastCheck) {
|
2017-04-05 19:03:44 +02:00
|
|
|
return cache.Value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf(PermissionURL, c.session, p)
|
|
|
|
log.Log.WithField("url", url).Info("has permission?")
|
2017-04-29 18:26:36 +02:00
|
|
|
|
2017-04-05 19:03:44 +02:00
|
|
|
res, err := http.Get(url)
|
|
|
|
|
2017-04-29 18:26:36 +02:00
|
|
|
value := false
|
|
|
|
if err == nil {
|
|
|
|
value = (res.StatusCode == http.StatusOK)
|
|
|
|
}
|
|
|
|
|
2017-04-05 19:03:44 +02:00
|
|
|
c.permissions[p] = boolMicroServiceCache{
|
|
|
|
LastCheck: c.LastCheck,
|
2017-04-29 18:26:36 +02:00
|
|
|
Value: value,
|
2017-04-05 19:03:44 +02:00
|
|
|
}
|
|
|
|
return c.permissions[p].Value, err
|
|
|
|
}
|
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// Cache for permissions
|
2017-04-05 19:03:44 +02:00
|
|
|
var permissionCache map[string]*permissionMicroServiceCache
|
2017-05-12 11:50:51 +02:00
|
|
|
var permissionMutex sync.Mutex
|
2017-05-15 10:22:24 +02:00
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// Function to initialize the permission cache
|
2017-04-05 19:03:44 +02:00
|
|
|
func init() {
|
|
|
|
permissionCache = make(map[string]*permissionMicroServiceCache)
|
|
|
|
}
|
|
|
|
|
2017-05-03 08:02:29 +02:00
|
|
|
// Function to check, if the current session has any permissions
|
2017-04-28 12:05:58 +02:00
|
|
|
func HasPermission(session string, p int) (bool, error) {
|
2017-05-12 11:50:51 +02:00
|
|
|
permissionMutex.Lock()
|
|
|
|
defer permissionMutex.Unlock()
|
2017-04-05 19:03:44 +02:00
|
|
|
_, ok := permissionCache[session]
|
|
|
|
if !ok {
|
|
|
|
permissionCache[session] = &permissionMicroServiceCache{
|
|
|
|
LastCheck: time.Now(),
|
|
|
|
session: session,
|
|
|
|
permissions: make(map[Permission]boolMicroServiceCache),
|
|
|
|
}
|
|
|
|
}
|
2017-04-28 12:05:58 +02:00
|
|
|
return permissionCache[session].HasPermission(Permission(p))
|
2017-04-05 19:03:44 +02:00
|
|
|
}
|