UNPKG

files.com

Version:
311 lines (237 loc) 12.3 kB
/* eslint-disable no-unused-vars */ import Api from '../Api' import * as errors from '../Errors' import { getType, isArray, isInt, isObject, isString, } from '../utils' /* eslint-enable no-unused-vars */ /** * Class Group */ class Group { attributes = {} options = {} constructor(attributes = {}, options = {}) { Object.entries(attributes).forEach(([key, value]) => { const normalizedKey = key.replace('?', '') this.attributes[normalizedKey] = value Object.defineProperty(this, normalizedKey, { value, writable: false }) }) this.options = { ...options } } isLoaded = () => !!this.attributes.id // int64 # Group ID getId = () => this.attributes.id setId = value => { this.attributes.id = value } // string # Group name getName = () => this.attributes.name setName = value => { this.attributes.name = value } // string # A list of allowed IPs if applicable. Newline delimited getAllowedIps = () => this.attributes.allowed_ips setAllowedIps = value => { this.attributes.allowed_ips = value } // string # Comma-delimited list of user IDs who are group administrators (separated by commas) getAdminIds = () => this.attributes.admin_ids setAdminIds = value => { this.attributes.admin_ids = value } // string # Notes about this group getNotes = () => this.attributes.notes setNotes = value => { this.attributes.notes = value } // string # Comma-delimited list of user IDs who belong to this group (separated by commas) getUserIds = () => this.attributes.user_ids setUserIds = value => { this.attributes.user_ids = value } // string # Comma-delimited list of usernames who belong to this group (separated by commas) getUsernames = () => this.attributes.usernames setUsernames = value => { this.attributes.usernames = value } // boolean # If true, users in this group can use FTP to login. This will override a false value of `ftp_permission` on the user level. getFtpPermission = () => this.attributes.ftp_permission setFtpPermission = value => { this.attributes.ftp_permission = value } // boolean # If true, users in this group can use SFTP to login. This will override a false value of `sftp_permission` on the user level. getSftpPermission = () => this.attributes.sftp_permission setSftpPermission = value => { this.attributes.sftp_permission = value } // boolean # If true, users in this group can use WebDAV to login. This will override a false value of `dav_permission` on the user level. getDavPermission = () => this.attributes.dav_permission setDavPermission = value => { this.attributes.dav_permission = value } // boolean # If true, users in this group can use the REST API to login. This will override a false value of `restapi_permission` on the user level. getRestapiPermission = () => this.attributes.restapi_permission setRestapiPermission = value => { this.attributes.restapi_permission = value } // int64 # Site ID getSiteId = () => this.attributes.site_id setSiteId = value => { this.attributes.site_id = value } // Parameters: // notes - string - Group notes. // user_ids - string - A list of user ids. If sent as a string, should be comma-delimited. // admin_ids - string - A list of group admin user ids. If sent as a string, should be comma-delimited. // ftp_permission - boolean - If true, users in this group can use FTP to login. This will override a false value of `ftp_permission` on the user level. // sftp_permission - boolean - If true, users in this group can use SFTP to login. This will override a false value of `sftp_permission` on the user level. // dav_permission - boolean - If true, users in this group can use WebDAV to login. This will override a false value of `dav_permission` on the user level. // restapi_permission - boolean - If true, users in this group can use the REST API to login. This will override a false value of `restapi_permission` on the user level. // allowed_ips - string - A list of allowed IPs if applicable. Newline delimited // name - string - Group name. update = async (params = {}) => { if (!this.attributes.id) { throw new errors.EmptyPropertyError('Current object has no id') } if (!isObject(params)) { throw new errors.InvalidParameterError(`Bad parameter: params must be of type object, received ${getType(params)}`) } params.id = this.attributes.id if (params.id && !isInt(params.id)) { throw new errors.InvalidParameterError(`Bad parameter: id must be of type Int, received ${getType(params.id)}`) } if (params.notes && !isString(params.notes)) { throw new errors.InvalidParameterError(`Bad parameter: notes must be of type String, received ${getType(params.notes)}`) } if (params.user_ids && !isString(params.user_ids)) { throw new errors.InvalidParameterError(`Bad parameter: user_ids must be of type String, received ${getType(params.user_ids)}`) } if (params.admin_ids && !isString(params.admin_ids)) { throw new errors.InvalidParameterError(`Bad parameter: admin_ids must be of type String, received ${getType(params.admin_ids)}`) } if (params.allowed_ips && !isString(params.allowed_ips)) { throw new errors.InvalidParameterError(`Bad parameter: allowed_ips must be of type String, received ${getType(params.allowed_ips)}`) } if (params.name && !isString(params.name)) { throw new errors.InvalidParameterError(`Bad parameter: name must be of type String, received ${getType(params.name)}`) } if (!params.id) { if (this.attributes.id) { params.id = this.id } else { throw new errors.MissingParameterError('Parameter missing: id') } } const response = await Api.sendRequest(`/groups/${encodeURIComponent(params.id)}`, 'PATCH', params, this.options) return new Group(response?.data, this.options) } delete = async (params = {}) => { if (!this.attributes.id) { throw new errors.EmptyPropertyError('Current object has no id') } if (!isObject(params)) { throw new errors.InvalidParameterError(`Bad parameter: params must be of type object, received ${getType(params)}`) } params.id = this.attributes.id if (params.id && !isInt(params.id)) { throw new errors.InvalidParameterError(`Bad parameter: id must be of type Int, received ${getType(params.id)}`) } if (!params.id) { if (this.attributes.id) { params.id = this.id } else { throw new errors.MissingParameterError('Parameter missing: id') } } await Api.sendRequest(`/groups/${encodeURIComponent(params.id)}`, 'DELETE', params, this.options) } destroy = (params = {}) => this.delete(params) save = async () => { if (this.attributes.id) { const newObject = await this.update(this.attributes) this.attributes = { ...newObject.attributes } return true } const newObject = await Group.create(this.attributes, this.options) this.attributes = { ...newObject.attributes } return true } // Parameters: // cursor - string - Used for pagination. When a list request has more records available, cursors are provided in the response headers `X-Files-Cursor-Next` and `X-Files-Cursor-Prev`. Send one of those cursor value here to resume an existing list from the next available record. Note: many of our SDKs have iterator methods that will automatically handle cursor-based pagination. // per_page - int64 - Number of records to show per page. (Max: 10,000, 1,000 or less is recommended). // sort_by - object - If set, sort records by the specified field in either `asc` or `desc` direction. Valid fields are `site_id` and `name`. // filter - object - If set, return records where the specified field is equal to the supplied value. Valid fields are `name`. // filter_prefix - object - If set, return records where the specified field is prefixed by the supplied value. Valid fields are `name`. // ids - string - Comma-separated list of group ids to include in results. // include_parent_site_groups - boolean - Include groups from the parent site. static list = async (params = {}, options = {}) => { if (params.cursor && !isString(params.cursor)) { throw new errors.InvalidParameterError(`Bad parameter: cursor must be of type String, received ${getType(params.cursor)}`) } if (params.per_page && !isInt(params.per_page)) { throw new errors.InvalidParameterError(`Bad parameter: per_page must be of type Int, received ${getType(params.per_page)}`) } if (params.ids && !isString(params.ids)) { throw new errors.InvalidParameterError(`Bad parameter: ids must be of type String, received ${getType(params.ids)}`) } const response = await Api.sendRequest('/groups', 'GET', params, options) return response?.data?.map(obj => new Group(obj, options)) || [] } static all = (params = {}, options = {}) => Group.list(params, options) // Parameters: // id (required) - int64 - Group ID. static find = async (id, params = {}, options = {}) => { if (!isObject(params)) { throw new errors.InvalidParameterError(`Bad parameter: params must be of type object, received ${getType(params)}`) } params.id = id if (!params.id) { throw new errors.MissingParameterError('Parameter missing: id') } if (params.id && !isInt(params.id)) { throw new errors.InvalidParameterError(`Bad parameter: id must be of type Int, received ${getType(params.id)}`) } const response = await Api.sendRequest(`/groups/${encodeURIComponent(params.id)}`, 'GET', params, options) return new Group(response?.data, options) } static get = (id, params = {}, options = {}) => Group.find(id, params, options) // Parameters: // notes - string - Group notes. // user_ids - string - A list of user ids. If sent as a string, should be comma-delimited. // admin_ids - string - A list of group admin user ids. If sent as a string, should be comma-delimited. // ftp_permission - boolean - If true, users in this group can use FTP to login. This will override a false value of `ftp_permission` on the user level. // sftp_permission - boolean - If true, users in this group can use SFTP to login. This will override a false value of `sftp_permission` on the user level. // dav_permission - boolean - If true, users in this group can use WebDAV to login. This will override a false value of `dav_permission` on the user level. // restapi_permission - boolean - If true, users in this group can use the REST API to login. This will override a false value of `restapi_permission` on the user level. // allowed_ips - string - A list of allowed IPs if applicable. Newline delimited // name (required) - string - Group name. static create = async (params = {}, options = {}) => { if (!params.name) { throw new errors.MissingParameterError('Parameter missing: name') } if (params.notes && !isString(params.notes)) { throw new errors.InvalidParameterError(`Bad parameter: notes must be of type String, received ${getType(params.notes)}`) } if (params.user_ids && !isString(params.user_ids)) { throw new errors.InvalidParameterError(`Bad parameter: user_ids must be of type String, received ${getType(params.user_ids)}`) } if (params.admin_ids && !isString(params.admin_ids)) { throw new errors.InvalidParameterError(`Bad parameter: admin_ids must be of type String, received ${getType(params.admin_ids)}`) } if (params.allowed_ips && !isString(params.allowed_ips)) { throw new errors.InvalidParameterError(`Bad parameter: allowed_ips must be of type String, received ${getType(params.allowed_ips)}`) } if (params.name && !isString(params.name)) { throw new errors.InvalidParameterError(`Bad parameter: name must be of type String, received ${getType(params.name)}`) } const response = await Api.sendRequest('/groups', 'POST', params, options) return new Group(response?.data, options) } } export default Group module.exports = Group module.exports.default = Group