Skip to content

Commit

Permalink
Disable create service account button if no policy (#1006)
Browse files Browse the repository at this point in the history
* disable button if no policies

* merge fix

* finding group policies

* fixing style

Co-authored-by: Daniel Valdivia <18384552+dvaldivia@users.noreply.github.com>
Co-authored-by: Adam Stafford <adamstafford@Adams-MacBook-Pro.local>
Co-authored-by: Alex <33497058+bexsoft@users.noreply.github.com>
  • Loading branch information
4 people committed Sep 7, 2021
1 parent 13cf3e6 commit 1d88bb4
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions models/user.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion portal-ui/src/screens/Console/Users/UserDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ const UserDetails = ({ classes, match }: IUserDetailsProps) => {
const [changeUserPasswordModalOpen, setChangeUserPasswordModalOpen] =
useState<boolean>(false);
const [deleteOpen, setDeleteOpen] = useState<boolean>(false);
const [hasPolicy, setHasPolicy] = useState<boolean>(false);

const userName = match.params["userName"];

Expand Down Expand Up @@ -188,6 +189,7 @@ const UserDetails = ({ classes, match }: IUserDetailsProps) => {
}
setCurrentPolicies(currentPolicies);
setEnabled(res.status === "enabled");
setHasPolicy(res.hasPolicy)
setLoading(false);
})
.catch((err: ErrorResponseHandler) => {
Expand Down Expand Up @@ -382,7 +384,7 @@ const UserDetails = ({ classes, match }: IUserDetailsProps) => {
/>
</TabPanel>
<TabPanel index={1} value={curTab}>
<UserServiceAccountsPanel user={userName} classes={classes} />
<UserServiceAccountsPanel user={userName} classes={classes} hasPolicy={hasPolicy} />
</TabPanel>
<TabPanel index={2} value={curTab}>
<div className={classes.actionsTray}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ interface IUserServiceAccountsProps {
classes: any;
user: string;
setErrorSnackMessage: typeof setErrorSnackMessage;
hasPolicy: boolean;
}

const styles = (theme: Theme) =>
Expand All @@ -53,6 +54,7 @@ const UserServiceAccountsPanel = ({
classes,
user,
setErrorSnackMessage,
hasPolicy,
}: IUserServiceAccountsProps) => {
const [records, setRecords] = useState<string[]>([]);
const [loading, setLoading] = useState<boolean>(false);
Expand All @@ -75,7 +77,6 @@ const UserServiceAccountsPanel = ({
.invoke("GET", `/api/v1/user/${user}/service-accounts`)
.then((res: string[]) => {
const serviceAccounts = res.sort(stringSort);

setLoading(false);
setRecords(serviceAccounts);
})
Expand Down Expand Up @@ -169,6 +170,7 @@ const UserServiceAccountsPanel = ({
setAddScreenOpen(true);
setSelectedServiceAccount(null);
}}
disabled={!hasPolicy}
>
Create service account
</Button>
Expand Down
26 changes: 25 additions & 1 deletion restapi/admin_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,35 @@ func getUserInfoResponse(session *models.Principal, params admin_api.GetUserInfo
return nil, prepareError(err)
}

var policies []string
if user.PolicyName == "" {
policies = []string{}
} else {
policies = strings.Split(user.PolicyName, ",")
}

hasPolicy := true

if len(policies) == 0 {
hasPolicy = false
for i := 0; i < len(user.MemberOf); i++ {
group, err := adminClient.getGroupDescription(ctx, user.MemberOf[i])
if err != nil {
continue
}
if group.Policy != "" {
hasPolicy = true
break
}
}
}

userInformation := &models.User{
AccessKey: params.Name,
MemberOf: user.MemberOf,
Policy: strings.Split(user.PolicyName, ","),
Policy: policies,
Status: string(user.Status),
HasPolicy: hasPolicy,
}

return userInformation, nil
Expand Down
6 changes: 6 additions & 0 deletions restapi/embedded_spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions swagger-console.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2485,6 +2485,9 @@ definitions:
type: string
status:
type: string
hasPolicy:
type: boolean

listUsersResponse:
type: object
properties:
Expand Down

0 comments on commit 1d88bb4

Please sign in to comment.