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
35 changes: 31 additions & 4 deletions src/libs/Navigation/AppNavigator/MainDrawerNavigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import ReportScreen from '../../../pages/home/ReportScreen';
import SidebarScreen from '../../../pages/home/sidebar/SidebarScreen';
import BaseDrawerNavigator from './BaseDrawerNavigator';
import * as ReportUtils from '../../ReportUtils';
import * as PolicyUtils from '../../PolicyUtils';
import CONST from '../../../CONST';

const propTypes = {
/** Available reports that would be displayed in this navigator */
Expand All @@ -22,6 +24,15 @@ const propTypes = {

/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),

/** The policies which the user has access to */
policies: PropTypes.objectOf(PropTypes.shape({
/** The policy name */
name: PropTypes.string,

/** The type of the policy */
type: PropTypes.string,
})).isRequired,
};

const defaultProps = {
Expand All @@ -33,19 +44,32 @@ const defaultProps = {
* Get the most recently accessed report for the user
*
* @param {Object} reports
* @param {Boolean} [ignoreDefaultRooms]
* @param {String[]} [reportTypesToIgnore]
* @returns {Object}
*/
const getInitialReportScreenParams = (reports, ignoreDefaultRooms) => {
const last = ReportUtils.findLastAccessedReport(reports, ignoreDefaultRooms);
const getInitialReportScreenParams = (reports, reportTypesToIgnore) => {
const last = ReportUtils.findLastAccessedReport(reports, reportTypesToIgnore);

// Fallback to empty if for some reason reportID cannot be derived - prevents the app from crashing
const reportID = lodashGet(last, 'reportID', '');
return {reportID: String(reportID)};
};

const MainDrawerNavigator = (props) => {
const initialParams = getInitialReportScreenParams(props.reports, !Permissions.canUseDefaultRooms(props.betas));
// If one is a member of a free policy, then they are allowed to see the Policy default rooms.
// For everyone else, one must be on the beta to see a default room.
let reportTypesToIgnore = [];
const isMemberOfFreePolicy = PolicyUtils.isMemberOfFreePolicy(props.policies);
if (isMemberOfFreePolicy && !Permissions.canUseDefaultRooms(props.betas)) {
reportTypesToIgnore = [CONST.REPORT.CHAT_TYPE.DOMAIN_ALL];
} else if (!Permissions.canUseDefaultRooms(props.betas)) {
reportTypesToIgnore = [
CONST.REPORT.CHAT_TYPE.POLICY_ADMINS,
CONST.REPORT.CHAT_TYPE.POLICY_ANNOUNCE,
CONST.REPORT.CHAT_TYPE.DOMAIN_ALL,
];
}
const initialParams = getInitialReportScreenParams(props.reports, reportTypesToIgnore);

// Wait until reports are fetched and there is a reportID in initialParams
if (!initialParams.reportID) {
Expand Down Expand Up @@ -81,5 +105,8 @@ export default withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
})(MainDrawerNavigator);
export {getInitialReportScreenParams};
10 changes: 9 additions & 1 deletion src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import * as ReportUtils from './ReportUtils';
import * as Localize from './Localize';
import Permissions from './Permissions';
import * as CollectionUtils from './CollectionUtils';
import * as PolicyUtils from './PolicyUtils';

/**
* OptionsListUtils is used to build a list options passed to the OptionsList component. Several different UI views can
Expand Down Expand Up @@ -439,7 +440,14 @@ function getOptions(reports, personalDetails, activeReportID, {
return;
}

if (ReportUtils.isDefaultRoom(report) && !Permissions.canUseDefaultRooms(betas)) {
// If one is a member of a free policy, then they are allowed to see the Policy default rooms.
// For everyone else, one must be on the beta to see a default room.
const isMemberOfFreePolicy = PolicyUtils.isMemberOfFreePolicy(policies);
if (isMemberOfFreePolicy && !Permissions.canUseDefaultRooms(betas)) {
if (ReportUtils.isDomainRoom(report)) {
return;
}
} else if (ReportUtils.isDefaultRoom(report) && !Permissions.canUseDefaultRooms(betas)) {
return;
}

Expand Down
30 changes: 30 additions & 0 deletions src/libs/PolicyUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import _ from 'underscore';
import CONST from '../CONST';

/**
* Is the user an admin of a free policy (aka workspace)?
*
* @param {Array} policies
* @returns {Boolean}
*/
function isAdminOfFreePolicy(policies) {
return _.some(policies, policy => policy
&& policy.type === CONST.POLICY.TYPE.FREE
&& policy.role === CONST.POLICY.ROLE.ADMIN);
}

/**
* Is the user a member of a free policy (aka workspace)?
*
* @param {Array} policies
* @returns {Boolean}
*/
function isMemberOfFreePolicy(policies) {
return _.some(policies, policy => policy
&& policy.type === CONST.POLICY.TYPE.FREE);
}

export {
isAdminOfFreePolicy,
isMemberOfFreePolicy,
};
9 changes: 5 additions & 4 deletions src/libs/ReportUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,14 @@ function isChatRoom(report) {
* Given a collection of reports returns the most recently accessed one
*
* @param {Record<String, {lastVisitedTimestamp, reportID}>|Array<{lastVisitedTimestamp, reportID}>} reports
* @param {Boolean} [ignoreDefaultRooms]
* @param {String[]} [reportTypesToIgnore]
* @returns {Object}
*/
function findLastAccessedReport(reports, ignoreDefaultRooms) {
function findLastAccessedReport(reports, reportTypesToIgnore) {
let sortedReports = sortReportsByLastVisited(reports);

if (ignoreDefaultRooms) {
sortedReports = _.filter(sortedReports, report => !isDefaultRoom(report));
if (reportTypesToIgnore) {
sortedReports = _.filter(sortedReports, report => !_.contains(reportTypesToIgnore, lodashGet(report, ['chatType'], '')));
}

return _.last(sortedReports);
Expand Down Expand Up @@ -512,6 +512,7 @@ export {
isDefaultRoom,
isAdminRoom,
isAnnounceRoom,
isDomainRoom,
isUserCreatedPolicyRoom,
isChatRoom,
getChatRoomSubtitle,
Expand Down
13 changes: 0 additions & 13 deletions src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,18 +267,6 @@ function loadFullPolicy(policyID) {
});
}

/**
* Is the user an admin of a free policy (aka workspace)?
*
* @param {Array} policies
* @returns {Boolean}
*/
function isAdminOfFreePolicy(policies) {
return _.some(policies, policy => policy
&& policy.type === CONST.POLICY.TYPE.FREE
&& policy.role === CONST.POLICY.ROLE.ADMIN);
}

/**
* Remove the passed members from the policy employeeList
*
Expand Down Expand Up @@ -550,7 +538,6 @@ export {
loadFullPolicy,
removeMembers,
invite,
isAdminOfFreePolicy,
create,
uploadAvatar,
update,
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/Welcome.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import lodashGet from 'lodash/get';
import Navigation from '../Navigation/Navigation';
import * as ReportUtils from '../ReportUtils';
import ROUTES from '../../ROUTES';
import * as Policy from './Policy';
import ONYXKEYS from '../../ONYXKEYS';
import NameValuePair from './NameValuePair';
import CONST from '../../CONST';
import SCREENS from '../../SCREENS';
import * as PolicyUtils from '../PolicyUtils';

let resolveIsReadyPromise;
let isReadyPromise = new Promise((resolve) => {
Expand Down Expand Up @@ -130,7 +130,7 @@ function show({routes, showCreateMenu}) {

// If user is not already an admin of a free policy and we are not navigating them to their workspace or creating a new workspace via workspace/new then
// we will show the create menu.
if (!Policy.isAdminOfFreePolicy(allPolicies) && !isDisplayingWorkspaceRoute) {
if (!PolicyUtils.isAdminOfFreePolicy(allPolicies) && !isDisplayingWorkspaceRoute) {
showCreateMenu();
}
});
Expand Down
22 changes: 21 additions & 1 deletion src/pages/home/ReportScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import FullScreenLoadingIndicator from '../../components/FullscreenLoadingIndica
import reportActionPropTypes from './report/reportActionPropTypes';
import ArchivedReportFooter from '../../components/ArchivedReportFooter';
import toggleReportActionComposeView from '../../libs/toggleReportActionComposeView';
import * as PolicyUtils from '../../libs/PolicyUtils';

const propTypes = {
/** Navigation route context info provided by react navigation */
Expand Down Expand Up @@ -65,6 +66,15 @@ const propTypes = {

/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),

/** The policies which the user has access to */
policies: PropTypes.objectOf(PropTypes.shape({
/** The policy name */
name: PropTypes.string,

/** The type of the policy */
type: PropTypes.string,
})).isRequired,
};

const defaultProps = {
Expand Down Expand Up @@ -185,7 +195,14 @@ class ReportScreen extends React.Component {
return null;
}

if (!Permissions.canUseDefaultRooms(this.props.betas) && ReportUtils.isDefaultRoom(this.props.report)) {
// If one is a member of a free policy, then they are allowed to see the Policy default rooms.
// For everyone else, one must be on the beta to see a default room.
const isMemberOfFreePolicy = PolicyUtils.isMemberOfFreePolicy(this.props.policies);
if (isMemberOfFreePolicy && !Permissions.canUseDefaultRooms(this.props.betas)) {
if (ReportUtils.isDomainRoom(this.props.report)) {
return null;
}
} else if (ReportUtils.isDefaultRoom(this.props.report) && !Permissions.canUseDefaultRooms(this.props.betas)) {
return null;
}

Expand Down Expand Up @@ -276,4 +293,7 @@ export default withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
})(ReportScreen);
3 changes: 2 additions & 1 deletion src/pages/home/sidebar/SidebarScreen/BaseSidebarScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import * as Policy from '../../../../libs/actions/Policy';
import Performance from '../../../../libs/Performance';
import * as Welcome from '../../../../libs/actions/Welcome';
import {sidebarPropTypes, sidebarDefaultProps} from './sidebarPropTypes';
import * as PolicyUtils from '../../../../libs/PolicyUtils';

const propTypes = {

Expand Down Expand Up @@ -163,7 +164,7 @@ class BaseSidebarScreen extends Component {
onSelected: () => Navigation.navigate(ROUTES.IOU_BILL),
},
] : []),
...(!this.props.isCreatingWorkspace && !Policy.isAdminOfFreePolicy(this.props.allPolicies) ? [
...(!this.props.isCreatingWorkspace && !PolicyUtils.isAdminOfFreePolicy(this.props.allPolicies) ? [
{
icon: Expensicons.NewWorkspace,
iconWidth: 46,
Expand Down