/
permissions.go
54 lines (43 loc) · 1.37 KB
/
permissions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import (
"fmt"
"strings"
"errors"
"github.com/mattermost/mattermost-plugin-ai/server/mmapi"
"github.com/mattermost/mattermost/server/public/model"
)
var ErrUsageRestriction = errors.New("usage restriction")
func (p *Plugin) checkUsageRestrictions(userID string, channel *model.Channel) error {
if err := p.checkUsageRestrictionsForUser(userID); err != nil {
return err
}
if err := p.checkUsageRestrictionsForChannel(channel); err != nil {
return err
}
return nil
}
func (p *Plugin) checkUsageRestrictionsForChannel(channel *model.Channel) error {
cfg := p.getConfiguration()
if cfg.EnableUseRestrictions {
if cfg.AllowedTeamIDs != "" && !strings.Contains(cfg.AllowedTeamIDs, channel.TeamId) {
return fmt.Errorf("can't work on this team: %w", ErrUsageRestriction)
}
if !cfg.AllowPrivateChannels {
if channel.Type != model.ChannelTypeOpen {
if !mmapi.IsDMWith(p.botid, channel) {
return fmt.Errorf("can't work on private channels: %w", ErrUsageRestriction)
}
}
}
}
return nil
}
func (p *Plugin) checkUsageRestrictionsForUser(userID string) error {
cfg := p.getConfiguration()
if cfg.EnableUseRestrictions && cfg.OnlyUsersOnTeam != "" {
if !p.pluginAPI.User.HasPermissionToTeam(userID, cfg.OnlyUsersOnTeam, model.PermissionViewTeam) {
return fmt.Errorf("user not on allowed team: %w", ErrUsageRestriction)
}
}
return nil
}