2017-07-24 16:24:21 +01:00
|
|
|
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
|
|
|
|
//
|
|
|
|
// This software (Documize Community Edition) is licensed under
|
|
|
|
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
|
|
|
|
//
|
|
|
|
// You can operate outside the AGPL restrictions by purchasing
|
|
|
|
// Documize Enterprise Edition and obtaining a commercial license
|
|
|
|
// by contacting <sales@documize.com>.
|
|
|
|
//
|
|
|
|
// https://documize.com
|
|
|
|
|
|
|
|
package space
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
"github.com/documize/community/core/env"
|
2017-07-24 16:24:21 +01:00
|
|
|
"github.com/documize/community/core/secrets"
|
|
|
|
"github.com/documize/community/core/uniqueid"
|
|
|
|
"github.com/documize/community/domain"
|
2017-08-02 15:26:31 +01:00
|
|
|
"github.com/documize/community/domain/mail"
|
2018-09-27 15:14:48 +01:00
|
|
|
"github.com/documize/community/domain/store"
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/model/account"
|
2017-09-18 17:53:42 +01:00
|
|
|
"github.com/documize/community/model/permission"
|
2017-07-26 10:50:26 +01:00
|
|
|
"github.com/documize/community/model/space"
|
|
|
|
"github.com/documize/community/model/user"
|
2017-07-24 16:24:21 +01:00
|
|
|
)
|
|
|
|
|
2017-09-13 19:22:38 +01:00
|
|
|
// Invite new user to a space that someone has shared with them.
|
2017-07-24 16:24:21 +01:00
|
|
|
// We create the user account with default values and then take them
|
|
|
|
// through a welcome process designed to capture profile data.
|
2017-09-13 19:22:38 +01:00
|
|
|
// We add them to the organization and grant them view-only space access.
|
2018-09-27 15:14:48 +01:00
|
|
|
func inviteNewUserToSharedSpace(ctx domain.RequestContext, rt *env.Runtime, s *store.Store, email string, invitedBy user.User,
|
2017-07-26 10:50:26 +01:00
|
|
|
baseURL string, sp space.Space, invitationMessage string) (err error) {
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
var u = user.User{}
|
|
|
|
u.Email = email
|
|
|
|
u.Firstname = email
|
|
|
|
u.Lastname = ""
|
|
|
|
u.Salt = secrets.GenerateSalt()
|
|
|
|
requestedPassword := secrets.GenerateRandomPassword()
|
|
|
|
u.Password = secrets.GeneratePassword(requestedPassword, u.Salt)
|
|
|
|
userID := uniqueid.Generate()
|
|
|
|
u.RefID = userID
|
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = s.User.Add(ctx, u)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's give this user access to the organization
|
|
|
|
var a account.Account
|
|
|
|
a.UserID = userID
|
2017-07-26 10:50:26 +01:00
|
|
|
a.OrgID = ctx.OrgID
|
2017-07-24 16:24:21 +01:00
|
|
|
a.Admin = false
|
|
|
|
a.Editor = false
|
2017-09-13 19:22:38 +01:00
|
|
|
a.Users = false
|
2017-07-24 16:24:21 +01:00
|
|
|
a.Active = true
|
2017-09-13 19:22:38 +01:00
|
|
|
a.RefID = uniqueid.Generate()
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-07-26 10:50:26 +01:00
|
|
|
err = s.Account.Add(ctx, a)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-09-18 17:53:42 +01:00
|
|
|
perm := permission.Permission{}
|
2017-09-13 19:22:38 +01:00
|
|
|
perm.OrgID = sp.OrgID
|
2018-03-02 14:47:58 +00:00
|
|
|
perm.Who = permission.UserPermission
|
2017-09-13 19:22:38 +01:00
|
|
|
perm.WhoID = userID
|
2018-03-02 14:47:58 +00:00
|
|
|
perm.Scope = permission.ScopeRow
|
|
|
|
perm.Location = permission.LocationSpace
|
2017-09-13 19:22:38 +01:00
|
|
|
perm.RefID = sp.RefID
|
|
|
|
perm.Action = "" // we send array for actions below
|
2017-07-24 16:24:21 +01:00
|
|
|
|
2017-09-18 17:53:42 +01:00
|
|
|
err = s.Permission.AddPermissions(ctx, perm, permission.SpaceView)
|
2017-07-24 16:24:21 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-08-02 15:26:31 +01:00
|
|
|
mailer := mail.Mailer{Runtime: rt, Store: s, Context: ctx}
|
|
|
|
|
2017-07-24 16:24:21 +01:00
|
|
|
url := fmt.Sprintf("%s/%s", baseURL, u.Salt)
|
2018-05-03 18:03:25 +01:00
|
|
|
go mailer.ShareSpaceNewUser(u.Email, invitedBy.Fullname(), invitedBy.Email, url, sp.Name, invitationMessage)
|
2017-07-24 16:24:21 +01:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|