Skip to content

Commit 932d495

Browse files
committed
Add key to organization invite
1 parent 044e1b8 commit 932d495

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

internal/email/templates/organization_invitation.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const organizationInvitationTemplate = `
6868
<td>
6969
<a
7070
style="color: initial;"
71-
href="https://joinimpact.org/dashboard/user/organizations/{{organizationID}}/invites/{{inviteID}}"
71+
href="https://joinimpact.org/dashboard/user/organizations/{{organizationID}}/invites/{{inviteID}}?key={{key}}"
7272
>Click here to join {{organizationName}}</a
7373
>
7474
</td>
@@ -96,14 +96,15 @@ const organizationInvitationTemplate = `
9696

9797
// OrganizationInvitationTemplate generates and returns a reset password email with the
9898
// provied name, email and key.
99-
func OrganizationInvitationTemplate(name, organizationName string, organizationID, inviteID int64) string {
99+
func OrganizationInvitationTemplate(name, organizationName string, organizationID, inviteID int64, key string) string {
100100
template := organizationInvitationTemplate
101101

102102
// Replace the template variables with the provided params.
103103
template = strings.Replace(template, `{{name}}`, name, -1)
104104
template = strings.Replace(template, `{{organizationName}}`, organizationName, -1)
105105
template = strings.Replace(template, `{{organizationID}}`, fmt.Sprintf("%d", organizationID), -1)
106106
template = strings.Replace(template, `{{inviteID}}`, fmt.Sprintf("%d", inviteID), -1)
107+
template = strings.Replace(template, `{{key}}`, fmt.Sprintf("%s", key), -1)
107108

108109
// Return the HTML string.
109110
return template

internal/models/organization_membership_invite.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ type OrganizationMembershipInvite struct {
99
Invitee User `gorm:"foreignkey:InviteeID"`
1010
OrganizationID int64 `json:"organizationId"`
1111
Organization Organization
12-
InviterID int64 `json:"inviterId"`
13-
Inviter User `gorm:"foreignkey:InviterID"`
12+
InviterID int64 `json:"inviterId"`
13+
Inviter User `gorm:"foreignkey:InviterID"`
14+
Key string `json:"-"` // the secret key which is sent to the invitee for authorization
1415
}
1516

1617
// OrganizationMembershipInviteRepository represents the interface for a repository of membership invite entities.

internal/organizations/rand.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package organizations
2+
3+
import (
4+
"math/rand"
5+
"time"
6+
)
7+
8+
const charset = "abcdefghijklmnopqrstuvwxyz" +
9+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
10+
11+
var seededRand *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
12+
13+
// stringWithCharset generates a random string using a charset.
14+
func stringWithCharset(length int, charset string) string {
15+
b := make([]byte, length)
16+
for i := range b {
17+
b[i] = charset[seededRand.Intn(len(charset))]
18+
}
19+
return string(b)
20+
}
21+
22+
// generateKey generates and returns a random string to use as a key.
23+
func generateKey() string {
24+
return stringWithCharset(24, charset)
25+
}

internal/organizations/service.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ func (s *service) inviteByEmail(inviterID int64, organization *models.Organizati
394394
InviteeEmail: userEmail,
395395
OrganizationID: organization.ID,
396396
InviterID: inviterID,
397+
Key: generateKey(),
397398
})
398399
if err != nil {
399400
return NewErrServerError()
@@ -430,6 +431,7 @@ func (s *service) inviteByID(inviterID int64, organization *models.Organization,
430431
InviteeID: user.ID,
431432
OrganizationID: organization.ID,
432433
InviterID: inviterID,
434+
Key: generateKey(),
433435
})
434436
if err != nil {
435437
return NewErrServerError()

0 commit comments

Comments
 (0)