shwstppr commented on a change in pull request #353: URL: https://github.com/apache/cloudstack-primate/pull/353#discussion_r429199025
########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] Review comment: ```suggestion rules: [{ required: true, message: $t('message.error.required.input') }] ``` ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] Review comment: ```suggestion rules: [{ required: true, message: $t('message.error.required.input') }] ``` ########## File path: src/views/iam/RolePermissionTab.vue ########## @@ -227,6 +232,47 @@ export default { this.resetNewFields() this.fetchData() }) + }, + csv ({ data = null, columnDelimiter = ',', lineDelimiter = '\n' }) { Review comment: can we have a better name for method, something like generateRulesCSV, etc ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] + }]" + :placeholder="$t('role.name')" /> + </a-form-item> + + <a-form-item :label="$t('description')"> + <a-input + v-decorator="['description']" + :placeholder="$t('role.description')" /> + </a-form-item> + + <a-form-item :label="$t('type')"> + <a-select + v-decorator="['type', { + rules: [{ required: true, message: 'Please select option'}] Review comment: message.error.required.input can be used ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] + }]" + :placeholder="$t('role.name')" /> + </a-form-item> + + <a-form-item :label="$t('description')"> + <a-input + v-decorator="['description']" + :placeholder="$t('role.description')" /> + </a-form-item> + + <a-form-item :label="$t('type')"> + <a-select + v-decorator="['type', { + rules: [{ required: true, message: 'Please select option'}] + }]" + :placeholder="$t('role.type')"> + <a-select-option value="Admin">Admin</a-select-option> + <a-select-option value="DomainAdmin">DomainAdmin</a-select-option> + <a-select-option value="ResourceAdmin">ResourceAdmin</a-select-option> + <a-select-option value="User">User</a-select-option> Review comment: Do we need to call listRoles API instead or the functionality limited to these roles only? ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] + }]" + :placeholder="$t('role.name')" /> + </a-form-item> + + <a-form-item :label="$t('description')"> + <a-input + v-decorator="['description']" + :placeholder="$t('role.description')" /> + </a-form-item> + + <a-form-item :label="$t('type')"> + <a-select + v-decorator="['type', { + rules: [{ required: true, message: 'Please select option'}] + }]" + :placeholder="$t('role.type')"> + <a-select-option value="Admin">Admin</a-select-option> + <a-select-option value="DomainAdmin">DomainAdmin</a-select-option> + <a-select-option value="ResourceAdmin">ResourceAdmin</a-select-option> + <a-select-option value="User">User</a-select-option> + </a-select> + </a-form-item> + + <a-form-item :label="$t('forced')"> + <a-switch + v-decorator="['forced', { + initialValue: false + }]" /> + </a-form-item> + + <div :span="24" class="action-button"> + <a-button @click="closeAction">{{ this.$t('Cancel') }}</a-button> + <a-button :loading="loading" type="primary" @click="handleSubmit">{{ this.$t('OK') }}</a-button> + </div> + </a-form> + </a-spin> + </div> +</template> + +<script> +import { api } from '@/api' + +export default { + name: 'ImportRole', + props: { + resource: { + type: Object, + required: true + }, + action: { + type: Object, + required: true + } Review comment: Is this used in code? ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] + }]" + :placeholder="$t('role.name')" /> + </a-form-item> + + <a-form-item :label="$t('description')"> + <a-input + v-decorator="['description']" + :placeholder="$t('role.description')" /> Review comment: We can directly show the description of API parameters as placeholders. See, https://github.com/apache/cloudstack-primate/blob/master/src/views/image/RegisterOrUploadTemplate.vue#L66 https://github.com/apache/cloudstack-primate/blob/master/src/views/image/RegisterOrUploadTemplate.vue#L411-L415 ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import Review comment: can be added in the translation file ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> Review comment: can the key here read rules.file or something? Will be more aligned to other translation keys ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] + }]" + :placeholder="$t('role.name')" /> + </a-form-item> + + <a-form-item :label="$t('description')"> + <a-input + v-decorator="['description']" + :placeholder="$t('role.description')" /> + </a-form-item> + + <a-form-item :label="$t('type')"> + <a-select + v-decorator="['type', { + rules: [{ required: true, message: 'Please select option'}] + }]" + :placeholder="$t('role.type')"> + <a-select-option value="Admin">Admin</a-select-option> + <a-select-option value="DomainAdmin">DomainAdmin</a-select-option> + <a-select-option value="ResourceAdmin">ResourceAdmin</a-select-option> + <a-select-option value="User">User</a-select-option> + </a-select> + </a-form-item> + + <a-form-item :label="$t('forced')"> + <a-switch + v-decorator="['forced', { + initialValue: false + }]" /> + </a-form-item> + + <div :span="24" class="action-button"> + <a-button @click="closeAction">{{ this.$t('Cancel') }}</a-button> + <a-button :loading="loading" type="primary" @click="handleSubmit">{{ this.$t('OK') }}</a-button> + </div> + </a-form> + </a-spin> + </div> +</template> + +<script> +import { api } from '@/api' + +export default { + name: 'ImportRole', + props: { + resource: { + type: Object, + required: true + }, + action: { + type: Object, + required: true + } + }, + data () { + return { + fileList: [], + loading: false + } + }, + beforeCreate () { + this.form = this.$form.createForm(this) + }, + methods: { + handleRemove (file) { + const index = this.fileList.indexOf(file) + const newFileList = this.fileList.slice() + newFileList.splice(index, 1) + this.fileList = newFileList + }, + handleChange (info) { + if (info.file.status === 'error') { + this.$notification.error({ + message: 'File Upload Failed', + description: 'File: ' + info.file.name + ' Upload Failed' + }) + } + }, + beforeUpload (file) { + if (file.type !== 'text/csv') { + alert('Only CSV file can be imported') + return false + } + + this.fileList = [file] + return false + }, + handleSubmit (e) { + e.preventDefault() + this.form.validateFields((err, values) => { + if (err) { + return + } + const params = {} + for (const key in values) { + const input = values[key] + if (input === undefined) { + continue + } + if (key === 'file') { + continue + } + + params[key] = input + } + + if (this.fileList.length !== 1) { + return + } + + this.loadCSVFile(this.fileList[0]).then((rules) => { + rules.forEach(function (values, index) { + for (const key in values) { + params['rules[' + index + '].' + key] = values[key] + } + }) + + this.importRole(params) + }) + }) + }, + closeAction () { + this.$emit('close-action') + }, + importRole (params) { + this.loading = true + api('importRole', {}, 'POST', params).then(json => { + const role = json.importroleresponse.role + if (role) { + this.$emit('refresh-data') + this.$notification.success({ + message: 'Import Role', + description: 'Sucessfully imported Role ' + params.name + }) + } + }).catch(error => { + this.$notifyError(error) + }).finally(() => { + this.loading = false + this.closeAction() + }) + }, + csvToJson (csv) { + var lines = csv.split('\n') + var result = [] + var headers = lines[0].split(',') + + lines.map((line, indexLine) => { + if (indexLine < 1) return // Jump header line + var obj = {} + var currentline = line.split(',') + + headers.map((header, indexHeader) => { + obj[header] = currentline[indexHeader] + }) + result.push(obj) + }) + result.pop() // remove the last item because undefined values + + return result + }, + loadCSVFile (file) { + return new Promise((resolve, reject) => { + if (window.FileReader) { + var reader = new FileReader() + reader.onload = (event) => { + var csv = event.target.result + // var lines = csv.split('\n') + // var result = [] + // var headers = lines[0].split(',') + + // lines.map((line, indexLine) => { + // if (indexLine < 1) return + // var obj = {} + // var currentline = line.split(',') + + // headers.map((header, indexHeader) => { + // obj[header] = currentline[indexHeader] + // }) + // result.push(obj) + // }) + // result.pop() + + var rules = this.csvToJson(csv) + resolve(rules) + } + + reader.onerror = (event) => { + if (event.target.error.name === 'NotReadableError') { + alert('Cannot read file!') Review comment: Probably trying to show validation error but we don't show `alert`. Not sure if this can help https://github.com/apache/cloudstack-primate/blob/5ce6d1ca4606add910dd9bd40a410d57306bec78/src/views/offering/AddComputeOffering.vue#L497-L502 ########## File path: src/views/iam/RolePermissionTab.vue ########## @@ -227,6 +232,47 @@ export default { this.resetNewFields() this.fetchData() }) + }, + csv ({ data = null, columnDelimiter = ',', lineDelimiter = '\n' }) { + let result = null + let ctr = null + + if (data === null || !data.length) { + return null + } + + var keys = ['rule', 'permission', 'description'] + + result = '' + result += keys.join(columnDelimiter) + result += lineDelimiter + + data.forEach(item => { + ctr = 0 + keys.forEach(key => { + if (ctr > 0) { + result += columnDelimiter + } + + if (item[key] === undefined) { + item[key] = '' + } + result += typeof item[key] === 'string' && item[key].includes(columnDelimiter) ? `"${item[key]}"` : item[key] + ctr++ + }) + result += lineDelimiter + }) + + return result + }, + exportRolePermissions () { + const csvData = this.csv({ data: this.rules }) + + const hiddenElement = document.createElement('a') Review comment: we need to delete this element? ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] + }]" + :placeholder="$t('role.name')" /> + </a-form-item> + + <a-form-item :label="$t('description')"> + <a-input + v-decorator="['description']" + :placeholder="$t('role.description')" /> + </a-form-item> + + <a-form-item :label="$t('type')"> + <a-select + v-decorator="['type', { + rules: [{ required: true, message: 'Please select option'}] + }]" + :placeholder="$t('role.type')"> + <a-select-option value="Admin">Admin</a-select-option> + <a-select-option value="DomainAdmin">DomainAdmin</a-select-option> + <a-select-option value="ResourceAdmin">ResourceAdmin</a-select-option> + <a-select-option value="User">User</a-select-option> + </a-select> + </a-form-item> + + <a-form-item :label="$t('forced')"> + <a-switch + v-decorator="['forced', { + initialValue: false + }]" /> + </a-form-item> + + <div :span="24" class="action-button"> + <a-button @click="closeAction">{{ this.$t('Cancel') }}</a-button> + <a-button :loading="loading" type="primary" @click="handleSubmit">{{ this.$t('OK') }}</a-button> + </div> + </a-form> + </a-spin> + </div> +</template> + +<script> +import { api } from '@/api' + +export default { + name: 'ImportRole', + props: { + resource: { + type: Object, + required: true + }, + action: { + type: Object, + required: true + } + }, + data () { + return { + fileList: [], + loading: false + } + }, + beforeCreate () { + this.form = this.$form.createForm(this) + }, + methods: { + handleRemove (file) { + const index = this.fileList.indexOf(file) + const newFileList = this.fileList.slice() + newFileList.splice(index, 1) + this.fileList = newFileList + }, + handleChange (info) { + if (info.file.status === 'error') { + this.$notification.error({ + message: 'File Upload Failed', + description: 'File: ' + info.file.name + ' Upload Failed' + }) + } + }, + beforeUpload (file) { + if (file.type !== 'text/csv') { + alert('Only CSV file can be imported') + return false + } + + this.fileList = [file] + return false + }, + handleSubmit (e) { + e.preventDefault() + this.form.validateFields((err, values) => { + if (err) { + return + } + const params = {} + for (const key in values) { + const input = values[key] + if (input === undefined) { + continue + } + if (key === 'file') { + continue + } + + params[key] = input + } + + if (this.fileList.length !== 1) { + return + } + + this.loadCSVFile(this.fileList[0]).then((rules) => { + rules.forEach(function (values, index) { + for (const key in values) { + params['rules[' + index + '].' + key] = values[key] + } + }) + + this.importRole(params) + }) + }) + }, + closeAction () { + this.$emit('close-action') + }, + importRole (params) { + this.loading = true + api('importRole', {}, 'POST', params).then(json => { + const role = json.importroleresponse.role + if (role) { + this.$emit('refresh-data') + this.$notification.success({ + message: 'Import Role', + description: 'Sucessfully imported Role ' + params.name + }) + } + }).catch(error => { + this.$notifyError(error) + }).finally(() => { + this.loading = false + this.closeAction() + }) + }, + csvToJson (csv) { + var lines = csv.split('\n') + var result = [] + var headers = lines[0].split(',') + + lines.map((line, indexLine) => { + if (indexLine < 1) return // Jump header line + var obj = {} + var currentline = line.split(',') + + headers.map((header, indexHeader) => { + obj[header] = currentline[indexHeader] + }) + result.push(obj) + }) + result.pop() // remove the last item because undefined values + + return result + }, + loadCSVFile (file) { + return new Promise((resolve, reject) => { + if (window.FileReader) { + var reader = new FileReader() + reader.onload = (event) => { + var csv = event.target.result + // var lines = csv.split('\n') + // var result = [] + // var headers = lines[0].split(',') + + // lines.map((line, indexLine) => { + // if (indexLine < 1) return + // var obj = {} + // var currentline = line.split(',') + + // headers.map((header, indexHeader) => { + // obj[header] = currentline[indexHeader] + // }) + // result.push(obj) + // }) + // result.pop() + Review comment: To be removed ########## File path: src/views/iam/ImportRole.vue ########## @@ -0,0 +1,267 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +<template> + <div class="form-layout"> + <a-spin :spinning="loading"> + <a-form + :form="form" + @submit="handleSubmit" + layout="vertical"> + <a-form-item :label="$t('rulesFile')"> + <a-upload-dragger + :multiple="false" + :fileList="fileList" + :remove="handleRemove" + :beforeUpload="beforeUpload" + @change="handleChange" + v-decorator="['file', { + rules: [{ required: true, message: 'Please enter input' }] + }]"> + <p class="ant-upload-drag-icon"> + <a-icon type="cloud-upload" /> + </p> + <p class="ant-upload-text" v-if="fileList.length === 0"> + Click or drag rule defintions CVS file to import + </p> + </a-upload-dragger> + </a-form-item> + <a-form-item :label="$t('name')"> + <a-input + v-decorator="['name', { + rules: [{ required: true, message: 'Please enter input' }] + }]" + :placeholder="$t('role.name')" /> + </a-form-item> + + <a-form-item :label="$t('description')"> + <a-input + v-decorator="['description']" + :placeholder="$t('role.description')" /> + </a-form-item> + + <a-form-item :label="$t('type')"> + <a-select + v-decorator="['type', { + rules: [{ required: true, message: 'Please select option'}] + }]" + :placeholder="$t('role.type')"> + <a-select-option value="Admin">Admin</a-select-option> + <a-select-option value="DomainAdmin">DomainAdmin</a-select-option> + <a-select-option value="ResourceAdmin">ResourceAdmin</a-select-option> + <a-select-option value="User">User</a-select-option> + </a-select> + </a-form-item> + + <a-form-item :label="$t('forced')"> + <a-switch + v-decorator="['forced', { + initialValue: false + }]" /> + </a-form-item> + + <div :span="24" class="action-button"> + <a-button @click="closeAction">{{ this.$t('Cancel') }}</a-button> + <a-button :loading="loading" type="primary" @click="handleSubmit">{{ this.$t('OK') }}</a-button> + </div> + </a-form> + </a-spin> + </div> +</template> + +<script> +import { api } from '@/api' + +export default { + name: 'ImportRole', + props: { + resource: { + type: Object, + required: true + }, + action: { + type: Object, + required: true + } + }, + data () { + return { + fileList: [], + loading: false + } + }, + beforeCreate () { + this.form = this.$form.createForm(this) + }, + methods: { + handleRemove (file) { + const index = this.fileList.indexOf(file) + const newFileList = this.fileList.slice() + newFileList.splice(index, 1) + this.fileList = newFileList + }, + handleChange (info) { + if (info.file.status === 'error') { + this.$notification.error({ + message: 'File Upload Failed', + description: 'File: ' + info.file.name + ' Upload Failed' + }) + } + }, + beforeUpload (file) { + if (file.type !== 'text/csv') { + alert('Only CSV file can be imported') + return false + } + + this.fileList = [file] + return false + }, + handleSubmit (e) { + e.preventDefault() + this.form.validateFields((err, values) => { + if (err) { + return + } + const params = {} + for (const key in values) { + const input = values[key] + if (input === undefined) { + continue + } + if (key === 'file') { + continue + } + + params[key] = input + } + + if (this.fileList.length !== 1) { + return + } + + this.loadCSVFile(this.fileList[0]).then((rules) => { + rules.forEach(function (values, index) { + for (const key in values) { + params['rules[' + index + '].' + key] = values[key] + } + }) + + this.importRole(params) + }) + }) + }, + closeAction () { + this.$emit('close-action') + }, + importRole (params) { + this.loading = true + api('importRole', {}, 'POST', params).then(json => { + const role = json.importroleresponse.role + if (role) { + this.$emit('refresh-data') + this.$notification.success({ + message: 'Import Role', + description: 'Sucessfully imported Role ' + params.name + }) + } + }).catch(error => { + this.$notifyError(error) + }).finally(() => { + this.loading = false + this.closeAction() + }) + }, + csvToJson (csv) { + var lines = csv.split('\n') + var result = [] + var headers = lines[0].split(',') + + lines.map((line, indexLine) => { + if (indexLine < 1) return // Jump header line + var obj = {} + var currentline = line.split(',') + + headers.map((header, indexHeader) => { + obj[header] = currentline[indexHeader] + }) + result.push(obj) + }) + result.pop() // remove the last item because undefined values + + return result + }, + loadCSVFile (file) { + return new Promise((resolve, reject) => { + if (window.FileReader) { + var reader = new FileReader() + reader.onload = (event) => { + var csv = event.target.result + // var lines = csv.split('\n') + // var result = [] + // var headers = lines[0].split(',') + + // lines.map((line, indexLine) => { + // if (indexLine < 1) return + // var obj = {} + // var currentline = line.split(',') + + // headers.map((header, indexHeader) => { + // obj[header] = currentline[indexHeader] + // }) + // result.push(obj) + // }) + // result.pop() + + var rules = this.csvToJson(csv) + resolve(rules) + } + + reader.onerror = (event) => { + if (event.target.error.name === 'NotReadableError') { + alert('Cannot read file!') + reject(event.target.error) + } + } + + reader.readAsText(file) + } else { + alert('FileReader not supported in this browser.') Review comment: Better use `this.$notification.error` or check if it can be show through validator ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org