| 1 | package slack |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | ) |
| 9 | |
| 10 | func (api *Client) adminRequest(ctx context.Context, method string, teamName string, values url.Values) error { |
| 11 | resp := &SlackResponse{} |
| 12 | err := parseAdminResponse(ctx, api.httpclient, method, teamName, values, resp, api) |
| 13 | if err != nil { |
| 14 | return err |
| 15 | } |
| 16 | |
| 17 | return resp.Err() |
| 18 | } |
| 19 | |
| 20 | // DisableUser disabled a user account, given a user ID |
| 21 | func (api *Client) DisableUser(teamName string, uid string) error { |
| 22 | return api.DisableUserContext(context.Background(), teamName, uid) |
| 23 | } |
| 24 | |
| 25 | // DisableUserContext disabled a user account, given a user ID with a custom context |
| 26 | func (api *Client) DisableUserContext(ctx context.Context, teamName string, uid string) error { |
| 27 | values := url.Values{ |
| 28 | "user": {uid}, |
| 29 | "token": {api.token}, |
| 30 | "set_active": {"true"}, |
| 31 | "_attempts": {"1"}, |
| 32 | } |
| 33 | |
| 34 | if err := api.adminRequest(ctx, "setInactive", teamName, values); err != nil { |
| 35 | return fmt.Errorf("failed to disable user with id '%s': %s", uid, err) |
| 36 | } |
| 37 | |
| 38 | return nil |
| 39 | } |
| 40 | |
| 41 | // InviteGuest invites a user to Slack as a single-channel guest |
| 42 | func (api *Client) InviteGuest(teamName, channel, firstName, lastName, emailAddress string) error { |
| 43 | return api.InviteGuestContext(context.Background(), teamName, channel, firstName, lastName, emailAddress) |
| 44 | } |
| 45 | |
| 46 | // InviteGuestContext invites a user to Slack as a single-channel guest with a custom context |
| 47 | func (api *Client) InviteGuestContext(ctx context.Context, teamName, channel, firstName, lastName, emailAddress string) error { |
| 48 | values := url.Values{ |
| 49 | "email": {emailAddress}, |
| 50 | "channels": {channel}, |
| 51 | "first_name": {firstName}, |
| 52 | "last_name": {lastName}, |
| 53 | "ultra_restricted": {"1"}, |
| 54 | "token": {api.token}, |
| 55 | "resend": {"true"}, |
| 56 | "set_active": {"true"}, |
| 57 | "_attempts": {"1"}, |
| 58 | } |
| 59 | |
| 60 | err := api.adminRequest(ctx, "invite", teamName, values) |
| 61 | if err != nil { |
| 62 | return fmt.Errorf("Failed to invite single-channel guest: %s", err) |
| 63 | } |
| 64 | |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | // InviteRestricted invites a user to Slack as a restricted account |
| 69 | func (api *Client) InviteRestricted(teamName, channel, firstName, lastName, emailAddress string) error { |
| 70 | return api.InviteRestrictedContext(context.Background(), teamName, channel, firstName, lastName, emailAddress) |
| 71 | } |
| 72 | |
| 73 | // InviteRestrictedContext invites a user to Slack as a restricted account with a custom context |
| 74 | func (api *Client) InviteRestrictedContext(ctx context.Context, teamName, channel, firstName, lastName, emailAddress string) error { |
| 75 | values := url.Values{ |
| 76 | "email": {emailAddress}, |
| 77 | "channels": {channel}, |
| 78 | "first_name": {firstName}, |
| 79 | "last_name": {lastName}, |
| 80 | "restricted": {"1"}, |
| 81 | "token": {api.token}, |
| 82 | "resend": {"true"}, |
| 83 | "set_active": {"true"}, |
| 84 | "_attempts": {"1"}, |
| 85 | } |
| 86 | |
| 87 | err := api.adminRequest(ctx, "invite", teamName, values) |
| 88 | if err != nil { |
| 89 | return fmt.Errorf("Failed to restricted account: %s", err) |
| 90 | } |
| 91 | |
| 92 | return nil |
| 93 | } |
| 94 | |
| 95 | // InviteToTeam invites a user to a Slack team |
| 96 | func (api *Client) InviteToTeam(teamName, firstName, lastName, emailAddress string) error { |
| 97 | return api.InviteToTeamContext(context.Background(), teamName, firstName, lastName, emailAddress) |
| 98 | } |
| 99 | |
| 100 | // InviteToTeamContext invites a user to a Slack team with a custom context |
| 101 | func (api *Client) InviteToTeamContext(ctx context.Context, teamName, firstName, lastName, emailAddress string) error { |
| 102 | values := url.Values{ |
| 103 | "email": {emailAddress}, |
| 104 | "first_name": {firstName}, |
| 105 | "last_name": {lastName}, |
| 106 | "token": {api.token}, |
| 107 | "set_active": {"true"}, |
| 108 | "_attempts": {"1"}, |
| 109 | } |
| 110 | |
| 111 | err := api.adminRequest(ctx, "invite", teamName, values) |
| 112 | if err != nil { |
| 113 | return fmt.Errorf("Failed to invite to team: %s", err) |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | // SetRegular enables the specified user |
| 120 | func (api *Client) SetRegular(teamName, user string) error { |
| 121 | return api.SetRegularContext(context.Background(), teamName, user) |
| 122 | } |
| 123 | |
| 124 | // SetRegularContext enables the specified user with a custom context |
| 125 | func (api *Client) SetRegularContext(ctx context.Context, teamName, user string) error { |
| 126 | values := url.Values{ |
| 127 | "user": {user}, |
| 128 | "token": {api.token}, |
| 129 | "set_active": {"true"}, |
| 130 | "_attempts": {"1"}, |
| 131 | } |
| 132 | |
| 133 | err := api.adminRequest(ctx, "setRegular", teamName, values) |
| 134 | if err != nil { |
| 135 | return fmt.Errorf("Failed to change the user (%s) to a regular user: %s", user, err) |
| 136 | } |
| 137 | |
| 138 | return nil |
| 139 | } |
| 140 | |
| 141 | // SendSSOBindingEmail sends an SSO binding email to the specified user |
| 142 | func (api *Client) SendSSOBindingEmail(teamName, user string) error { |
| 143 | return api.SendSSOBindingEmailContext(context.Background(), teamName, user) |
| 144 | } |
| 145 | |
| 146 | // SendSSOBindingEmailContext sends an SSO binding email to the specified user with a custom context |
| 147 | func (api *Client) SendSSOBindingEmailContext(ctx context.Context, teamName, user string) error { |
| 148 | values := url.Values{ |
| 149 | "user": {user}, |
| 150 | "token": {api.token}, |
| 151 | "set_active": {"true"}, |
| 152 | "_attempts": {"1"}, |
| 153 | } |
| 154 | |
| 155 | err := api.adminRequest(ctx, "sendSSOBind", teamName, values) |
| 156 | if err != nil { |
| 157 | return fmt.Errorf("Failed to send SSO binding email for user (%s): %s", user, err) |
| 158 | } |
| 159 | |
| 160 | return nil |
| 161 | } |
| 162 | |
| 163 | // SetUltraRestricted converts a user into a single-channel guest |
| 164 | func (api *Client) SetUltraRestricted(teamName, uid, channel string) error { |
| 165 | return api.SetUltraRestrictedContext(context.Background(), teamName, uid, channel) |
| 166 | } |
| 167 | |
| 168 | // SetUltraRestrictedContext converts a user into a single-channel guest with a custom context |
| 169 | func (api *Client) SetUltraRestrictedContext(ctx context.Context, teamName, uid, channel string) error { |
| 170 | values := url.Values{ |
| 171 | "user": {uid}, |
| 172 | "channel": {channel}, |
| 173 | "token": {api.token}, |
| 174 | "set_active": {"true"}, |
| 175 | "_attempts": {"1"}, |
| 176 | } |
| 177 | |
| 178 | err := api.adminRequest(ctx, "setUltraRestricted", teamName, values) |
| 179 | if err != nil { |
| 180 | return fmt.Errorf("Failed to ultra-restrict account: %s", err) |
| 181 | } |
| 182 | |
| 183 | return nil |
| 184 | } |
| 185 | |
| 186 | // SetRestricted converts a user into a restricted account |
| 187 | func (api *Client) SetRestricted(teamName, uid string, channelIds ...string) error { |
| 188 | return api.SetRestrictedContext(context.Background(), teamName, uid, channelIds...) |
| 189 | } |
| 190 | |
| 191 | // SetRestrictedContext converts a user into a restricted account with a custom context |
| 192 | func (api *Client) SetRestrictedContext(ctx context.Context, teamName, uid string, channelIds ...string) error { |
| 193 | values := url.Values{ |
| 194 | "user": {uid}, |
| 195 | "token": {api.token}, |
| 196 | "set_active": {"true"}, |
| 197 | "_attempts": {"1"}, |
| 198 | "channels": {strings.Join(channelIds, ",")}, |
| 199 | } |
| 200 | |
| 201 | err := api.adminRequest(ctx, "setRestricted", teamName, values) |
| 202 | if err != nil { |
| 203 | return fmt.Errorf("failed to restrict account: %s", err) |
| 204 | } |
| 205 | |
| 206 | return nil |
| 207 | } |