Skip to content

Commit

Permalink
Merge 8767633 into 49df909
Browse files Browse the repository at this point in the history
  • Loading branch information
acsauk authored Feb 8, 2024
2 parents 49df909 + 8767633 commit c7052e9
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 57 deletions.
59 changes: 59 additions & 0 deletions internal/actor/enum_permission.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 14 additions & 3 deletions internal/actor/organisation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package actor

import "time"
import (
"time"
)

const memberInviteExpireAfter = time.Hour * 48

Expand All @@ -24,7 +26,12 @@ type Member struct {
// CreatedAt is when the Member was created
CreatedAt time.Time
// UpdatedAt is when the Member was last updated
UpdatedAt time.Time
UpdatedAt time.Time
FirstNames string
LastName string
Email string
// Permission is the type of permissions assigned to the member to set available actions in an Organisation
Permission Permission
}

// A MemberInvite is created to allow a new Member to join an Organisation
Expand All @@ -35,7 +42,11 @@ type MemberInvite struct {
// OrganisationID identifies the organisation the invite is for
OrganisationID string
// Email is the address the new Member must signin as for the invite
Email string
Email string
FirstNames string
LastName string
// Permission is the type of permissions assigned to the member to set available actions in an Organisation
Permission Permission
}

func (i MemberInvite) HasExpired() bool {
Expand Down
9 changes: 9 additions & 0 deletions internal/actor/permission.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package actor

//go:generate enumerator -type Permission -linecomment
type Permission uint8

const (
None Permission = iota //none
Admin // admin
)
6 changes: 4 additions & 2 deletions internal/app/organisation_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ func (s *organisationStore) Create(ctx context.Context, name string) (*actor.Org

return organisation, nil
}

func (s *organisationStore) Get(ctx context.Context) (*actor.Organisation, error) {
data, err := page.SessionDataFromContext(ctx)
if err != nil {
Expand Down Expand Up @@ -82,13 +81,16 @@ func (s *organisationStore) Put(ctx context.Context, organisation *actor.Organis
return s.dynamoClient.Put(ctx, organisation)
}

func (s *organisationStore) CreateMemberInvite(ctx context.Context, organisation *actor.Organisation, email, code string) error {
func (s *organisationStore) CreateMemberInvite(ctx context.Context, organisation *actor.Organisation, firstNames, lastname, email, code string, permission actor.Permission) error {
invite := &actor.MemberInvite{
PK: memberInviteKey(code),
SK: memberInviteKey(code),
CreatedAt: s.now(),
OrganisationID: organisation.ID,
Email: email,
FirstNames: firstNames,
LastName: lastname,
Permission: permission,
}

if err := s.dynamoClient.Create(ctx, invite); err != nil {
Expand Down
7 changes: 5 additions & 2 deletions internal/app/organisation_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,15 @@ func TestOrganisationStoreCreateMemberInvite(t *testing.T) {
CreatedAt: testNow,
OrganisationID: "a-uuid",
Email: "[email protected]",
FirstNames: "a",
LastName: "b",
Permission: actor.None,
}).
Return(nil)

organisationStore := &organisationStore{dynamoClient: dynamoClient, now: testNowFn}

err := organisationStore.CreateMemberInvite(ctx, &actor.Organisation{ID: "a-uuid"}, "[email protected]", "abcde")
err := organisationStore.CreateMemberInvite(ctx, &actor.Organisation{ID: "a-uuid"}, "a", "b", "[email protected]", "abcde", actor.None)
assert.Nil(t, err)
}

Expand All @@ -217,7 +220,7 @@ func TestOrganisationStoreCreateMemberInviteWhenErrors(t *testing.T) {

organisationStore := &organisationStore{dynamoClient: dynamoClient, now: testNowFn}

err := organisationStore.CreateMemberInvite(ctx, &actor.Organisation{}, "[email protected]", "abcde")
err := organisationStore.CreateMemberInvite(ctx, &actor.Organisation{}, "a", "b", "[email protected]", "abcde", actor.None)
assert.ErrorIs(t, err, expectedError)
}

Expand Down
2 changes: 0 additions & 2 deletions internal/page/donor/sign_your_lpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ func SignYourLpa(tmpl template.Template, donorStore DonorStore) Handler {
}

if r.Method == http.MethodPost {
r.ParseForm()

data.Form = readSignYourLpaForm(r)
data.Errors = data.Form.Validate()

Expand Down
47 changes: 38 additions & 9 deletions internal/page/supporter/invite_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import (
)

type inviteMemberData struct {
App page.AppData
Errors validation.List
Form *inviteMemberForm
App page.AppData
Errors validation.List
Form *inviteMemberForm
Options actor.PermissionOptions
}

func InviteMember(tmpl template.Template, organisationStore OrganisationStore, notifyClient NotifyClient, randomString func(int) string, appPublicURL string) Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request, organisation *actor.Organisation) error {
data := &inviteMemberData{
App: appData,
Form: &inviteMemberForm{},
App: appData,
Form: &inviteMemberForm{},
Options: actor.PermissionValues,
}

if r.Method == http.MethodPost {
Expand All @@ -35,7 +37,15 @@ func InviteMember(tmpl template.Template, organisationStore OrganisationStore, n
}

inviteCode := randomString(12)
if err := organisationStore.CreateMemberInvite(r.Context(), organisation, data.Form.Email, inviteCode); err != nil {
if err := organisationStore.CreateMemberInvite(
r.Context(),
organisation,
data.Form.FirstNames,
data.Form.LastName,
data.Form.Email,
inviteCode,
data.Form.Permission,
); err != nil {
return err
}

Expand All @@ -57,21 +67,40 @@ func InviteMember(tmpl template.Template, organisationStore OrganisationStore, n
}

type inviteMemberForm struct {
Email string
FirstNames string
LastName string
Email string
Permission actor.Permission
}

func readInviteMemberForm(r *http.Request) *inviteMemberForm {
return &inviteMemberForm{
Email: page.PostFormString(r, "email"),
form := &inviteMemberForm{
Email: page.PostFormString(r, "email"),
FirstNames: page.PostFormString(r, "first-names"),
LastName: page.PostFormString(r, "last-name"),
}

form.Permission, _ = actor.ParsePermission(page.PostFormString(r, "permission"))

return form
}

func (f *inviteMemberForm) Validate() validation.List {
var errors validation.List

errors.String("first-names", "firstNames", f.FirstNames,
validation.Empty(),
validation.StringTooLong(53))

errors.String("last-name", "lastName", f.LastName,
validation.Empty(),
validation.StringTooLong(61))

errors.String("email", "email", f.Email,
validation.Empty(),
validation.Email())

errors.Options("permission", "makeThisPersonAnAdmin", []string{f.Permission.String()}, validation.Select(actor.None.String(), actor.Admin.String()))

return errors
}
Loading

0 comments on commit c7052e9

Please sign in to comment.