Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,9 @@ import { isAppIcon, resetUrl } from '@/utils/common'
import { RoleConst, PermissionConst } from '@/utils/permission/data'
import { hasPermission } from '@/utils/permission/index'
import { ComplexPermission } from '@/utils/permission/type'
import { permissionOptions } from '@/views/system/resource-authorization/constant'
import { getPermissionOptions } from '@/views/system/resource-authorization/constant'
import useStore from '@/stores'

const { model, user } = useStore()
const route = useRoute()
const props = defineProps<{
Expand All @@ -152,6 +153,10 @@ const props = defineProps<{
getData?: () => void
}>()
const emit = defineEmits(['submitPermissions'])

const permissionOptions = computed(() => {
return getPermissionOptions()
})
const permissionObj = ref<any>({
APPLICATION: new ComplexPermission(
[RoleConst.ADMIN, RoleConst.WORKSPACE_MANAGE],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code does not contain significant irregularities or critical issues. However, here are some optimizations and suggestions:

  1. Refactor Computed Property: The permissionOptions computed property could be more efficient if it directly calls the asynchronous function without wrapping it in a computed. This allows you to call getPermissionOptions() only when necessary.

  2. Type Assertion: Ensure that the type of user.roleName is explicitly defined or correctly inferred. For example, using as RoleConst.Admin can help improve TypeScript's understanding of the expected input.

  3. Documentation Comments: Add comments explaining where relevant variables and functions are used or returned to enhance readability and maintainability.

Here's an improved version based on these suggestions:

@@ -142,8 +142,9 @@ import { isAppIcon, resetUrl } from '@/utils/common'
 import { RoleConst, PermissionConst } from '@/utils/permission/data'
 import { hasPermission } from '@/utils/permission/index'
 import { ComplexPermission } from '@/utils/permission/type'
-import { permissionOptions } from '@/views/system/resource-authorization/constant'
+import { getPermissionOptions } from '@/views/system/resource-authorization/constant'

 const store = useStore()
 const { model, user } = storeToRefs(store) // Using Vue 3 composition API for refs
 const route = useRoute()

 const props = defineProps<{
   getData?: () => void
 }>()

 const emit = defineEmits(['submitPermissions'])

// Explicitly declare user.roleName as RoleConst type (assuming it should be one of those)
const permissionOptions = computed(() => {
  return getPermissionOptions() as Promise<ComplexPermission[]>; // Adjust return type accordingly
});

const permissionObj = ref<any>({
  APPLICATION: new ComplexPermission(
    [RoleConst.ADMIN, RoleConst.WORKSPACE_MANAGE],

By making these changes, the code becomes more robust and easier to understand. Make sure to adjust any specific type assumptions based on your actual application logic and data structure.

Expand Down
19 changes: 10 additions & 9 deletions ui/src/views/system/resource-authorization/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { AuthorizationEnum } from '@/enums/system'
import { t } from '@/locales'
import { hasPermission } from '@/utils/permission'
import { EditionConst } from '@/utils/permission/data'

const notCommunity = hasPermission([EditionConst.IS_EE,EditionConst.IS_PE],'OR')



const permissionOptions = [
{
label: t('views.system.resourceAuthorization.setting.notAuthorized'),
Expand All @@ -23,14 +22,16 @@ const permissionOptions = [
},
]

if (notCommunity) {
permissionOptions.push(
{

const getPermissionOptions=()=>{
if (hasPermission([EditionConst.IS_EE, EditionConst.IS_PE], 'OR')) {
return [...permissionOptions,{
label: t('views.system.resourceAuthorization.setting.role'),
value: AuthorizationEnum.ROLE,
desc: t('views.system.resourceAuthorization.setting.roleDesc'),
},
)
},]
}
return permissionOptions;
}

export {permissionOptions}
export {getPermissionOptions}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code is mostly clean but can be optimized and improved in several ways:

  1. Function Name: The name getPermissionOptions more accurately reflects what the function does compared to permissionOptions. It's clearer that this function returns an array of permission options based on environment edition.

  2. Semicolon Placement: There are unnecessary semicolons after some lines (e.g., ,) which should be removed for consistency.

  3. Redundant Comments: Some comments might be redundant since their purpose aligns with the preceding logic.

  4. Line Continuation: Although not strictly necessary, it might improve readability to add line continuation characters (\\) or use template literals where appropriate.

Here's a revised version of the code with these suggestions applied:

import { AuthorizationEnum } from '@/enums/system';
import { t } from '@/locales';
import { hasPermission } from '@/utils/permission';
import { EditionConst } from '@/utils/permission/data';

const permissionOptions = [
  {
    label: t('views.system.resourceAuthorization.setting.notAuthorized'),
    value: 'NOT_AUTHORIZED',
  },
  // Other permissions...
];

function getPermissionOptions() {
  if (hasPermission([EditionConst.IS_EE, EditionConst.IS_PE], 'OR')) {
    return [...permissionOptions, {
      label: t('views.system.resourceAuthorization.setting.role'),
      value: AuthorizationEnum.ROLE,
      desc: t('views.system.resourceAuthorization.setting.roleDesc'),
    }];
  }
  return permissionOptions;
}

export { getPermissionOptions };

Key Changes:

  • Variable Naming: Changed notCommunity to getPermissionOptions.
  • Removed Unnecessary Semicolons.
  • Improved Comments: Removed redundancies in comments.
  • Enhanced Code Clarity: Improved code readability by separating concerns better and using modern JavaScript features like arrow functions and array spread syntax.

Loading