Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add internal flag to companies #1005

Merged
merged 1 commit into from
Jul 29, 2024
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
32 changes: 15 additions & 17 deletions assets/companies/components/CompanyListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
import React from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import {gettext, shortDate, isInPast} from 'utils';
import {getCountryLabel} from '../utils';
import {ICompany, ICountry} from 'interfaces';

function CompanyListItem({company, type, isActive, onClick, showSubscriberId, countries}: any) {
interface IProps {
company: ICompany;
type?: {name: string};
isActive: boolean;
onClick: (id: string) => void;
showSubscriberId: boolean;
countries: Array<ICountry>;
}

export default function CompanyListItem({company, type, isActive, onClick, showSubscriberId, countries}: IProps) {
return (
<tr key={company._id}
className={classNames({'table--selected': isActive, 'table-secondary': !company.is_enabled})}
onClick={() => onClick(company._id)}
tabIndex={0}
data-test-id={`company-list-item--${company._id}`}
>
<td className="name">{company.name}</td>
<td className="name">{company.name} {company.internal ? (
<span className="badge badge--neutral-translucent me-2">{gettext('Internal')}</span>
) : null}</td>
<td className="type">{type ? gettext(type.name) : ''}</td>
{showSubscriberId && <td>{company.sd_subscriber_id}</td>}
<td>{company.account_manager}</td>
Expand All @@ -26,17 +37,4 @@ function CompanyListItem({company, type, isActive, onClick, showSubscriberId, co
<td>{company.expiry_date && shortDate(company.expiry_date.substring(0, 10), false)}</td>
</tr>
);
}

CompanyListItem.propTypes = {
company: PropTypes.object,
type: PropTypes.shape({
name: PropTypes.string.isRequired,
}),
isActive: PropTypes.bool,
onClick: PropTypes.func,
showSubscriberId: PropTypes.bool,
countries: PropTypes.array
};

export default CompanyListItem;
}
8 changes: 8 additions & 0 deletions assets/companies/components/EditCompanyDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ export function EditCompanyDetails({
onChange={onChange}
/>

<CheckboxInput
labelClass={company.internal ? 'text-warning' : ''}
name='internal'
label={gettext('Internal')}
value={company.internal}
onChange={onChange}
/>

</div>
<div className='list-item__preview-footer'>
{company._id && <input
Expand Down
6 changes: 3 additions & 3 deletions assets/interfaces/company.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TDatetime} from './common';
import {IResourceItem, TDatetime} from './common';
import {IUser} from './user';

export interface IAuthProvider {
Expand All @@ -17,8 +17,7 @@ export interface IService {
code: string;
}

export interface ICompany {
_id: string;
export interface ICompany extends IResourceItem {
name: string;
url?: string;
sd_subscriber_id?: string;
Expand Down Expand Up @@ -48,4 +47,5 @@ export interface ICompany {
}>;
auth_domains?: Array<string>;
auth_provider?: IAuthProvider['_id']; // if not defined, system assumes a value of 'newshub'
internal?: boolean;
}
4 changes: 4 additions & 0 deletions newsroom/auth/saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def get_userdata(nameid: str, saml_data: Dict[str, List[str]]) -> UserData:
company = superdesk.get_resource_service("companies").find_one(req=None, auth_domains=domain)
if company is not None:
userdata["company"] = company["_id"]
if not company.get("internal"):
userdata["user_type"] = "public"

# then based on preconfigured saml client
if session.get(SESSION_SAML_CLIENT) and not userdata.get("company"):
Expand All @@ -95,6 +97,8 @@ def get_userdata(nameid: str, saml_data: Dict[str, List[str]]) -> UserData:
)
if company is not None:
userdata["company"] = company["_id"]
if not company.get("internal"):
userdata["user_type"] = "public"

# last option is global env variable
if app.config.get("SAML_COMPANY") and not userdata.get("company"):
Expand Down
1 change: 1 addition & 0 deletions newsroom/companies/companies.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class CompaniesResource(newsroom.Resource):
"auth_provider": {"type": "string"},
"company_size": {"type": "string"},
"referred_by": {"type": "string"},
"internal": {"type": "boolean", "default": False},
}

datasource = {"source": "companies", "default_sort": [("name", 1)]}
Expand Down
10 changes: 9 additions & 1 deletion newsroom/companies/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,15 @@ def get_company_updates(data, original=None):
"auth_provider": data.get("auth_provider") or original.get("auth_provider") or "newshub",
}

for field in ["sections", "archive_access", "events_only", "restrict_coverage_info", "products", "seats"]:
for field in [
"sections",
"archive_access",
"events_only",
"restrict_coverage_info",
"products",
"seats",
"internal",
]:
if field in data:
updates[field] = data[field]

Expand Down
2 changes: 1 addition & 1 deletion newsroom/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class UserDashboardEntry(TypedDict):

class UserRequired(TypedDict):
email: str
user_type: str
user_type: Literal["administrator", "internal", "public", "company_admin", "account_management"]


class UserData(UserRequired, total=False):
Expand Down
13 changes: 13 additions & 0 deletions tests/core/test_saml.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ def test_user_data_with_matching_company(app):
with app.test_request_context():
user_data = get_userdata("foo@example.com", saml_data)
assert user_data.get("company") == company["_id"]
assert user_data.get("user_type") == "public"

user_data = get_userdata("foo@newcomp.com", saml_data)
assert user_data.get("company") is None
assert user_data.get("user_type") == "internal"


def test_user_data_with_matching_preconfigured_client(app, client):
Expand Down Expand Up @@ -52,6 +54,17 @@ def test_user_data_with_matching_preconfigured_client(app, client):

user_data = get_userdata("foo@example.com", saml_data)
assert user_data.get("company") == company["_id"]
assert user_data.get("user_type") == "public"

app.data.update("companies", company["_id"], {"internal": True}, company)

with app.test_client() as c:
resp = c.get("/login/samplecomp")
assert 200 == resp.status_code

user_data = get_userdata("foo@example.com", saml_data)
assert user_data.get("company") == company["_id"]
assert user_data.get("user_type") == "internal"


def test_company_auth_domains(app):
Expand Down
Loading