Add support to ban users from creating sessions
This commit is contained in:
@@ -84,7 +84,7 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
|
|||||||
http.Redirect(rw, req, "/ooc", http.StatusFound)
|
http.Redirect(rw, req, "/ooc", http.StatusFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Println(err)
|
log.Printf("%#v \n", err)
|
||||||
http.Redirect(rw, req, "/500", http.StatusInternalServerError)
|
http.Redirect(rw, req, "/500", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
//TODO: Return some error code
|
//TODO: Return some error code
|
||||||
|
|||||||
@@ -47,6 +47,10 @@ type SessionSetupInstanceConf struct {
|
|||||||
func (p *pwd) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) {
|
func (p *pwd) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) {
|
||||||
defer observeAction("SessionNew", time.Now())
|
defer observeAction("SessionNew", time.Now())
|
||||||
|
|
||||||
|
if u, _ := p.storage.UserGet(config.UserId); u.IsBanned {
|
||||||
|
return nil, fmt.Errorf("User %s is banned\n", config.UserId)
|
||||||
|
}
|
||||||
|
|
||||||
s := &types.Session{}
|
s := &types.Session{}
|
||||||
s.Id = p.generator.NewId()
|
s.Id = p.generator.NewId()
|
||||||
s.CreatedAt = time.Now()
|
s.CreatedAt = time.Now()
|
||||||
|
|||||||
@@ -77,6 +77,43 @@ func TestSessionNew(t *testing.T) {
|
|||||||
_e.M.AssertExpectations(t)
|
_e.M.AssertExpectations(t)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSessionFailWhenUserIsBanned(t *testing.T) {
|
||||||
|
config.PWDContainerName = "pwd"
|
||||||
|
|
||||||
|
_d := &docker.Mock{}
|
||||||
|
_f := &docker.FactoryMock{}
|
||||||
|
_s := &storage.Mock{}
|
||||||
|
_g := &id.MockGenerator{}
|
||||||
|
_e := &event.Mock{}
|
||||||
|
|
||||||
|
ipf := provisioner.NewInstanceProvisionerFactory(provisioner.NewWindowsASG(_f, _s), provisioner.NewDinD(_g, _f, _s))
|
||||||
|
sp := provisioner.NewOverlaySessionProvisioner(_f)
|
||||||
|
|
||||||
|
_g.On("NewId").Return("aaaabbbbcccc")
|
||||||
|
_f.On("GetForSession", mock.AnythingOfType("*types.Session")).Return(_d, nil)
|
||||||
|
_d.On("NetworkCreate", "aaaabbbbcccc", dtypes.NetworkCreate{Attachable: true, Driver: "overlay"}).Return(nil)
|
||||||
|
_d.On("DaemonHost").Return("localhost")
|
||||||
|
_d.On("NetworkConnect", config.L2ContainerName, "aaaabbbbcccc", "").Return("10.0.0.1", nil)
|
||||||
|
_s.On("SessionPut", mock.AnythingOfType("*types.Session")).Return(nil)
|
||||||
|
_s.On("UserGet", mock.Anything).Return(&types.User{IsBanned: true}, nil)
|
||||||
|
_s.On("SessionCount").Return(1, nil)
|
||||||
|
_s.On("InstanceCount").Return(0, nil)
|
||||||
|
_s.On("ClientCount").Return(0, nil)
|
||||||
|
|
||||||
|
var nilArgs []interface{}
|
||||||
|
_e.M.On("Emit", event.SESSION_NEW, "aaaabbbbcccc", nilArgs).Return()
|
||||||
|
|
||||||
|
p := NewPWD(_f, _e, _s, sp, ipf)
|
||||||
|
p.generator = _g
|
||||||
|
|
||||||
|
playground := &types.Playground{Id: "foobar"}
|
||||||
|
sConfig := types.SessionConfig{Playground: playground, UserId: "", Duration: time.Hour, Stack: "", StackName: "", ImageName: ""}
|
||||||
|
s, e := p.SessionNew(context.Background(), sConfig)
|
||||||
|
assert.NotNil(t, e)
|
||||||
|
assert.Nil(t, s)
|
||||||
|
assert.Contains(t, e.Error(), "banned")
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
************************** Not sure how to test this as it can pick any manager as the first node in the swarm cluster.
|
************************** Not sure how to test this as it can pick any manager as the first node in the swarm cluster.
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ type User struct {
|
|||||||
Avatar string `json:"avatar" bson:"avatar"`
|
Avatar string `json:"avatar" bson:"avatar"`
|
||||||
Provider string `json:"provider" bson:"provider"`
|
Provider string `json:"provider" bson:"provider"`
|
||||||
Email string `json:"email" bson:"email"`
|
Email string `json:"email" bson:"email"`
|
||||||
|
IsBanned bool `json:"banned" bson:"banned"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type LoginRequest struct {
|
type LoginRequest struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user