package org.innovateuk.ifs.finance.security;

import org.innovateuk.ifs.commons.security.PermissionRule;
import org.innovateuk.ifs.commons.security.PermissionRules;
import org.innovateuk.ifs.competition.resource.CompetitionResource;
import org.innovateuk.ifs.finance.domain.GrantClaimMaximum;
import org.innovateuk.ifs.security.BasePermissionRules;
import org.innovateuk.ifs.user.resource.UserResource;
import org.innovateuk.ifs.user.transactional.UsersRolesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static org.innovateuk.ifs.util.SecurityRuleUtil.*;

/**
 * Permission rules for {@link GrantClaimMaximum} for permissioning
 */
@Component
@PermissionRules
public class GrantClaimMaximumPermissionRules extends BasePermissionRules {

    @Autowired
    private UsersRolesService usersRolesService;

    @PermissionRule(value = "MAX_FUNDING_LEVEL_OVERRIDDEN",
            description = "A user can see the grant claim maximums if they are an internal admin user")
    public boolean internalAdminCanCheckMaxFundingLevelOverridden
            (CompetitionResource competition, UserResource user) {
        return isInternalAdmin(user);
    }

    @PermissionRule(value = "MAX_FUNDING_LEVEL_OVERRIDDEN",
        description = "A user can see the grant claim maximums if they have an application for the competition")
    public boolean usersWithApplicationForCompetitionCanCheckMaxFundingLevelOverridden
        (CompetitionResource competition, UserResource user) {
        return usersRolesService.userHasApplicationForCompetition(user.getId(), competition.getId()).getSuccess();
    }

    @PermissionRule(value = "MAX_FUNDING_LEVEL_OVERRIDDEN",
        description = "A user can see the grant claim maximums if they are in a project for the competition")
    public boolean userInAProjectCanCheckMaxFundingLevelOverridden
        (CompetitionResource competition, UserResource user) {
        return projectUserRepository.existsByProjectApplicationCompetitionIdAndUserId(competition.getId(), user.getId());
    }
}
@@ -958,32 +958,33 @@
 description = %22A
+n
  user can see th
@@ -1034,14 +1034,8 @@
 nal 
-admin 
 user
@@ -1195,21 +1195,16 @@
 Internal
-Admin
 (user);%0A

Fix perm