@ -38,12 +38,16 @@ import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController ;
import org.thingsboard.common.util.JacksonUtil ;
import org.thingsboard.rule.engine.api.MailService ;
import org.thingsboard.server.common.data.DashboardInfo ;
import org.thingsboard.server.common.data.EntityType ;
import org.thingsboard.server.common.data.User ;
import org.thingsboard.server.common.data.UserEmailInfo ;
import org.thingsboard.server.common.data.alarm.Alarm ;
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode ;
import org.thingsboard.server.common.data.exception.ThingsboardException ;
import org.thingsboard.server.common.data.id.AlarmId ;
import org.thingsboard.server.common.data.id.CustomerId ;
import org.thingsboard.server.common.data.id.DashboardId ;
import org.thingsboard.server.common.data.id.TenantId ;
import org.thingsboard.server.common.data.id.UserId ;
import org.thingsboard.server.common.data.page.PageData ;
@ -55,9 +59,14 @@ import org.thingsboard.server.common.data.query.EntityTypeFilter;
import org.thingsboard.server.common.data.query.TsValue ;
import org.thingsboard.server.common.data.security.Authority ;
import org.thingsboard.server.common.data.security.UserCredentials ;
import org.thingsboard.server.common.data.security.UserSettings ;
import org.thingsboard.server.common.data.settings.LastVisitedDashboardInfo ;
import org.thingsboard.server.common.data.settings.UserDashboardAction ;
import org.thingsboard.server.common.data.settings.UserDashboardsInfo ;
import org.thingsboard.server.common.data.settings.UserSettings ;
import org.thingsboard.server.common.data.security.event.UserCredentialsInvalidationEvent ;
import org.thingsboard.server.common.data.security.model.JwtPair ;
import org.thingsboard.server.dao.entity.EntityService ;
import org.thingsboard.server.common.data.settings.UserSettingsType ;
import org.thingsboard.server.queue.util.TbCoreComponent ;
import org.thingsboard.server.service.entitiy.user.TbUserService ;
import org.thingsboard.server.service.query.EntityQueryService ;
@ -69,13 +78,17 @@ import org.thingsboard.server.service.security.permission.Resource;
import org.thingsboard.server.service.security.system.SystemSecurityService ;
import javax.servlet.http.HttpServletRequest ;
import java.util.ArrayList ;
import java.util.Arrays ;
import java.util.Collections ;
import java.util.List ;
import java.util.Map ;
import static org.thingsboard.server.common.data.query.EntityKeyType.ENTITY_FIELD ;
import static org.thingsboard.server.controller.ControllerConstants.ALARM_ID_PARAM_DESCRIPTION ;
import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID ;
import static org.thingsboard.server.controller.ControllerConstants.CUSTOMER_ID_PARAM_DESCRIPTION ;
import static org.thingsboard.server.controller.ControllerConstants.DASHBOARD_ID_PARAM_DESCRIPTION ;
import static org.thingsboard.server.controller.ControllerConstants.DEFAULT_DASHBOARD ;
import static org.thingsboard.server.controller.ControllerConstants.HOME_DASHBOARD ;
import static org.thingsboard.server.controller.ControllerConstants.PAGE_DATA_PARAMETERS ;
@ -118,6 +131,9 @@ public class UserController extends BaseController {
@Autowired
private EntityQueryService entityQueryService ;
@Autowired
private EntityService entityService ;
@ApiOperation ( value = "Get User (getUserById)" ,
notes = "Fetch the User object based on the provided User Id. " +
"If the user has the authority of 'SYS_ADMIN', the server does not perform additional checks. " +
@ -130,22 +146,18 @@ public class UserController extends BaseController {
@ApiParam ( value = USER_ID_PARAM_DESCRIPTION )
@PathVariable ( USER_ID ) String strUserId ) throws ThingsboardException {
checkParameter ( USER_ID , strUserId ) ;
try {
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
User user = checkUserId ( userId , Operation . READ ) ;
if ( user . getAdditionalInfo ( ) . isObject ( ) ) {
ObjectNode additionalInfo = ( ObjectNode ) user . getAdditionalInfo ( ) ;
processDashboardIdFromAdditionalInfo ( additionalInfo , DEFAULT_DASHBOARD ) ;
processDashboardIdFromAdditionalInfo ( additionalInfo , HOME_DASHBOARD ) ;
UserCredentials userCredentials = userService . findUserCredentialsByUserId ( user . getTenantId ( ) , user . getId ( ) ) ;
if ( userCredentials . isEnabled ( ) & & ! additionalInfo . has ( "userCredentialsEnabled" ) ) {
additionalInfo . put ( "userCredentialsEnabled" , true ) ;
}
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
User user = checkUserId ( userId , Operation . READ ) ;
if ( user . getAdditionalInfo ( ) . isObject ( ) ) {
ObjectNode additionalInfo = ( ObjectNode ) user . getAdditionalInfo ( ) ;
processDashboardIdFromAdditionalInfo ( additionalInfo , DEFAULT_DASHBOARD ) ;
processDashboardIdFromAdditionalInfo ( additionalInfo , HOME_DASHBOARD ) ;
UserCredentials userCredentials = userService . findUserCredentialsByUserId ( user . getTenantId ( ) , user . getId ( ) ) ;
if ( userCredentials . isEnabled ( ) & & ! additionalInfo . has ( "userCredentialsEnabled" ) ) {
additionalInfo . put ( "userCredentialsEnabled" , true ) ;
}
return user ;
} catch ( Exception e ) {
throw handleException ( e ) ;
}
return user ;
}
@ApiOperation ( value = "Check Token Access Enabled (isUserTokenAccessEnabled)" ,
@ -170,21 +182,17 @@ public class UserController extends BaseController {
@ApiParam ( value = USER_ID_PARAM_DESCRIPTION )
@PathVariable ( USER_ID ) String strUserId ) throws ThingsboardException {
checkParameter ( USER_ID , strUserId ) ;
try {
if ( ! userTokenAccessEnabled ) {
throw new ThingsboardException ( YOU_DON_T_HAVE_PERMISSION_TO_PERFORM_THIS_OPERATION ,
ThingsboardErrorCode . PERMISSION_DENIED ) ;
}
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
SecurityUser authUser = getCurrentUser ( ) ;
User user = checkUserId ( userId , Operation . READ ) ;
UserPrincipal principal = new UserPrincipal ( UserPrincipal . Type . USER_NAME , user . getEmail ( ) ) ;
UserCredentials credentials = userService . findUserCredentialsByUserId ( authUser . getTenantId ( ) , userId ) ;
SecurityUser securityUser = new SecurityUser ( user , credentials . isEnabled ( ) , principal ) ;
return tokenFactory . createTokenPair ( securityUser ) ;
} catch ( Exception e ) {
throw handleException ( e ) ;
if ( ! userTokenAccessEnabled ) {
throw new ThingsboardException ( YOU_DON_T_HAVE_PERMISSION_TO_PERFORM_THIS_OPERATION ,
ThingsboardErrorCode . PERMISSION_DENIED ) ;
}
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
SecurityUser authUser = getCurrentUser ( ) ;
User user = checkUserId ( userId , Operation . READ ) ;
UserPrincipal principal = new UserPrincipal ( UserPrincipal . Type . USER_NAME , user . getEmail ( ) ) ;
UserCredentials credentials = userService . findUserCredentialsByUserId ( authUser . getTenantId ( ) , userId ) ;
SecurityUser securityUser = new SecurityUser ( user , credentials . isEnabled ( ) , principal ) ;
return tokenFactory . createTokenPair ( securityUser ) ;
}
@ApiOperation ( value = "Save Or update User (saveUser)" ,
@ -219,23 +227,19 @@ public class UserController extends BaseController {
@ApiParam ( value = "Email of the user" , required = true )
@RequestParam ( value = "email" ) String email ,
HttpServletRequest request ) throws ThingsboardException {
try {
User user = checkNotNull ( userService . findUserByEmail ( getCurrentUser ( ) . getTenantId ( ) , email ) ) ;
accessControlService . checkPermission ( getCurrentUser ( ) , Resource . USER , Operation . READ ,
user . getId ( ) , user ) ;
UserCredentials userCredentials = userService . findUserCredentialsByUserId ( getCurrentUser ( ) . getTenantId ( ) , user . getId ( ) ) ;
if ( ! userCredentials . isEnabled ( ) & & userCredentials . getActivateToken ( ) ! = null ) {
String baseUrl = systemSecurityService . getBaseUrl ( getTenantId ( ) , getCurrentUser ( ) . getCustomerId ( ) , request ) ;
String activateUrl = String . format ( ACTIVATE_URL_PATTERN , baseUrl ,
userCredentials . getActivateToken ( ) ) ;
mailService . sendActivationEmail ( activateUrl , email ) ;
} else {
throw new ThingsboardException ( "User is already activated!" , ThingsboardErrorCode . BAD_REQUEST_PARAMS ) ;
}
} catch ( Exception e ) {
throw handleException ( e ) ;
User user = checkNotNull ( userService . findUserByEmail ( getCurrentUser ( ) . getTenantId ( ) , email ) ) ;
accessControlService . checkPermission ( getCurrentUser ( ) , Resource . USER , Operation . READ ,
user . getId ( ) , user ) ;
UserCredentials userCredentials = userService . findUserCredentialsByUserId ( getCurrentUser ( ) . getTenantId ( ) , user . getId ( ) ) ;
if ( ! userCredentials . isEnabled ( ) & & userCredentials . getActivateToken ( ) ! = null ) {
String baseUrl = systemSecurityService . getBaseUrl ( getTenantId ( ) , getCurrentUser ( ) . getCustomerId ( ) , request ) ;
String activateUrl = String . format ( ACTIVATE_URL_PATTERN , baseUrl ,
userCredentials . getActivateToken ( ) ) ;
mailService . sendActivationEmail ( activateUrl , email ) ;
} else {
throw new ThingsboardException ( "User is already activated!" , ThingsboardErrorCode . BAD_REQUEST_PARAMS ) ;
}
}
@ -250,21 +254,17 @@ public class UserController extends BaseController {
@PathVariable ( USER_ID ) String strUserId ,
HttpServletRequest request ) throws ThingsboardException {
checkParameter ( USER_ID , strUserId ) ;
try {
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
User user = checkUserId ( userId , Operation . READ ) ;
SecurityUser authUser = getCurrentUser ( ) ;
UserCredentials userCredentials = userService . findUserCredentialsByUserId ( authUser . getTenantId ( ) , user . getId ( ) ) ;
if ( ! userCredentials . isEnabled ( ) & & userCredentials . getActivateToken ( ) ! = null ) {
String baseUrl = systemSecurityService . getBaseUrl ( getTenantId ( ) , getCurrentUser ( ) . getCustomerId ( ) , request ) ;
String activateUrl = String . format ( ACTIVATE_URL_PATTERN , baseUrl ,
userCredentials . getActivateToken ( ) ) ;
return activateUrl ;
} else {
throw new ThingsboardException ( "User is already activated!" , ThingsboardErrorCode . BAD_REQUEST_PARAMS ) ;
}
} catch ( Exception e ) {
throw handleException ( e ) ;
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
User user = checkUserId ( userId , Operation . READ ) ;
SecurityUser authUser = getCurrentUser ( ) ;
UserCredentials userCredentials = userService . findUserCredentialsByUserId ( authUser . getTenantId ( ) , user . getId ( ) ) ;
if ( ! userCredentials . isEnabled ( ) & & userCredentials . getActivateToken ( ) ! = null ) {
String baseUrl = systemSecurityService . getBaseUrl ( getTenantId ( ) , getCurrentUser ( ) . getCustomerId ( ) , request ) ;
String activateUrl = String . format ( ACTIVATE_URL_PATTERN , baseUrl ,
userCredentials . getActivateToken ( ) ) ;
return activateUrl ;
} else {
throw new ThingsboardException ( "User is already activated!" , ThingsboardErrorCode . BAD_REQUEST_PARAMS ) ;
}
}
@ -303,16 +303,12 @@ public class UserController extends BaseController {
@RequestParam ( required = false ) String sortProperty ,
@ApiParam ( value = SORT_ORDER_DESCRIPTION , allowableValues = SORT_ORDER_ALLOWABLE_VALUES )
@RequestParam ( required = false ) String sortOrder ) throws ThingsboardException {
try {
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
SecurityUser currentUser = getCurrentUser ( ) ;
if ( Authority . TENANT_ADMIN . equals ( currentUser . getAuthority ( ) ) ) {
return checkNotNull ( userService . findUsersByTenantId ( currentUser . getTenantId ( ) , pageLink ) ) ;
} else {
return checkNotNull ( userService . findCustomerUsers ( currentUser . getTenantId ( ) , currentUser . getCustomerId ( ) , pageLink ) ) ;
}
} catch ( Exception e ) {
throw handleException ( e ) ;
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
SecurityUser currentUser = getCurrentUser ( ) ;
if ( Authority . TENANT_ADMIN . equals ( currentUser . getAuthority ( ) ) ) {
return checkNotNull ( userService . findUsersByTenantId ( currentUser . getTenantId ( ) , pageLink ) ) ;
} else {
return checkNotNull ( userService . findCustomerUsers ( currentUser . getTenantId ( ) , currentUser . getCustomerId ( ) , pageLink ) ) ;
}
}
@ -373,13 +369,9 @@ public class UserController extends BaseController {
@ApiParam ( value = SORT_ORDER_DESCRIPTION , allowableValues = SORT_ORDER_ALLOWABLE_VALUES )
@RequestParam ( required = false ) String sortOrder ) throws ThingsboardException {
checkParameter ( "tenantId" , strTenantId ) ;
try {
TenantId tenantId = TenantId . fromUUID ( toUUID ( strTenantId ) ) ;
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
return checkNotNull ( userService . findTenantAdmins ( tenantId , pageLink ) ) ;
} catch ( Exception e ) {
throw handleException ( e ) ;
}
TenantId tenantId = TenantId . fromUUID ( toUUID ( strTenantId ) ) ;
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
return checkNotNull ( userService . findTenantAdmins ( tenantId , pageLink ) ) ;
}
@ApiOperation ( value = "Get Customer Users (getCustomerUsers)" ,
@ -401,15 +393,11 @@ public class UserController extends BaseController {
@ApiParam ( value = SORT_ORDER_DESCRIPTION , allowableValues = SORT_ORDER_ALLOWABLE_VALUES )
@RequestParam ( required = false ) String sortOrder ) throws ThingsboardException {
checkParameter ( "customerId" , strCustomerId ) ;
try {
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
checkCustomerId ( customerId , Operation . READ ) ;
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;
return checkNotNull ( userService . findCustomerUsers ( tenantId , customerId , pageLink ) ) ;
} catch ( Exception e ) {
throw handleException ( e ) ;
}
CustomerId customerId = new CustomerId ( toUUID ( strCustomerId ) ) ;
checkCustomerId ( customerId , Operation . READ ) ;
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;
return checkNotNull ( userService . findCustomerUsers ( tenantId , customerId , pageLink ) ) ;
}
@ApiOperation ( value = "Enable/Disable User credentials (setUserCredentialsEnabled)" ,
@ -423,28 +411,73 @@ public class UserController extends BaseController {
@ApiParam ( value = "Disable (\"true\") or enable (\"false\") the credentials." , defaultValue = "true" )
@RequestParam ( required = false , defaultValue = "true" ) boolean userCredentialsEnabled ) throws ThingsboardException {
checkParameter ( USER_ID , strUserId ) ;
try {
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
User user = checkUserId ( userId , Operation . WRITE ) ;
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;
userService . setUserCredentialsEnabled ( tenantId , userId , userCredentialsEnabled ) ;
UserId userId = new UserId ( toUUID ( strUserId ) ) ;
User user = checkUserId ( userId , Operation . WRITE ) ;
TenantId tenantId = getCurrentUser ( ) . getTenantId ( ) ;
userService . setUserCredentialsEnabled ( tenantId , userId , userCredentialsEnabled ) ;
if ( ! userCredentialsEnabled ) {
eventPublisher . publishEvent ( new UserCredentialsInvalidationEvent ( userId ) ) ;
}
}
if ( ! userCredentialsEnabled ) {
eventPublisher . publishEvent ( new UserCredentialsInvalidationEvent ( userId ) ) ;
@ApiOperation ( value = "Get usersForAssign (getUsersForAssign)" ,
notes = "Returns page of user data objects that can be assigned to provided alarmId. " +
"Search is been executed by email, firstName and lastName fields. " +
PAGE_DATA_PARAMETERS + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@RequestMapping ( value = "/users/assign/{alarmId}" , params = { "pageSize" , "page" } , method = RequestMethod . GET )
@ResponseBody
public PageData < UserEmailInfo > getUsersForAssign (
@ApiParam ( value = ALARM_ID_PARAM_DESCRIPTION , required = true )
@PathVariable ( "alarmId" ) String strAlarmId ,
@ApiParam ( value = PAGE_SIZE_DESCRIPTION , required = true )
@RequestParam int pageSize ,
@ApiParam ( value = PAGE_NUMBER_DESCRIPTION , required = true )
@RequestParam int page ,
@ApiParam ( value = USER_TEXT_SEARCH_DESCRIPTION )
@RequestParam ( required = false ) String textSearch ,
@ApiParam ( value = SORT_PROPERTY_DESCRIPTION , allowableValues = USER_SORT_PROPERTY_ALLOWABLE_VALUES )
@RequestParam ( required = false ) String sortProperty ,
@ApiParam ( value = SORT_ORDER_DESCRIPTION , allowableValues = SORT_ORDER_ALLOWABLE_VALUES )
@RequestParam ( required = false ) String sortOrder ) throws ThingsboardException {
try {
checkParameter ( "alarmId" , strAlarmId ) ;
AlarmId alarmEntityId = new AlarmId ( toUUID ( strAlarmId ) ) ;
Alarm alarm = checkAlarmId ( alarmEntityId , Operation . READ ) ;
SecurityUser currentUser = getCurrentUser ( ) ;
TenantId tenantId = currentUser . getTenantId ( ) ;
CustomerId originatorCustomerId = entityService . fetchEntityCustomerId ( tenantId , alarm . getOriginator ( ) ) . get ( ) ;
PageLink pageLink = createPageLink ( pageSize , page , textSearch , sortProperty , sortOrder ) ;
PageData < User > pageData ;
if ( Authority . TENANT_ADMIN . equals ( currentUser . getAuthority ( ) ) ) {
if ( alarm . getCustomerId ( ) = = null ) {
pageData = userService . findTenantAdmins ( tenantId , pageLink ) ;
} else {
ArrayList < CustomerId > customerIds = new ArrayList < > ( Collections . singletonList ( new CustomerId ( CustomerId . NULL_UUID ) ) ) ;
if ( ! CustomerId . NULL_UUID . equals ( originatorCustomerId . getId ( ) ) ) {
customerIds . add ( originatorCustomerId ) ;
}
pageData = userService . findUsersByCustomerIds ( tenantId , customerIds , pageLink ) ;
}
} else {
pageData = userService . findCustomerUsers ( tenantId , alarm . getCustomerId ( ) , pageLink ) ;
}
return pageData . mapData ( user - > new UserEmailInfo ( user . getId ( ) , user . getEmail ( ) , user . getFirstName ( ) , user . getLastName ( ) ) ) ;
} catch ( Exception e ) {
throw handleException ( e ) ;
}
}
@ApiOperation ( value = "Save user settings (saveUserSettings)" ,
notes = "Save user settings represented in json format for authorized user. " )
notes = "Save user settings represented in json format for authorized user. " )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
@PostMapping ( value = "/user/settings" )
public JsonNode saveUserSettings ( @RequestBody JsonNode settings ) throws ThingsboardException {
SecurityUser currentUser = getCurrentUser ( ) ;
UserSettings userSettings = new UserSettings ( ) ;
userSettings . setType ( UserSettingsType . GENERAL ) ;
userSettings . setSettings ( settings ) ;
userSettings . setUserId ( currentUser . getId ( ) ) ;
return userSettingsService . saveUserSettings ( currentUser . getTenantId ( ) , userSettings ) . getSettings ( ) ;
@ -458,31 +491,108 @@ public class UserController extends BaseController {
@PutMapping ( value = "/user/settings" )
public void putUserSettings ( @RequestBody JsonNode settings ) throws ThingsboardException {
SecurityUser currentUser = getCurrentUser ( ) ;
userSettingsService . updateUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , settings ) ;
userSettingsService . updateUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , UserSettingsType . GENERAL , settings ) ;
}
@ApiOperation ( value = "Get user settings (getUserSettings)" ,
notes = "Fetch the User settings based on authorized user. " )
notes = "Fetch the User settings based on authorized user. " )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
@GetMapping ( value = "/user/settings" )
public JsonNode getUserSettings ( ) throws ThingsboardException {
SecurityUser currentUser = getCurrentUser ( ) ;
UserSettings userSettings = userSettingsService . findUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) ) ;
return userSettings = = null ? JacksonUtil . newObjectNode ( ) : userSettings . getSettings ( ) ;
UserSettings userSettings = userSettingsService . findUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , UserSettingsType . GENERAL ) ;
return userSettings = = null ? JacksonUtil . newObjectNode ( ) : userSettings . getSettings ( ) ;
}
@ApiOperation ( value = "Delete user settings (deleteUserSettings)" ,
notes = "Delete user settings by specifying list of json element xpaths. \n " +
"Example: to delete B and C element in { \"A\": {\"B\": 5}, \"C\": 15} send A.B,C in jsonPaths request parameter" )
"Example: to delete B and C element in { \"A\": {\"B\": 5}, \"C\": 15} send A.B,C in jsonPaths request parameter" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
@RequestMapping ( value = "/user/settings/{paths}" , method = RequestMethod . DELETE )
public void deleteUserSettings ( @ApiParam ( value = PATHS )
@PathVariable ( PATHS ) String paths ) throws ThingsboardException {
@PathVariable ( PATHS ) String paths ) throws ThingsboardException {
checkParameter ( USER_ID , paths ) ;
SecurityUser currentUser = getCurrentUser ( ) ;
userSettingsService . deleteUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , UserSettingsType . GENERAL , Arrays . asList ( paths . split ( "," ) ) ) ;
}
@ApiOperation ( value = "Update user settings (saveUserSettings)" ,
notes = "Update user settings for authorized user. Only specified json elements will be updated." +
"Example: you have such settings: {A:5, B:{C:10, D:20}}. Updating it with {B:{C:10, D:30}} will result in" +
"{A:5, B:{C:10, D:30}}. The same could be achieved by putting {B.D:30}" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
@PutMapping ( value = "/user/settings/{type}" )
public void putUserSettings ( @ApiParam ( value = "Settings type, case insensitive, one of: \"general\", \"quick_links\", \"doc_links\" or \"dashboards\"." )
@PathVariable ( "type" ) String strType , @RequestBody JsonNode settings ) throws ThingsboardException {
SecurityUser currentUser = getCurrentUser ( ) ;
UserSettingsType type = checkEnumParameter ( "Settings type" , strType , UserSettingsType : : valueOf ) ;
checkNotReserved ( strType , type ) ;
userSettingsService . updateUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , type , settings ) ;
}
@ApiOperation ( value = "Get user settings (getUserSettings)" ,
notes = "Fetch the User settings based on authorized user. " )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
@GetMapping ( value = "/user/settings/{type}" )
public JsonNode getUserSettings ( @ApiParam ( value = "Settings type, case insensitive, one of: \"general\", \"quick_links\", \"doc_links\" or \"dashboards\"." )
@PathVariable ( "type" ) String strType ) throws ThingsboardException {
SecurityUser currentUser = getCurrentUser ( ) ;
UserSettingsType type = checkEnumParameter ( "Settings type" , strType , UserSettingsType : : valueOf ) ;
checkNotReserved ( strType , type ) ;
UserSettings userSettings = userSettingsService . findUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , type ) ;
return userSettings = = null ? JacksonUtil . newObjectNode ( ) : userSettings . getSettings ( ) ;
}
@ApiOperation ( value = "Delete user settings (deleteUserSettings)" ,
notes = "Delete user settings by specifying list of json element xpaths. \n " +
"Example: to delete B and C element in { \"A\": {\"B\": 5}, \"C\": 15} send A.B,C in jsonPaths request parameter" )
@PreAuthorize ( "hasAnyAuthority('SYS_ADMIN', 'TENANT_ADMIN', 'CUSTOMER_USER')" )
@RequestMapping ( value = "/user/settings/{type}/{paths}" , method = RequestMethod . DELETE )
public void deleteUserSettings ( @ApiParam ( value = PATHS )
@PathVariable ( PATHS ) String paths ,
@ApiParam ( value = "Settings type, case insensitive, one of: \"general\", \"quick_links\", \"doc_links\" or \"dashboards\"." )
@PathVariable ( "type" ) String strType ) throws ThingsboardException {
checkParameter ( USER_ID , paths ) ;
UserSettingsType type = checkEnumParameter ( "Settings type" , strType , UserSettingsType : : valueOf ) ;
checkNotReserved ( strType , type ) ;
SecurityUser currentUser = getCurrentUser ( ) ;
userSettingsService . deleteUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , type , Arrays . asList ( paths . split ( "," ) ) ) ;
}
@ApiOperation ( value = "Get information about last visited and starred dashboards (getLastVisitedDashboards)" ,
notes = "Fetch the list of last visited and starred dashboards. Both lists are limited to 10 items." + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@GetMapping ( value = "/user/dashboards" )
public UserDashboardsInfo getUserDashboardsInfo ( ) throws ThingsboardException {
SecurityUser currentUser = getCurrentUser ( ) ;
return userSettingsService . findUserDashboardsInfo ( currentUser . getTenantId ( ) , currentUser . getId ( ) ) ;
}
@ApiOperation ( value = "Report action of User over the dashboard (reportUserDashboardAction)" ,
notes = "Report action of User over the dashboard. " + TENANT_OR_CUSTOMER_AUTHORITY_PARAGRAPH )
@PreAuthorize ( "hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')" )
@RequestMapping ( value = "/user/dashboards/{dashboardId}/{action}" , method = RequestMethod . GET )
@ResponseBody
public UserDashboardsInfo reportUserDashboardAction (
@ApiParam ( value = DASHBOARD_ID_PARAM_DESCRIPTION )
@PathVariable ( DashboardController . DASHBOARD_ID ) String strDashboardId ,
@ApiParam ( value = "Dashboard action, one of: \"visit\", \"star\" or \"unstar\"." )
@PathVariable ( "action" ) String strAction ) throws ThingsboardException {
checkParameter ( DashboardController . DASHBOARD_ID , strDashboardId ) ;
checkParameter ( "action" , strAction ) ;
UserDashboardAction action = checkEnumParameter ( "Action" , strAction , UserDashboardAction : : valueOf ) ;
DashboardId dashboardId = new DashboardId ( toUUID ( strDashboardId ) ) ;
checkDashboardInfoId ( dashboardId , Operation . READ ) ;
SecurityUser currentUser = getCurrentUser ( ) ;
userSettingsService . deleteUserSettings ( currentUser . getTenantId ( ) , currentUser . getId ( ) , Arrays . asList ( paths . split ( "," ) ) ) ;
return userSettingsService . reportUserDashboardAction ( currentUser . getTenantId ( ) , currentUser . getId ( ) , dashboardId , action ) ;
}
private void checkNotReserved ( String strType , UserSettingsType type ) throws ThingsboardException {
if ( type . isReserved ( ) ) {
throw new ThingsboardException ( "Settings with type: " + strType + " are reserved for internal use!" , ThingsboardErrorCode . BAD_REQUEST_PARAMS ) ;
}
}
}