• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

smartsheet / smartsheet-java-sdk / #61

04 Dec 2025 01:00PM UTC coverage: 60.098% (-0.06%) from 60.156%
#61

push

github

web-flow
Merge pull request #155 from smartsheet/add-user-reactivate-deactivate-endpoints-support

Add user reactivate deactivate endpoints support

0 of 4 new or added lines in 1 file covered. (0.0%)

93 existing lines in 4 files now uncovered.

4395 of 7313 relevant lines covered (60.1%)

0.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.03
/src/main/java/com/smartsheet/api/internal/UserResourcesImpl.java
1
/*
2
 * Copyright (C) 2025 Smartsheet
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package com.smartsheet.api.internal;
18

19
import com.smartsheet.api.AuthorizationException;
20
import com.smartsheet.api.InvalidRequestException;
21
import com.smartsheet.api.ResourceNotFoundException;
22
import com.smartsheet.api.ServiceUnavailableException;
23
import com.smartsheet.api.SmartsheetException;
24
import com.smartsheet.api.UserResources;
25
import com.smartsheet.api.internal.http.HttpEntity;
26
import com.smartsheet.api.internal.http.HttpMethod;
27
import com.smartsheet.api.internal.http.HttpRequest;
28
import com.smartsheet.api.internal.http.HttpResponse;
29
import com.smartsheet.api.internal.util.QueryUtil;
30
import com.smartsheet.api.internal.util.Util;
31
import com.smartsheet.api.models.AlternateEmail;
32
import com.smartsheet.api.models.DeleteUserParameters;
33
import com.smartsheet.api.models.PagedResult;
34
import com.smartsheet.api.models.PaginationParameters;
35
import com.smartsheet.api.models.Result;
36
import com.smartsheet.api.models.Sheet;
37
import com.smartsheet.api.models.TokenPaginatedResult;
38
import com.smartsheet.api.models.User;
39
import com.smartsheet.api.models.UserPlan;
40
import com.smartsheet.api.models.UserProfile;
41
import com.smartsheet.api.models.enums.ListUserInclusion;
42
import com.smartsheet.api.models.enums.SeatType;
43
import com.smartsheet.api.models.enums.UpgradeSeatType;
44
import com.smartsheet.api.models.enums.DowngradeSeatType;
45
import com.smartsheet.api.models.enums.UserInclusion;
46

47
import java.io.File;
48
import java.io.FileInputStream;
49
import java.io.FileNotFoundException;
50
import java.io.InputStream;
51
import java.net.URLEncoder;
52
import java.nio.charset.StandardCharsets;
53
import java.text.SimpleDateFormat;
54
import java.util.Date;
55
import java.util.EnumSet;
56
import java.util.HashMap;
57
import java.util.List;
58
import java.util.Map;
59
import java.util.Set;
60
import java.util.Collections;
61

62
/**
63
 * This is the implementation of the UserResources.
64
 * <p>
65
 * Thread Safety: This class is thread safe because it is immutable and its base class is thread safe.
66
 */
67
public class UserResourcesImpl extends AbstractResources implements UserResources {
68

69
    private static final String USERS = "users";
70
    private static final String ALTERNATE_EMAILS = "alternateemails";
71
    public static final String PLANS = "/plans/";
72

73
    /**
74
     * Constructor.
75
     * <p>
76
     * Exceptions: - IllegalArgumentException : if any argument is null
77
     *
78
     * @param smartsheet the smartsheet
79
     */
80
    public UserResourcesImpl(SmartsheetImpl smartsheet) {
81
        super(smartsheet);
1✔
82
    }
1✔
83

84
    /**
85
     * List all users.
86
     * <p>
87
     * It mirrors to the following Smartsheet REST API method: GET /users
88
     *
89
     * @return the list of all users
90
     * @throws IllegalArgumentException    if any argument is null or empty string
91
     * @throws InvalidRequestException     if there is any problem with the REST API request
92
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
93
     * @throws ResourceNotFoundException   if the resource cannot be found
94
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
95
     * @throws SmartsheetException         if there is any other error during the operation
96
     */
97
    public PagedResult<User> listUsers() throws SmartsheetException {
98
        return this.listUsersInternal(null, null, null, null, null);
1✔
99
    }
100

101
    /**
102
     * List all users.
103
     * <p>
104
     * It mirrors to the following Smartsheet REST API method: GET /users
105
     * <p>
106
     * Exceptions:
107
     *   - InvalidRequestException : if there is any problem with the REST API request
108
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
109
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
110
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
111
     *   - SmartsheetException : if there is any other error occurred during the operation
112
     *
113
     * @param pagination the object containing the pagination query parameters
114
     * @return all users (note that empty list will be returned if there is none)
115
     * @throws SmartsheetException the smartsheet exception
116
     */
117
    public PagedResult<User> listUsers(PaginationParameters pagination) throws SmartsheetException {
118
        return this.listUsersInternal(null, null, null, null, pagination);
1✔
119
    }
120

121
    /**
122
     * List all users.
123
     * <p>
124
     * It mirrors to the following Smartsheet REST API method: GET /users
125
     * <p>
126
     * Exceptions:
127
     * - InvalidRequestException : if there is any problem with the REST API request
128
     * - AuthorizationException : if there is any problem with the REST API authorization(access token)
129
     * - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
130
     * - SmartsheetRestException : if there is any other REST API related error occurred during the operation
131
     * - SmartsheetException : if there is any other error occurred during the operation
132
     *
133
     * @param email      the list of email addresses
134
     * @param pagination the object containing the pagination query parameters
135
     * @return all users (note that empty list will be returned if there is none)
136
     * @throws SmartsheetException the smartsheet exception
137
     */
138
    public PagedResult<User> listUsers(Set<String> email, PaginationParameters pagination) throws SmartsheetException {
139
        return this.listUsersInternal(email, null, null, null, pagination);
1✔
140
    }
141

142
    /**
143
     * List all users.
144
     * <p>
145
     * It mirrors to the following Smartsheet REST API method: GET /users
146
     * <p>
147
     * Exceptions:
148
     * - InvalidRequestException : if there is any problem with the REST API request
149
     * - AuthorizationException : if there is any problem with the REST API authorization(access token)
150
     * - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
151
     * - SmartsheetRestException : if there is any other REST API related error occurred during the operation
152
     * - SmartsheetException : if there is any other error occurred during the operation
153
     *
154
     * @param email      the list of email addresses
155
     * @param includes   elements to include in the response
156
     * @param pagination the object containing the pagination query parameters
157
     * @return all users (note that empty list will be returned if there is none)
158
     * @throws SmartsheetException the smartsheet exception
159
     */
160
    public PagedResult<User> listUsers(Set<String> email, EnumSet<ListUserInclusion> includes,
161
                                       PaginationParameters pagination) throws SmartsheetException {
162
        return this.listUsersInternal(email, includes, null, null, pagination);
1✔
163
    }
164

165
    /**
166
     * List all users with support for Seat Type and Plan ID. If planID or seatType is provided, then the response
167
     * will contain  planId, seatType, seatTypeLastChangedAt, isInternal, otherwise - not
168
     * <p>
169
     * It mirrors to the following Smartsheet REST API method: GET /users
170
     * <p>
171
     * Exceptions:
172
     *   - InvalidRequestException : if there is any problem with the REST API request
173
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
174
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
175
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
176
     *   - SmartsheetException : if there is any other error occurred during the operation
177
     *
178
     * @param email the list of email addresses
179
     * @param pagination the object containing the pagination query parameters
180
     * @param planId filtering all users part of the specific plan
181
     * @param seatType filter users by seat type
182
     * @return all users (note that empty list will be returned if there is none)
183
     * @throws SmartsheetException the smartsheet exception
184
     */
185
    @Override
186
    public PagedResult<User> listUsers(Set<String> email, Long planId,
187
                                       SeatType seatType, PaginationParameters pagination
188
                                       ) throws SmartsheetException {
189
        return this.listUsersInternal(email, null, planId, seatType, pagination);
1✔
190
    }
191

192
    /**
193
     * List all users with support for Seat Type and Plan ID. If planID or seatType is provided, then the response
194
     * will contain  planId, seatType, seatTypeLastChangedAt, isInternal, otherwise - not
195
     * <p>
196
     * It mirrors to the following Smartsheet REST API method: GET /users
197
     * <p>
198
     * Exceptions:
199
     *   - InvalidRequestException : if there is any problem with the REST API request
200
     *   - AuthorizationException : if there is any problem with the REST API authorization(access token)
201
     *   - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
202
     *   - SmartsheetRestException : if there is any other REST API related error occurred during the operation
203
     *   - SmartsheetException : if there is any other error occurred during the operation
204
     *
205
     * @param email the list of email addresses
206
     * @param includes elements to include in the response
207
     * @param pagination the object containing the pagination query parameters
208
     * @param planId filtering all users part of the specific plan
209
     * @param seatType filter users by seat type
210
     * @return all users (note that empty list will be returned if there is none)
211
     * @throws SmartsheetException the smartsheet exception
212
     */
213
    private PagedResult<User> listUsersInternal(Set<String> email, EnumSet<ListUserInclusion> includes,
214
                                                Long planId, SeatType seatType, PaginationParameters pagination
215
                                                ) throws SmartsheetException {
216
        String path = USERS;
1✔
217
        Map<String, Object> parameters = new HashMap<>();
1✔
218

219
        if (pagination != null) {
1✔
220
            parameters = pagination.toHashMap();
1✔
221
        }
222

223
        if (email != null) {
1✔
224
            parameters.put("email", QueryUtil.generateCommaSeparatedList(email));
1✔
225
        }
226

227
        if (includes != null) {
1✔
228
            parameters.put("include", QueryUtil.generateCommaSeparatedList(includes));
1✔
229
        }
230

231
        // Seat type support
232
        if (planId != null) {
1✔
233
            parameters.put("planId", planId);
1✔
234
        }
235

236
        if (seatType != null) {
1✔
237
            parameters.put("seatType", seatType);
1✔
238
        }
239

240
        path += QueryUtil.generateUrl(null, parameters);
1✔
241
        return this.listResourcesWithWrapper(path, User.class);
1✔
242
    }
243

244
    /**
245
     * Add a user to the organization, without sending email.
246
     * <p>
247
     * It mirrors to the following Smartsheet REST API method: POST /users
248
     * <p>
249
     * Exceptions:
250
     * - IllegalArgumentException : if any argument is null
251
     * - InvalidRequestException : if there is any problem with the REST API request
252
     * - AuthorizationException : if there is any problem with the REST API authorization(access token)
253
     * - ResourceNotFoundException : if the resource can not be found
254
     * - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
255
     * - SmartsheetRestException : if there is any other REST API related error occurred during the operation
256
     * - SmartsheetException : if there is any other error occurred during the operation
257
     *
258
     * @param user the user object limited to the following attributes: * admin * email * licensedSheetCreator
259
     * @return the user
260
     * @throws SmartsheetException the smartsheet exception
261
     */
262
    public User addUser(User user) throws SmartsheetException {
263
        return this.createResource(USERS, User.class, user);
1✔
264
    }
265

266
    /**
267
     * Add a user to the organization, without sending email.
268
     * <p>
269
     * It mirrors to the following Smartsheet REST API method: POST /users
270
     * <p>
271
     * Exceptions:
272
     * - IllegalArgumentException : if any argument is null
273
     * - InvalidRequestException : if there is any problem with the REST API request
274
     * - AuthorizationException : if there is any problem with the REST API authorization(access token)
275
     * - ResourceNotFoundException : if the resource can not be found
276
     * - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
277
     * - SmartsheetRestException : if there is any other REST API related error occurred during the operation
278
     * - SmartsheetException : if there is any other error occurred during the operation
279
     *
280
     * @param user      the created user
281
     * @param sendEmail whether to send email
282
     * @return the user object limited to the following attributes: * admin * email * licensedSheetCreator
283
     * @throws SmartsheetException the smartsheet exception
284
     */
285
    public User addUser(User user, boolean sendEmail) throws SmartsheetException {
286
        return this.createResource("users?sendEmail=" + sendEmail, User.class, user);
1✔
287
    }
288

289
    /**
290
     * Get the current user.
291
     * <p>
292
     * It mirrors to the following Smartsheet REST API method: GET /users/{userId}
293
     *
294
     * @param userId the user id
295
     * @return the user
296
     * @throws IllegalArgumentException    if any argument is null or empty string
297
     * @throws InvalidRequestException     if there is any problem with the REST API request
298
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
299
     * @throws ResourceNotFoundException   if the resource cannot be found
300
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
301
     * @throws SmartsheetException         if there is any other error during the operation
302
     */
303
    public UserProfile getUser(long userId) throws SmartsheetException {
304
        return this.getResource(USERS + "/" + userId, UserProfile.class);
1✔
305
    }
306

307
    /**
308
     * Get the current user.
309
     * <p>
310
     * It mirrors to the following Smartsheet REST API method: GET /users/me
311
     * <p>
312
     * Exceptions:
313
     * - InvalidRequestException : if there is any problem with the REST API request
314
     * - AuthorizationException : if there is any problem with the REST API authorization(access token)
315
     * - ResourceNotFoundException : if the resource can not be found
316
     * - ServiceUnavailableException : if the REST API service is not available (possibly due to rate limiting)
317
     * - SmartsheetRestException : if there is any other REST API related error occurred during the operation
318
     * - SmartsheetException : if there is any other error occurred during the operation
319
     *
320
     * @return the resource (note that if there is no such resource, this method will throw ResourceNotFoundException
321
     * rather than returning null).
322
     * @throws SmartsheetException the smartsheet exception
323
     */
324
    public UserProfile getCurrentUser() throws SmartsheetException {
325
        return this.getResource("users/me", UserProfile.class);
1✔
326
    }
327

328
    /**
329
     * <p>Get the current user.</p>
330
     *
331
     * <p>It mirrors to the following Smartsheet REST API method: GET /user/me</p>
332
     *
333
     * @param includes used to specify the optional objects to include.
334
     * @return the current user
335
     * @throws IllegalArgumentException    if any argument is null or empty string
336
     * @throws InvalidRequestException     if there is any problem with the REST API request
337
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
338
     * @throws ResourceNotFoundException   if the resource cannot be found
339
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
340
     * @throws SmartsheetException         if there is any other error during the operation
341
     */
342
    public UserProfile getCurrentUser(EnumSet<UserInclusion> includes) throws SmartsheetException {
343
        String path = "users/me";
1✔
344

345
        Map<String, Object> parameters = new HashMap<>();
1✔
346
        parameters.put("include", QueryUtil.generateCommaSeparatedList(includes));
1✔
347

348
        path += QueryUtil.generateUrl(null, parameters);
1✔
349
        return this.getResource(path, UserProfile.class);
1✔
350
    }
351

352
    /**
353
     * List all organisation sheets.
354
     * <p>
355
     * It mirrors to the following Smartsheet REST API method: GET /users/sheets
356
     *
357
     * @param pagination the object containing the pagination query parameters
358
     * @return the list of all organisation sheets
359
     * @throws IllegalArgumentException    if any argument is null or empty string
360
     * @throws InvalidRequestException     if there is any problem with the REST API request
361
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
362
     * @throws ResourceNotFoundException   if the resource cannot be found
363
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
364
     * @throws SmartsheetException         if there is any other error during the operation
365
     */
366
    public PagedResult<Sheet> listOrgSheets(PaginationParameters pagination, Date modifiedSince) throws SmartsheetException {
367
        String path = "users/sheets";
1✔
368

369
        Map<String, Object> parameters = new HashMap<>();
1✔
370
        if (pagination != null) {
1✔
371
            parameters = pagination.toHashMap();
1✔
372
        }
373
        if (modifiedSince != null) {
1✔
374
            String isoDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").format(modifiedSince);
×
UNCOV
375
            parameters.put("modifiedSince", isoDate);
×
376
        }
377
        path += QueryUtil.generateUrl(null, parameters);
1✔
378
        return this.listResourcesWithWrapper(path, Sheet.class);
1✔
379
    }
380

381
    public PagedResult<Sheet> listOrgSheets(PaginationParameters pagination) throws SmartsheetException {
UNCOV
382
        return this.listOrgSheets(pagination, null);
×
383
    }
384

385
    /**
386
     * List all user alternate emails.
387
     * <p>
388
     * It mirrors to the following Smartsheet REST API method: GET /users/{userId}/alternateemails
389
     *
390
     * @param userId     the id of the user
391
     * @param pagination the object containing the pagination query parameters
392
     * @return the list of all user alternate emails
393
     * @throws IllegalArgumentException    if any argument is null or empty string
394
     * @throws InvalidRequestException     if there is any problem with the REST API request
395
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
396
     * @throws ResourceNotFoundException   if the resource cannot be found
397
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
398
     * @throws SmartsheetException         if there is any other error during the operation
399
     */
400
    public PagedResult<AlternateEmail> listAlternateEmails(long userId, PaginationParameters pagination) throws SmartsheetException {
401
        String path = USERS + "/" + userId + "/" + ALTERNATE_EMAILS;
1✔
402

403
        if (pagination != null) {
1✔
404
            path += pagination.toQueryString();
1✔
405
        }
406
        return this.listResourcesWithWrapper(path, AlternateEmail.class);
1✔
407
    }
408

409
    /**
410
     * Get alternate email.
411
     * <p>
412
     * It mirrors to the following Smartsheet REST API method: GET /users/{userId}/alternateemails/{alternateEmailId}
413
     *
414
     * @param userId     the id of the user
415
     * @param altEmailId the alternate email id for the alternate email to retrieve.
416
     * @return the resource (note that if there is no such resource, this method will throw
417
     * ResourceNotFoundException rather than returning null).
418
     * @throws IllegalArgumentException    if any argument is null or empty string
419
     * @throws InvalidRequestException     if there is any problem with the REST API request
420
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
421
     * @throws ResourceNotFoundException   if the resource cannot be found
422
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
423
     * @throws SmartsheetException         if there is any other error during the operation
424
     */
425
    public AlternateEmail getAlternateEmail(long userId, long altEmailId) throws SmartsheetException {
426
        return this.getResource(USERS + "/" + userId + "/" + ALTERNATE_EMAILS + "/" + altEmailId, AlternateEmail.class);
1✔
427
    }
428

429
    /**
430
     * Add an alternate email.
431
     * <p>
432
     * It mirrors to the following Smartsheet REST API method: POST /users/{userId}/alternateemails
433
     *
434
     * @param userId    the id of the user
435
     * @param altEmails AlternateEmail alternate email address to add.
436
     * @return the resource (note that if there is no such resource, this method will throw
437
     * ResourceNotFoundException rather than returning null).
438
     * @throws IllegalArgumentException    if any argument is null or empty string
439
     * @throws InvalidRequestException     if there is any problem with the REST API request
440
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
441
     * @throws ResourceNotFoundException   if the resource cannot be found
442
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
443
     * @throws SmartsheetException         if there is any other error during the operation
444
     */
445
    public List<AlternateEmail> addAlternateEmail(long userId, List<AlternateEmail> altEmails) throws SmartsheetException {
446
        Util.throwIfNull(altEmails);
1✔
447
        if (altEmails.size() == 0) {
1✔
448
            return altEmails;
1✔
449
        }
450
        return this.postAndReceiveList(USERS + "/" + userId + "/" + ALTERNATE_EMAILS, altEmails, AlternateEmail.class);
1✔
451
    }
452

453
    /**
454
     * Delete an alternate email.
455
     * <p>
456
     * It mirrors to the following Smartsheet REST API method: DELETE /users/{userId}/alternateemails/{alternateEmailId}
457
     *
458
     * @param userId     the id of the user
459
     * @param altEmailId the alternate email id for the alternate email to retrieve.
460
     * @throws IllegalArgumentException    if any argument is null or empty string
461
     * @throws InvalidRequestException     if there is any problem with the REST API request
462
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
463
     * @throws ResourceNotFoundException   if the resource cannot be found
464
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
465
     * @throws SmartsheetException         if there is any other error during the operation
466
     */
467
    public void deleteAlternateEmail(long userId, long altEmailId) throws SmartsheetException {
468
        this.deleteResource(USERS + "/" + userId + "/" + ALTERNATE_EMAILS + "/" + altEmailId, AlternateEmail.class);
1✔
469
    }
1✔
470

471
    /**
472
     * Promote and alternate email to primary.
473
     *
474
     * @param userId     id of the user
475
     * @param altEmailId alternate email id
476
     * @return alternateEmail of the primary
477
     * @throws IllegalArgumentException    if any argument is null or empty string
478
     * @throws InvalidRequestException     if there is any problem with the REST API request
479
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
480
     * @throws ResourceNotFoundException   if the resource cannot be found
481
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
482
     * @throws SmartsheetException         f there is any other error during the operation
483
     */
484
    public AlternateEmail promoteAlternateEmail(long userId, long altEmailId) throws SmartsheetException {
485

UNCOV
486
        HttpRequest request = createHttpRequest(smartsheet.getBaseURI().resolve(
×
487
                USERS + "/" + userId + "/" + ALTERNATE_EMAILS + "/" + altEmailId + "/makeprimary"), HttpMethod.POST);
488

UNCOV
489
        Object obj = null;
×
490
        try {
491
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
UNCOV
492
            switch (response.getStatusCode()) {
×
493
                case 200:
494
                    obj = this.smartsheet.getJsonSerializer().deserializeResult(AlternateEmail.class,
×
495
                            response.getEntity().getContent());
×
UNCOV
496
                    break;
×
497
                default:
UNCOV
498
                    handleError(response);
×
499
            }
500
        } finally {
UNCOV
501
            smartsheet.getHttpClient().releaseConnection();
×
502
        }
503

UNCOV
504
        return (AlternateEmail) obj;
×
505
    }
506

507
    /**
508
     * Uploads a profile image for the specified user.
509
     *
510
     * @param userId   id of the user
511
     * @param file     path to the image file
512
     * @param fileType content type of the image file
513
     * @return user
514
     * @throws IllegalArgumentException    if any argument is null or empty string
515
     * @throws InvalidRequestException     if there is any problem with the REST API request
516
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
517
     * @throws ResourceNotFoundException   if the resource cannot be found
518
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
519
     * @throws SmartsheetException         f there is any other error during the operation
520
     */
521
    public User addProfileImage(long userId, String file, String fileType) throws SmartsheetException, FileNotFoundException {
UNCOV
522
        return attachProfileImage(USERS + "/" + userId + "/profileimage", file, fileType);
×
523
    }
524

525
    private User attachProfileImage(String path, String file, String contentType) throws SmartsheetException, FileNotFoundException {
UNCOV
526
        Util.throwIfNull(file);
×
527

528
        if (contentType == null) {
×
UNCOV
529
            contentType = "application/octet-stream";
×
530
        }
531

532
        Map<String, Object> parameters = new HashMap<>();
×
UNCOV
533
        path += QueryUtil.generateUrl(null, parameters);
×
534

535
        HttpRequest request = createHttpRequest(this.smartsheet.getBaseURI().resolve(path), HttpMethod.POST);
×
536
        String attachmentHeaderValue = "attachment; filename=\"" + URLEncoder.encode(file, StandardCharsets.UTF_8) + "\"";
×
UNCOV
537
        request.getHeaders().put("Content-Disposition", attachmentHeaderValue);
×
538

539
        File f = new File(file);
×
UNCOV
540
        InputStream is = new FileInputStream(f);
×
541

542
        HttpEntity entity = new HttpEntity();
×
543
        entity.setContentType(contentType);
×
544
        entity.setContent(is);
×
545
        entity.setContentLength(f.length());
×
UNCOV
546
        request.setEntity(entity);
×
547

UNCOV
548
        User obj = null;
×
549
        try {
550
            HttpResponse response = this.smartsheet.getHttpClient().request(request);
×
UNCOV
551
            switch (response.getStatusCode()) {
×
552
                case 200:
553
                    obj = this.smartsheet.getJsonSerializer().deserializeResult(User.class,
×
554
                            response.getEntity().getContent()).getResult();
×
UNCOV
555
                    break;
×
556
                default:
UNCOV
557
                    handleError(response);
×
558
            }
559
        } finally {
UNCOV
560
            smartsheet.getHttpClient().releaseConnection();
×
561
        }
562

UNCOV
563
        return obj;
×
564
    }
565

566
    /**
567
     * <p>Update a user.</p>
568
     *
569
     * <p>It mirrors to the following Smartsheet REST API method: PUT /user/{id}</p>
570
     *
571
     * @param user the user to update
572
     * @return the updated user
573
     * @throws IllegalArgumentException    if any argument is null or empty string
574
     * @throws InvalidRequestException     if there is any problem with the REST API request
575
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
576
     * @throws ResourceNotFoundException   if the resource cannot be found
577
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
578
     * @throws SmartsheetException         if there is any other error during the operation
579
     */
580
    @Override
581
    public User updateUser(User user) throws SmartsheetException {
582
        return this.updateResource(USERS + "/" + user.getId(), User.class, user);
1✔
583
    }
584

585
    /**
586
     * <p>Fetch all user's plans.</p>
587
     *
588
     * <p>It mirrors to the following Smartsheet REST API method: GET /users/{userId}/plans</p>
589
     *
590
     * @param userId the id of the user whose plans to fetch
591
     * @param lastKey lastKey from previous response to get next page of results
592
     * @return UserPlansResponse json response
593
     * @throws IllegalArgumentException    if any argument is null or empty string
594
     * @throws InvalidRequestException     if there is any problem with the REST API request
595
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
596
     * @throws ResourceNotFoundException   if the resource cannot be found
597
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
598
     * @throws SmartsheetException         if there is any other error during the operation
599
     */
600
    @Override
601
    public TokenPaginatedResult<UserPlan> listUserPlans(long userId, String lastKey, Long maxItems) throws SmartsheetException {
602

603
        String path = USERS + "/" + userId + "/plans";
1✔
604
        Map<String, Object> parameters = new HashMap<>();
1✔
605

606
        if (lastKey != null) {
1✔
607
            parameters.put("lastKey", lastKey);
1✔
608
        }
609

610
        if (maxItems != null) {
1✔
611
            parameters.put("maxItems", maxItems);
1✔
612
        }
613
        path += QueryUtil.generateUrl(null, parameters);
1✔
614
        return this.listResourcesWithTokenPagination(path, UserPlan.class);
1✔
615
    }
616

617
    /**
618
     * <p>Remove's a user from a plan.</p>
619
     *
620
     * <p>It mirrors to the following Smartsheet REST API method: DELETE /2.0/users/{userId}/plans/{planId}</p>
621
     *
622
     * @param userId the id of the user whose plans to fetch
623
     * @param planId the id of the plan from which to remove the user
624
     * @throws IllegalArgumentException    if any argument is null or empty string
625
     * @throws InvalidRequestException     if there is any problem with the REST API request
626
     * @throws AuthorizationException      if there is any problem with  the REST API authorization (access token)
627
     * @throws ResourceNotFoundException   if the resource cannot be found
628
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
629
     * @throws SmartsheetException         if there is any other error during the operation
630
     */
631
    @Override
632
    public void removeUserFromPlan(long userId, long planId) throws SmartsheetException {
633
        deleteResource(USERS + "/" + userId + PLANS + planId, Object.class);
1✔
634
    }
1✔
635

636
    /**
637
     * <p>Upgrades a user's seat type.</p>
638
     *
639
     * <p>It mirrors to the following Smartsheet REST API method: POST /users/{userId}/plans/{planId}/upgrade</p>
640
     * @param userId the ID of the user to upgrade
641
     * @param planId the ID of the plan to upgrade to
642
     * @param seatType the new seat type for the user
643
     * @throws IllegalArgumentException if any argument is null or empty string
644
     * @throws InvalidRequestException if there is any problem with the REST API request
645
     * @throws AuthorizationException if there is any problem with the REST API authorization
646
     * @throws ResourceNotFoundException if the resource cannot be found
647
     * @throws ServiceUnavailableException if the REST API service is not available
648
     * @throws SmartsheetException if there is any other error during the operation
649
     */
650
    @Override
651
    public void upgradeUser(long userId, long planId, UpgradeSeatType seatType) throws SmartsheetException {
652
        if (seatType != null) {
1✔
653
            changeSeatType(seatType.name(), USERS + "/" + userId + PLANS + planId + "/upgrade");
1✔
654
            return;
1✔
655
        }
UNCOV
656
        changeSeatType(null, USERS + "/" + userId + PLANS + planId + "/upgrade");
×
UNCOV
657
    }
×
658

659
    /**
660
     * <p>Downgrades a user's seat type.</p>
661
     *
662
     * <p>It mirrors to the following Smartsheet REST API method: POST /users/{userId}/plans/{planId}/downgrade</p>
663
     * @param userId the ID of the user to downgrade
664
     * @param planId the ID of the plan to downgrade to
665
     * @param seatType the new seat type for the user
666
     * @throws IllegalArgumentException if any argument is null or empty string
667
     * @throws InvalidRequestException if there is any problem with the REST API request
668
     * @throws AuthorizationException if there is any problem with the REST API authorization
669
     * @throws ResourceNotFoundException if the resource cannot be found
670
     * @throws ServiceUnavailableException if the REST API service is not available
671
     * @throws SmartsheetException if there is any other error during the operation
672
     */
673
    @Override
674
    public void downgradeUser(long userId, long planId, DowngradeSeatType seatType) throws SmartsheetException {
675
        Util.throwIfNull(seatType);
1✔
676
        changeSeatType(seatType.name(), USERS + "/" + userId + PLANS + planId + "/downgrade");
1✔
677
    }
1✔
678

679
    private void changeSeatType(String seatType, String path) throws SmartsheetException {
680
        Map<String, String> body = Collections.emptyMap();
1✔
681
        if (seatType != null) {
1✔
682
            body = Map.of("seatType", seatType);
1✔
683
        }
684
        createResource(path, Result.class, body);
1✔
685
    }
1✔
686

687
    /**
688
     * <p>Reactivates a user.</p>
689
     *
690
     * <p>Reactivates the user associated with the current Smartsheet plan, restoring the user's access to
691
     * Smartsheet, owned items, and shared items.</p>
692
     *
693
     * <p>Important: You can reactivate the user only if that user has been deactivated for less than thirty (30) days.</p>
694
     *
695
     * <p>It mirrors to the following Smartsheet REST API method: POST /users/{userId}/reactivate</p>
696
     *
697
     * @param userId the id of the user to reactivate
698
     * @throws IllegalArgumentException    if any argument is null or empty string
699
     * @throws InvalidRequestException     if there is any problem with the REST API request (e.g., user email belongs to ISP domain)
700
     * @throws AuthorizationException      if there is any problem with the REST API authorization (access token)
701
     * @throws ResourceNotFoundException   if the resource cannot be found
702
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
703
     * @throws SmartsheetException         if there is any other error during the operation
704
     */
705
    @Override
706
    public void reactivateUser(long userId) throws SmartsheetException {
NEW
707
        createResource(USERS + "/" + userId + "/reactivate", Result.class, Collections.emptyMap());
×
NEW
708
    }
×
709

710
    /**
711
     * <p>Deactivates a user.</p>
712
     *
713
     * <p>Deactivates the user associated with the current Smartsheet plan, blocking the user from using Smartsheet in any way.
714
     * Deactivating a user does not affect their existing permissions on owned or shared items.</p>
715
     *
716
     * <p>It mirrors to the following Smartsheet REST API method: POST /users/{userId}/deactivate</p>
717
     *
718
     * @param userId the id of the user to deactivate
719
     * @throws IllegalArgumentException    if any argument is null or empty string
720
     * @throws InvalidRequestException     if there is any problem with the REST API request (e.g., user email belongs to ISP domain or user is managed by external source)
721
     * @throws AuthorizationException      if there is any problem with the REST API authorization (access token)
722
     * @throws ResourceNotFoundException   if the resource cannot be found
723
     * @throws ServiceUnavailableException if the REST API service is not available (possibly due to rate limiting)
724
     * @throws SmartsheetException         if there is any other error during the operation
725
     */
726
    @Override
727
    public void deactivateUser(long userId) throws SmartsheetException {
NEW
728
        createResource(USERS + "/" + userId + "/deactivate", Result.class, Collections.emptyMap());
×
NEW
729
    }
×
730

731
    @Override
732
    public void deleteUser(long userId, DeleteUserParameters parameters) throws SmartsheetException {
733
        String path = USERS + "/" + userId;
1✔
734

735
        if (parameters != null) {
1✔
736
            path += parameters.toQueryString();
1✔
737
        }
738

739
        this.deleteResource(path, User.class);
1✔
740
    }
1✔
741
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc