On the multi-tenant edition anyone can set up a team: their account, the company and its website, whose registered domain the team works on (the profile keeps to it). A person can belong to several teams and switch between them from the sidebar, or set up another. Inviting someone who has an account adds them to the team. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
292 lines
8.7 KiB
Go
292 lines
8.7 KiB
Go
package web
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"golang.org/x/net/publicsuffix"
|
|
|
|
"supermarket/internal/appmail"
|
|
"supermarket/internal/audit"
|
|
"supermarket/internal/profile"
|
|
"supermarket/internal/users"
|
|
)
|
|
|
|
// Teams on the multi-tenant edition: signing up makes one, a person can
|
|
// belong to several and switch between them, and each team works on one
|
|
// website domain (a registered domain, with its subdomains).
|
|
|
|
// registeredDomain is a website's registered domain: www.shop.example.co.uk
|
|
// is example.co.uk.
|
|
func registeredDomain(site string) (string, error) {
|
|
site = strings.TrimSpace(site)
|
|
if !strings.Contains(site, "://") {
|
|
site = "https://" + site
|
|
}
|
|
u, err := url.Parse(site)
|
|
if err != nil || u.Hostname() == "" {
|
|
return "", fmt.Errorf("%q isn't a website's address", site)
|
|
}
|
|
host := strings.ToLower(strings.TrimSuffix(u.Hostname(), "."))
|
|
d, err := publicsuffix.EffectiveTLDPlusOne(host)
|
|
if err != nil {
|
|
return "", fmt.Errorf("%q isn't a website's address on the internet", host)
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
// onDomain says whether a website is the domain or one of its subdomains.
|
|
func onDomain(site, domain string) bool {
|
|
d, err := registeredDomain(site)
|
|
return err == nil && d == domain
|
|
}
|
|
|
|
// teamDomain is the team's website domain; "" when it has none (the local
|
|
// and server editions).
|
|
func (s *Server) teamDomain() string {
|
|
if !s.multi() {
|
|
return ""
|
|
}
|
|
t, err := users.GetTeam(s.platform(), s.team())
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
return t.Domain
|
|
}
|
|
|
|
// checkWebsites says what's wrong with a profile's websites for the team:
|
|
// on the multi-tenant edition they must be on its domain.
|
|
func (s *Server) checkWebsites(p *profile.Profile) string {
|
|
dom := s.teamDomain()
|
|
if dom == "" {
|
|
return ""
|
|
}
|
|
for _, w := range p.Websites {
|
|
if w.URL != "" && !onDomain(w.URL, dom) {
|
|
return fmt.Sprintf("%s isn't on %s, this team's domain. A team works on one domain and its subdomains; for another domain, set up another team.", w.URL, dom)
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// Signing up.
|
|
|
|
type signupForm struct {
|
|
Email, Company, Website, Problem string
|
|
}
|
|
|
|
func (s *Server) signupPage(w http.ResponseWriter, r *http.Request) {
|
|
if !s.multi() {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
if v, _ := s.lookupSession(r); v != nil {
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
return
|
|
}
|
|
s.render(w, r, "signup", page{Title: "Sign up", Bare: true, Data: signupForm{}})
|
|
}
|
|
|
|
func (s *Server) signup(w http.ResponseWriter, r *http.Request) {
|
|
if !s.multi() {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
now := time.Now()
|
|
f := signupForm{Email: strings.TrimSpace(r.PostFormValue("email")), Company: strings.TrimSpace(r.PostFormValue("company")),
|
|
Website: strings.TrimSpace(r.PostFormValue("website"))}
|
|
fail := func(msg string, code int) {
|
|
f.Problem = msg
|
|
w.WriteHeader(code)
|
|
s.render(w, r, "signup", page{Title: "Sign up", Bare: true, Data: f})
|
|
}
|
|
ipKey := "signup:ip:" + clientIP(r)
|
|
if s.throttled(ipKey, 5, now) {
|
|
fail("Several teams were just set up from here: wait a quarter of an hour.", http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
email, err := users.NormalEmail(f.Email)
|
|
if err != nil {
|
|
fail("Give your email address: you sign in with it.", http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
pw := r.PostFormValue("password")
|
|
if err := users.CheckNewPassword(pw, email); err != nil {
|
|
fail(sentence(err.Error())+".", http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
if f.Company == "" {
|
|
fail("Give the company's name.", http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
domain, err := registeredDomain(f.Website)
|
|
if err != nil {
|
|
fail("Give the company's website, such as example.com: "+err.Error()+".", http.StatusUnprocessableEntity)
|
|
return
|
|
}
|
|
site := f.Website
|
|
if !strings.Contains(site, "://") {
|
|
site = "https://" + site
|
|
}
|
|
// Someone with an account adds a team from inside, signed in: here,
|
|
// the address must be new.
|
|
if _, err := users.ByEmail(s.platform(), email); err == nil {
|
|
fail("There's an account for that address already: sign in, then add a team from the team menu.", http.StatusConflict)
|
|
return
|
|
} else if !errors.Is(err, users.ErrNotFound) {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
_ = users.Fail(s.DB, ipKey, users.ThrottleWindow, now)
|
|
u := &users.User{Email: email, Name: strings.TrimSpace(r.PostFormValue("name")), Status: users.Active}
|
|
if err := users.Create(s.platform(), u, now); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
if err := users.SetPassword(s.platform(), u, pw); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
team, err := s.newTeam(u, f.Company, domain, site, now)
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
token, _, err := users.StartSessionIn(s.platform(), u.ID, team.ID, clientIP(r), r.UserAgent(), now)
|
|
if err == nil {
|
|
err = users.SignedIn(s.platform(), u.ID, now)
|
|
}
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
s.setSessionCookie(w, token)
|
|
redirect(w, r, "/settings/profile", "team-created")
|
|
}
|
|
|
|
// newTeam makes a team, its schema and its owner, with the company's
|
|
// profile started.
|
|
func (s *Server) newTeam(owner *users.User, company, domain, site string, now time.Time) (*users.TeamInfo, error) {
|
|
team, err := users.CreateTeam(s.platform(), company, domain, now)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := s.Teams.Create(team.ID); err != nil {
|
|
return nil, err
|
|
}
|
|
if err := users.Join(s.platform(), team.ID, owner.ID, users.Owner, nil, now); err != nil {
|
|
return nil, err
|
|
}
|
|
ts, err := s.forTeam(team.ID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if err := profile.Save(ts.DB, &profile.Profile{Name: company, Websites: []profile.Website{{URL: site}}}); err != nil {
|
|
return nil, err
|
|
}
|
|
ts.record(audit.Entry{UserID: owner.ID, UserName: displayName(owner), Action: "team-created", Target: company, Detail: domain})
|
|
s.Log.Info("teams: created", "team", team.ID, "domain", domain, "owner", owner.Email)
|
|
return team, nil
|
|
}
|
|
|
|
// Several teams.
|
|
|
|
// teamsOf are the viewer's teams, for the switcher.
|
|
func (s *Server) teamsOf(v *viewer) []users.TeamInfo {
|
|
if !s.multi() || v == nil || v.User == nil {
|
|
return nil
|
|
}
|
|
list, _ := users.TeamsOf(s.platform(), v.User.ID)
|
|
return list
|
|
}
|
|
|
|
// teamSwitch moves the session to another of the person's teams.
|
|
func (s *Server) teamSwitch(w http.ResponseWriter, r *http.Request) {
|
|
v := viewerOf(r)
|
|
if !s.multi() || v.Session == nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
id, _ := strconv.ParseInt(r.PostFormValue("team"), 10, 64)
|
|
if _, err := users.MembershipOf(s.platform(), id, v.User.ID); err != nil {
|
|
s.forbidden(w, r)
|
|
return
|
|
}
|
|
if t, err := users.GetTeam(s.platform(), id); err != nil || t.Status != users.TeamActive {
|
|
s.forbidden(w, r)
|
|
return
|
|
}
|
|
if err := users.SetSessionTeam(s.platform(), v.Session.ID, id); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
|
|
// teamNewPage and teamNew: a signed-in person sets up another team.
|
|
func (s *Server) teamNewPage(w http.ResponseWriter, r *http.Request) {
|
|
if !s.multi() || viewerOf(r).User == nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
s.render(w, r, "team-new", page{Title: "New team", Data: signupForm{}})
|
|
}
|
|
|
|
func (s *Server) teamNew(w http.ResponseWriter, r *http.Request) {
|
|
v := viewerOf(r)
|
|
if !s.multi() || v.User == nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
now := time.Now()
|
|
f := signupForm{Company: strings.TrimSpace(r.PostFormValue("company")), Website: strings.TrimSpace(r.PostFormValue("website"))}
|
|
domain, err := registeredDomain(f.Website)
|
|
switch {
|
|
case f.Company == "":
|
|
f.Problem = "Give the company's name."
|
|
case err != nil:
|
|
f.Problem = "Give the company's website, such as example.com."
|
|
}
|
|
if f.Problem != "" {
|
|
w.WriteHeader(http.StatusUnprocessableEntity)
|
|
s.render(w, r, "team-new", page{Title: "New team", Data: f})
|
|
return
|
|
}
|
|
site := f.Website
|
|
if !strings.Contains(site, "://") {
|
|
site = "https://" + site
|
|
}
|
|
team, err := s.newTeam(v.User, f.Company, domain, site, now)
|
|
if err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
if err := users.SetSessionTeam(s.platform(), v.Session.ID, team.ID); err != nil {
|
|
s.serverError(w, err)
|
|
return
|
|
}
|
|
redirect(w, r, "/settings/profile", "team-created")
|
|
}
|
|
|
|
// addExisting adds someone who already has an account to the team (they
|
|
// sign in as they do), and tells them.
|
|
func (s *Server) addExisting(by *viewer, u *users.User, role users.Role, mods map[string]users.Access, now time.Time) error {
|
|
if err := users.Join(s.platform(), s.team(), u.ID, role, mods, now); err != nil {
|
|
return err
|
|
}
|
|
team, _ := users.GetTeam(s.platform(), s.team())
|
|
name := "a team"
|
|
if team != nil {
|
|
name = team.Name
|
|
}
|
|
s.sendMail(appmail.Message{To: u.Email, ToName: u.Name, Subject: displayName(by.User) + " added you to " + name + " on Supermarket",
|
|
Body: "Hello" + nameSuffix(u.Name) + ",\n\n" + displayName(by.User) + " added you to " + name + " on Supermarket.\n\n" +
|
|
"Sign in as you do, and choose " + name + " from the team menu:\n" + s.PublicURL + "/signin\n"})
|
|
return nil
|
|
}
|