Skip to content

Commit

Permalink
Add support to ban users from creating sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosnils committed Mar 5, 2019
1 parent 43fe20f commit 5afc852
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion handlers/new_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func NewSession(rw http.ResponseWriter, req *http.Request) {
http.Redirect(rw, req, "/ooc", http.StatusFound)
return
}
log.Println(err)
log.Printf("%#v \n", err)
http.Redirect(rw, req, "/500", http.StatusInternalServerError)
return
//TODO: Return some error code
Expand Down
4 changes: 4 additions & 0 deletions pwd/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ type SessionSetupInstanceConf struct {
func (p *pwd) SessionNew(ctx context.Context, config types.SessionConfig) (*types.Session, error) {
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.Id = p.generator.NewId()
s.CreatedAt = time.Now()
Expand Down
37 changes: 37 additions & 0 deletions pwd/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,43 @@ func TestSessionNew(t *testing.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.
Expand Down
1 change: 1 addition & 0 deletions pwd/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type User struct {
Avatar string `json:"avatar" bson:"avatar"`
Provider string `json:"provider" bson:"provider"`
Email string `json:"email" bson:"email"`
IsBanned bool `json:"banned" bson:"banned"`
}

type LoginRequest struct {
Expand Down

0 comments on commit 5afc852

Please sign in to comment.