Skip to content

Commit 37ee429

Browse files
committed
refactor: move all permissions in one place
ref issue #2841
1 parent 6ee2e13 commit 37ee429

File tree

90 files changed

+1528
-1344
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+1528
-1344
lines changed

docs/permissions-guide/permissions-guide.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,23 @@ Let's say you would like to add a new place in code where you want to check user
1818

1919
2. After you add a new permission, regenerate [permissions list](https://htmlpreview.github.io/?https://github.com/appirio-tech/connect-app/blob/dev/docs/permissions.html) by running `npm run generate:doc:permissions`.
2020

21-
3. To check if user has permission in code use method `hasPermission(permission)`.
21+
3. To check if logged-in user has permission in code use method `hasPermission(permission)`.
2222

2323
Example:<br>
24-
24+
2525
```js
26-
import PERMISSIONS from 'config/permissions'
26+
import { PERMISSIONS } from 'config/permissions'
2727
import { hasPermission } from 'helpers/permissions'
2828

2929
if (hasPermission(PERMISSIONS.MANAGE_PROJECT_PLAN)) {
3030
...
3131
}
3232
```
3333

34-
- Note, optionally, you may pass the `project` object like this `hasPermission(permission, project)`. But you don't have to as `hasPermission` gets `project` object from the Redux Store (`projectState.project`) automatically. Only in case if you want to check user permission to another project which is not loaded into the Redux Store then you may pass `project` explicitly.
34+
4. If you would like to check permissions for other user (not the current user) or for other project (not the current project) you may pass the second argument `entities: { project?: object, user?: object }`:
35+
- `hasPermission(permission, { project })` - check permissions for another project
36+
- `hasPermission(permission, { user })` - check permissions for another user
37+
- `hasPermission(permission, { project, user })` - check permissions for another project and user
3538

3639
## Roles
3740

@@ -49,4 +52,4 @@ By default every user has one role `Topcoder User`, generally this means that su
4952

5053
When user joins some project and become a member of the project, such a user has one **Project Role** inside that project. One user may have different **Project Role** in different projects. See [the list of all Project Roles](https://github.com/appirio-tech/connect-app/blob/dev/src/config/constants.js#L638-L647) which we use in Connect App.
5154

52-
<img src="./images/project-roles.png" width="720">
55+
<img src="./images/project-roles.png" width="720">

docs/permissions.html

Lines changed: 586 additions & 71 deletions
Large diffs are not rendered by default.

scripts/permissions-doc/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import _ from 'lodash'
1111
import fs from 'fs'
1212
import path from 'path'
1313
import handlebars from 'handlebars'
14-
import PERMISSIONS from '../../src/config/permissions'
14+
import { PERMISSIONS } from '../../src/config/permissions'
1515
import {
1616
PROJECT_ROLE_CUSTOMER,
1717
PROJECT_ROLE_COPILOT,

scripts/permissions-doc/template.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
77
<title>Permissions</title>
88
<style>
9+
body {
10+
padding-bottom: 100px;
11+
}
12+
913
.small-text {
1014
font-size: 62.5%;
1115
line-height: 120%;

src/components/AssetsLibrary/FilesGridView.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import {
2525
PROJECT_FEED_TYPE_MESSAGES
2626
} from '../../config/constants'
2727
import { hasPermission } from '../../helpers/permissions'
28-
import PERMISSIONS from '../../config/permissions'
28+
import { PERMISSIONS } from '../../config/permissions'
2929

3030
let selectedLink
3131
let clearing = false

src/components/AssetsLibrary/LinksGridView.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
} from '../../config/constants'
2323
import FilterColHeader from './FilterColHeader'
2424
import { hasPermission } from '../../helpers/permissions'
25-
import PERMISSIONS from '../../config/permissions'
25+
import { PERMISSIONS } from '../../config/permissions'
2626

2727
let selectedLink
2828
let clearing = false

src/components/Feed/Feed.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import RichTextArea from '../RichTextArea/RichTextArea'
1010
import NotificationsReader from '../../components/NotificationsReader'
1111
import IconButton from '../IconButton/IconButton'
1212

13-
import { EVENT_TYPE, PROJECT_FEED_TYPE_MESSAGES, PROJECT_ROLE_CUSTOMER } from '../../config/constants'
13+
import { EVENT_TYPE, PROJECT_FEED_TYPE_MESSAGES } from '../../config/constants'
1414

1515
import XMarkIcon from '../../assets/icons/x-mark.svg'
1616
import FullscreenIcon from '../../assets/icons/ui-fullscreen.svg'
@@ -20,6 +20,8 @@ import CloseIcon from 'appirio-tech-react-components/components/Icons/CloseIcon'
2020
import MoreIcon from '../../assets/icons/more.svg'
2121

2222
import './Feed.scss'
23+
import { hasPermission } from '../../helpers/permissions'
24+
import { PERMISSIONS } from '../../config/permissions'
2325

2426
class Feed extends React.Component {
2527
constructor(props) {
@@ -95,7 +97,7 @@ class Feed extends React.Component {
9597
}
9698

9799
filterProjectMembers(projectMembers, isPrivate) {
98-
return isPrivate ? _.pickBy(projectMembers, pm => pm.role !== PROJECT_ROLE_CUSTOMER) : projectMembers
100+
return isPrivate ? _.filter(projectMembers, member => hasPermission(PERMISSIONS.ACCESS_PRIVATE_POST, { user: member })) : projectMembers
99101
}
100102

101103
render() {

src/components/IncomepleteUserProfileDialog/IncompleteUserProfileDialog.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const IncompleteUserProfileDialog = ({
4545
IncompleteUserProfileDialog.propTypes = {
4646
profileSettings: PT.object.isRequired,
4747
saveProfileSettings: PT.func.isRequired,
48-
isTopcoderUser: PT.bool.isRequired,
4948
user: PT.object.isRequired,
5049
onCloseDialog: PT.func.isRequired,
5150
title: PT.string.isRequired,

src/components/IncompleteUserProfile/IncompleteUserProfile.jsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
*/
44
import React from 'react'
55
import PT from 'prop-types'
6-
import { PROFILE_FIELDS_CONFIG } from '../../config/constants'
76
import ProfileSettingsForm from '../../routes/settings/routes/profile/components/ProfileSettingsForm'
8-
import { getDefaultTopcoderRole } from '../../helpers/permissions'
7+
import { getDefaultTopcoderRole, hasPermission } from '../../helpers/permissions'
98
import { timezones } from 'appirio-tech-react-components/constants/timezones'
9+
import { getUserProfileFieldsConfig } from '../../helpers/tcHelpers'
10+
import { PERMISSIONS } from '../../config/permissions'
1011

1112
const IncompleteUserProfile = ({
1213
profileSettings,
1314
saveProfileSettings,
14-
isTopcoderUser,
1515
user,
1616
...restProps
1717
}) => {
18-
const fieldsConfig = isTopcoderUser ? PROFILE_FIELDS_CONFIG.TOPCODER : PROFILE_FIELDS_CONFIG.CUSTOMER
18+
const fieldsConfig = getUserProfileFieldsConfig()
1919
// never show avatar
2020
delete fieldsConfig.avatar
2121
// config the form to only show required fields which doesn't have the value yet
@@ -40,7 +40,7 @@ const IncompleteUserProfile = ({
4040
console.log('Auto-detected timezone', prefilledProfileSettings.settings.timeZone)
4141
}
4242

43-
if (isTopcoderUser) {
43+
if (!hasPermission(PERMISSIONS.VIEW_USER_PROFILE_AS_CUSTOMER)) {
4444
// We don't ask Topcoder User for "Company Name" and "Title"
4545
// but server requires them, so if they are not yet defined, we set them automatically
4646
if (!profileSettings.settings.companyName) {
@@ -71,7 +71,6 @@ const IncompleteUserProfile = ({
7171
IncompleteUserProfile.propTypes = {
7272
profileSettings: PT.object.isRequired,
7373
saveProfileSettings: PT.func.isRequired,
74-
isTopcoderUser: PT.bool.isRequired,
7574
user: PT.object.isRequired,
7675
}
7776

src/components/Posts/PostsContainer.jsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class PostsContainer extends React.Component {
6060
* which is accepted by Feed component
6161
*/
6262
prepareFeed() {
63-
const { topic, error, allMembers, currentMemberRole, tag } = this.props
63+
const { topic, error, allMembers, tag } = this.props
6464
const { showAll } = this.state
6565

6666
if (!topic || !tag) {
@@ -73,7 +73,7 @@ class PostsContainer extends React.Component {
7373
// Github issue##623, allow comments on all posts (including system posts)
7474
allowComments: true,
7575
user: isSystemUser(topic.userId) ? SYSTEM_USER : allMembers[topic.userId],
76-
unread: !topic.read && !!currentMemberRole,
76+
unread: !topic.read,
7777
totalComments: topic.totalPosts,
7878
comments: [],
7979
}
@@ -88,7 +88,7 @@ class PostsContainer extends React.Component {
8888
isSavingComment: p.isSavingComment,
8989
isDeletingComment: p.isDeletingComment,
9090
error: p.error,
91-
unread: !p.read && !!currentMemberRole,
91+
unread: !p.read,
9292
date,
9393
createdAt: p.date,
9494
edited,

0 commit comments

Comments
 (0)