Skip to content

Commit

Permalink
fix(flow): add flow typing to user permission types
Browse files Browse the repository at this point in the history
  • Loading branch information
landonreed committed Oct 25, 2019
1 parent 1409672 commit 28fe033
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
26 changes: 21 additions & 5 deletions lib/common/user/UserPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,25 @@

import type {Subscription} from './UserSubscriptions'

/**
* The permission types that can be defined in the user profile object
* and which determine a user's role and permissions within the application's
* modules.
*/
export type PermissionType =
// Items related to GTFS editing (NOTE: these end in -gtfs not -feed).
'edit-gtfs' | 'approve-gtfs' |
// Items related to feed source management.
'manage-feed' | 'view-feed' |
// MTC alerts module items
'approve-alert' | 'edit-alert' |
// Privileges related to administration of applications, organizations,
// and projects.
'administer-application' | 'administer-organization' | 'administer-project'

export type Permission = {
feeds?: Array<string>,
type: string
type: PermissionType
}

type Project = {
Expand Down Expand Up @@ -103,7 +119,7 @@ export default class UserPermissions {
this.getOrganizationPermission(orgId, 'administer-organization') != null
}

getOrganizationPermission (organizationId: string, permissionType: string) {
getOrganizationPermission (organizationId: string, permissionType: PermissionType) {
if (!this.hasOrganization(organizationId)) return null
for (const permission of this.getOrganizationPermissions(organizationId)) {
if (permission.type === permissionType) return permission
Expand Down Expand Up @@ -138,13 +154,13 @@ export default class UserPermissions {
return this.projectLookup[projectId].defaultFeeds || []
}

hasProjectPermission (organizationId: ?string, projectId: string, permissionType: string) {
hasProjectPermission (organizationId: ?string, projectId: string, permissionType: PermissionType) {
if (this.isProjectAdmin(projectId, organizationId)) return true
const p = this.getProjectPermission(projectId, permissionType)
return (p !== null)
}

getProjectPermission (projectId: string, permissionType: string): ?Permission {
getProjectPermission (projectId: string, permissionType: PermissionType): ?Permission {
if (!this.hasProject(projectId)) return null
var projectPermissions = this.getProjectPermissions(projectId)
for (const permission of projectPermissions) {
Expand All @@ -159,7 +175,7 @@ export default class UserPermissions {
return null
}

hasFeedPermission (organizationId: ?string, projectId: string, feedId: string, permissionType: string) {
hasFeedPermission (organizationId: ?string, projectId: string, feedId: string, permissionType: PermissionType) {
if (this.isProjectAdmin(projectId, organizationId)) return true
const permission = this.getProjectPermission(projectId, permissionType)
if (permission) {
Expand Down
3 changes: 2 additions & 1 deletion lib/common/util/permissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import {isModuleEnabled} from './config'

import type {PermissionType} from '../user/UserPermissions'
import type {AlertEntity, Feed, Project} from '../../types'
import type {ManagerUserState} from '../../types/reducers'

export function getFeedsForPermission (
project: ?Project,
user: ManagerUserState,
permission: string
permission: PermissionType
): Array<Feed> {
if (project && project.feedSources) {
const {id, organizationId} = project
Expand Down
3 changes: 2 additions & 1 deletion lib/types/reducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
Trip,
UserProfile
} from './'
import type {PermissionType} from '../common/user/UserPermissions'

type FetchStatusSubState = {
fetchStatus: FetchStatus
Expand Down Expand Up @@ -284,7 +285,7 @@ export type FilterState = {
loadedFeeds: Array<any>,
map: MapFilter,
patternFilter: ?string,
permissionFilter: string,
permissionFilter: PermissionType,
project: ?string,
routeFilter: ?string,
routeLimit: number,
Expand Down

0 comments on commit 28fe033

Please sign in to comment.