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

Improve handling if user does not have access to the server #3982

Merged
merged 10 commits into from
Feb 7, 2022
4 changes: 2 additions & 2 deletions codalab/rest/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

class CompatibleInteger(fields.Integer):
def serialize(self, attr, obj, accessor=None):
"""Overrides change done from 2.10.2->2.10.3 in https://github.com/marshmallow-code/marshmallow/commit/d81cab413e231ec40123020f110a8c0af22163ed.
"""
"""Overrides change done from 2.10.2->2.10.3 in https://github.com/marshmallow-code/marshmallow/commit/d81cab413e231ec40123020f110a8c0af22163ed."""
ret = Field.serialize(self, attr, obj, accessor=accessor)
return self._to_string(ret) if (self.as_string and ret is not None) else ret

Expand Down Expand Up @@ -307,6 +306,7 @@ class UserSchema(Schema):
url = fields.Url(allow_none=True)
date_joined = fields.LocalDateTime("%c")
avatar_id = fields.String(allow_none=True)
has_access = fields.Bool()

class Meta:
type_ = 'users'
Expand Down
4 changes: 3 additions & 1 deletion docs/REST-API-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ Name | Type
`url` | Url
`date_joined` | LocalDateTime
`avatar_id` | String
`has_access` | Boolean
`email` | String
`notifications` | Integer
`time_quota` | Integer
Expand All @@ -145,7 +146,6 @@ Name | Type
`disk_used` | Integer
`last_login` | LocalDateTime
`is_verified` | Boolean
`has_access` | Boolean

## users

Expand All @@ -160,6 +160,7 @@ Name | Type
`url` | Url
`date_joined` | LocalDateTime
`avatar_id` | String
`has_access` | Boolean
`email` | String
`notifications` | Integer
`time_quota` | Integer
Expand Down Expand Up @@ -293,6 +294,7 @@ Name | Type
`url` | Url
`date_joined` | LocalDateTime
`avatar_id` | String
`has_access` | Boolean

## worksheet-items

Expand Down
9 changes: 8 additions & 1 deletion frontend/src/components/Dashboard/NewDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { default as MainPanel } from './MainPanel';
import $ from 'jquery';
import { withRouter } from 'react-router';
import { defaultErrorHandler, getUser, getUsers } from '../../util/apiWrapper';
import ErrorMessage from '../worksheets/ErrorMessage';

/**
* This route page displays the new Dashboard, which is the landing page for all the users.
Expand Down Expand Up @@ -67,7 +68,13 @@ class NewDashboard extends React.Component<{

/** Renderer. */
render() {
if (this.state.userInfo) {
if (this.state.userInfo && !this.state.userInfo.has_access) {
return (
<ErrorMessage
message={'No access to this server, please contact the administrators.'}
/>
);
} else if (this.state.userInfo) {
return (
<div>
<Grid container spacing={30}>
Expand Down
18 changes: 10 additions & 8 deletions tests/unit/rest/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
class UserTest(BaseTestCase):
def test_user_unauthenticated(self):
os.environ["CODALAB_TEST_USER"] = ""
response = self.app.get('/rest/user')
response = self.app.get("/rest/user")
self.assertEqual(response.status_int, 302)
self.assertEqual(
response.headers["Location"],
Expand All @@ -14,7 +14,7 @@ def test_user_unauthenticated(self):

def test_user_authenticated(self):
os.environ["CODALAB_TEST_USER"] = "codalab"
response = self.app.get('/rest/user')
response = self.app.get("/rest/user")
self.assertEqual(response.status_int, 200)
data = response.json["data"]
# These variables can change due to other tests.
Expand All @@ -24,21 +24,23 @@ def test_user_authenticated(self):
del data["attributes"]["last_login"]
del data["attributes"]["disk_quota"]
del data["attributes"]["time_quota"]

self.assertEqual(
data,
{
"type": "users",
"attributes": {
"email": "",
"first_name": "",
"parallel_run_quota": 100,
"last_name": "",
"url": None,
"first_name": "",
"affiliation": "",
"email": "",
"has_access": True,
"notifications": 2,
"user_name": "codalab",
"affiliation": "",
"url": None,
"parallel_run_quota": 100,
"avatar_id": None,
'is_root_user': True,
"is_root_user": True,
},
"id": "0",
},
Expand Down