<commit_before>package ai.subut.kurjun.web.controllers;


import ai.subut.kurjun.identity.DefaultRelationObject;
import ai.subut.kurjun.model.identity.*;
import ai.subut.kurjun.web.controllers.rest.RestIdentityController;
import ai.subut.kurjun.web.security.AuthorizedUser;
import ai.subut.kurjun.web.service.IdentityManagerService;
import ai.subut.kurjun.web.service.RelationManagerService;

import com.google.inject.Inject;

import ai.subut.kurjun.web.service.RepositoryService;
import ninja.Context;
import ninja.Result;
import ninja.Results;
import ninja.params.Param;
import ninja.params.Params;
import ninja.params.PathParam;
import ninja.session.FlashScope;
import ninja.session.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;


/**
 * Web Controller for Trust Relation Management
 */
public class RelationController extends BaseController
{

    private static final Logger LOGGER = LoggerFactory.getLogger( RelationController.class );

    @Inject
    private RelationManagerService relationManagerService;

    @Inject
    private IdentityManagerService identityManagerService;

    @Inject
    private RepositoryService repositoryService;


    public Result getRelations( Context context )
    {
        //************************************
        UserSession userSession = ( UserSession ) context.getAttribute( "USER_SESSION" );
        relationManagerService.setUserSession( userSession );
        //************************************

        if ( userSession.getUser().equals( identityManagerService.getPublicUser() ) )
        {
            return Results.html().template( "views/relations.ftl" )
                          .render( "relations", null );
        }
        else
        {
                return Results.html().template( "views/relations.ftl" )
                              .render( "relations", relationManagerService.getAllRelations() );
        }

    }


    public Result getRelationsByOwner( @AuthorizedUser UserSession userSession,
                                       @Param( "fingerprint" ) String fingerprint )
    {
        relationManagerService.setUserSession( userSession );
        return Results.html().template( "views/_popup-view-permissions.ftl" ).render( "relations",
                relationManagerService.getTrustRelationsBySource(
                        relationManagerService.toSourceObject( identityManagerService.getUser( fingerprint ) ) ) );
    }


    public Result getRelationsByTarget( @AuthorizedUser UserSession userSession,
                                        @Param( "fingerprint" ) String fingerprint )
    {
        relationManagerService.setUserSession( userSession );
        return Results.html().template( "views/_popup-view-permissions.ftl" ).render( "relations",
                relationManagerService
                        .getTrustRelationsByTarget( relationManagerService.toTargetObject( fingerprint ) ) );
    }


    public Result getRelationsByObject( @AuthorizedUser UserSession userSession, @Param( "id" ) String id,
                                        @Param( "name" ) String name, @Param( "version" ) String version,
                                        @Param( "md5" ) String md5, @Param( "obj_type" ) int objType )
    {
        relationManagerService.setUserSession( userSession );
        RelationObjectType relObjType = RelationObjectType.valueOf( objType );
        relObjType = ( relObjType == null ? RelationObjectType.RepositoryContent : relObjType );
        List<Relation> rels = relationManagerService
                .getTrustRelationsByObject( relationManagerService.toTrustObject( id, null, null, null, relObjType ) );
        //.stream().filter( r -> !r.getSource().getId().equals( r.getTarget().getId() ) ).collect Collectors.toList() );
        return Results.html().template( "views/_popup-view-permissions.ftl" ).render( "relations", rels );
    }


    public Result getAddTrustRelationForm( @AuthorizedUser UserSession userSession )
    {
        repositoryService.setUserSession( userSession );
        List<String> repos = repositoryService.getRepositories();

        return Results.html().template( "views/_popup-add-trust-rel.ftl" ).render( "repos", repos );
    }


    public Result addTrustRelation( @AuthorizedUser UserSession userSession,
                                    @Param( "target_fprint" ) String targetFprint,
                                    @Param( "trust_obj_type" ) int trustObjType,
                                    @Param( "template_id" ) String templateId, @Param( "repo" ) String repo,
                                    @Params( "permission" ) String[] permissions, Context context,
                                    FlashScope flashScope )
    {

        if ( userSession.getUser().equals( identityManagerService.getPublicUser() ) )
        {
            return Results.notFound();
        }
        else
        {
            //UserSession userSession = (UserSession ) context.getAttribute( "USER_SESSION" );
            RelationObject owner = relationManagerService.toSourceObject( userSession.getUser() );
            RelationObject target = relationManagerService.toTargetObject( targetFprint );
            RelationObject trustObject = null;

            if ( trustObjType == RelationObjectType.RepositoryContent.getId() )
            {
                trustObject = new DefaultRelationObject();
                trustObject.setId( templateId );
                trustObject.setType( RelationObjectType.RepositoryContent.getId() );
            }
            else
            {
                trustObject = new DefaultRelationObject();
                trustObject.setId( repo );
                trustObject.setType( RelationObjectType.RepositoryTemplate.getId() );
            }
            //trustObject = relationManagerService.toTrustObject( templateId, null, null, null );
            Set<Permission> objectPermissions = new HashSet<>();
            Arrays.asList( permissions ).forEach( p -> objectPermissions.add( Permission.valueOf( p ) ) );


            Relation relation = relationManagerService.addTrustRelation( owner, target, trustObject, objectPermissions );
            if ( relation != null )
            {
                flashScope.success( "Trust relation added." );
            }

            return Results.redirect( context.getContextPath() + "/relations" );
        }
    }


    public Result delete( @PathParam( "id" ) String id, @Param( "source_id" ) String sourceId,
                          @Param( "target_id" ) String targetId, @Param( "object_id" ) String objectId, Context context,
                          FlashScope flashScope )
    {
        UserSession userSession = ( UserSession ) context.getAttribute( "USER_SESSION" );
        Relation rel;

        if ( !StringUtils.isBlank( id ) )
        {
            rel = relationManagerService.getRelation( id );
        }
        else
        {
            rel = relationManagerService.getRelation( sourceId, targetId, objectId, 0 );
        }

        if ( rel != null )
        {
            if ( relationManagerService
                    .checkUserPermissions( userSession, rel.getTrustObject().getId(), rel.getTrustObject().getType() )
                    .contains( Permission.Delete ) )
            {
                relationManagerService.removeRelation( rel );
                flashScope.success( "Deleted successfully." );
            }
            else<commit_msg>Security check when adding trust relation.<commit_after>package ai.subut.kurjun.web.controllers;


import ai.subut.kurjun.identity.DefaultRelationObject;
import ai.subut.kurjun.model.identity.*;
import ai.subut.kurjun.web.controllers.rest.RestIdentityController;
import ai.subut.kurjun.web.security.AuthorizedUser;
import ai.subut.kurjun.web.service.IdentityManagerService;
import ai.subut.kurjun.web.service.RelationManagerService;

import com.google.inject.Inject;

import ai.subut.kurjun.web.service.RepositoryService;
import ninja.Context;
import ninja.Result;
import ninja.Results;
import ninja.params.Param;
import ninja.params.Params;
import ninja.params.PathParam;
import ninja.session.FlashScope;
import ninja.session.Session;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;


/**
 * Web Controller for Trust Relation Management
 */
public class RelationController extends BaseController
{

    private static final Logger LOGGER = LoggerFactory.getLogger( RelationController.class );

    @Inject
    private RelationManagerService relationManagerService;

    @Inject
    private IdentityManagerService identityManagerService;

    @Inject
    private RepositoryService repositoryService;


    public Result getRelations( Context context )
    {
        //************************************
        UserSession userSession = ( UserSession ) context.getAttribute( "USER_SESSION" );
        relationManagerService.setUserSession( userSession );
        //************************************

        if ( userSession.getUser().equals( identityManagerService.getPublicUser() ) )
        {
            return Results.html().template( "views/relations.ftl" )
                          .render( "relations", null );
        }
        else
        {
                return Results.html().template( "views/relations.ftl" )
                              .render( "relations", relationManagerService.getAllRelations() );
        }

    }


    public Result getRelationsByOwner( @AuthorizedUser UserSession userSession,
                                       @Param( "fingerprint" ) String fingerprint )
    {
        relationManagerService.setUserSession( userSession );
        return Results.html().template( "views/_popup-view-permissions.ftl" ).render( "relations",
                relationManagerService.getTrustRelationsBySource(
                        relationManagerService.toSourceObject( identityManagerService.getUser( fingerprint ) ) ) );
    }


    public Result getRelationsByTarget( @AuthorizedUser UserSession userSession,
                                        @Param( "fingerprint" ) String fingerprint )
    {
        relationManagerService.setUserSession( userSession );
        return Results.html().template( "views/_popup-view-permissions.ftl" ).render( "relations",
                relationManagerService
                        .getTrustRelationsByTarget( relationManagerService.toTargetObject( fingerprint ) ) );
    }


    public Result getRelationsByObject( @AuthorizedUser UserSession userSession, @Param( "id" ) String id,
                                        @Param( "name" ) String name, @Param( "version" ) String version,
                                        @Param( "md5" ) String md5, @Param( "obj_type" ) int objType )
    {
        relationManagerService.setUserSession( userSession );
        RelationObjectType relObjType = RelationObjectType.valueOf( objType );
        relObjType = ( relObjType == null ? RelationObjectType.RepositoryContent : relObjType );
        List<Relation> rels = relationManagerService
                .getTrustRelationsByObject( relationManagerService.toTrustObject( id, null, null, null, relObjType ) );
        //.stream().filter( r -> !r.getSource().getId().equals( r.getTarget().getId() ) ).collect Collectors.toList() );
        return Results.html().template( "views/_popup-view-permissions.ftl" ).render( "relations", rels );
    }


    public Result getAddTrustRelationForm( @AuthorizedUser UserSession userSession )
    {
        repositoryService.setUserSession( userSession );
        List<String> repos = repositoryService.getRepositories();

        return Results.html().template( "views/_popup-add-trust-rel.ftl" ).render( "repos", repos );
    }


    public Result addTrustRelation( @AuthorizedUser UserSession userSession,
                                    @Param( "target_fprint" ) String targetFprint,
                                    @Param( "trust_obj_type" ) int trustObjType,
                                    @Param( "template_id" ) String templateId, @Param( "repo" ) String repo,
                                    @Params( "permission" ) String[] permissions, Context context,
                                    FlashScope flashScope )
    {

        if ( userSession.getUser().equals( identityManagerService.getPublicUser() ) )
        {
            return Results.notFound();
        }
        else
        {
            //UserSession userSession = (UserSession ) context.getAttribute( "USER_SESSION" );
            RelationObject owner = relationManagerService.toSourceObject( userSession.getUser() );
            RelationObject target = relationManagerService.toTargetObject( targetFprint );
            RelationObject trustObject = null;

            if ( trustObjType == RelationObjectType.RepositoryContent.getId() )
            {
                trustObject = new DefaultRelationObject();
                trustObject.setId( templateId );
                trustObject.setType( RelationObjectType.RepositoryContent.getId() );
            }
            else
            {
                trustObject = new DefaultRelationObject();
                trustObject.setId( repo );
                trustObject.setType( RelationObjectType.RepositoryTemplate.getId() );
            }
            //trustObject = relationManagerService.toTrustObject( templateId, null, null, null );
            Set<Permission> objectPermissions = new HashSet<>();
            Arrays.asList( permissions ).forEach( p -> objectPermissions.add( Permission.valueOf( p ) ) );

            Set<Permission> userPermissions = relationManagerService.checkUserPermissions( userSession, trustObject.getId(),
                    trustObject.getType() );

            if ( userPermissions.containsAll( objectPermissions ) )
            {
                Relation relation = relationManagerService.addTrustRelation( owner, target, trustObject, objectPermissions );
                if ( relation != null )
                {
                    flashScope.success( "Trust relation added." );
                }
            }
            else {
                flashScope.error( "Access denied. You don't have permissions to this object." );
            }
            
            return Results.redirect( context.getContextPath() + "/relations" );
        }
    }


    public Result delete( @PathParam( "id" ) String id, @Param( "source_id" ) String sourceId,
                          @Param( "target_id" ) String targetId, @Param( "object_id" ) String objectId, Context context,
                          FlashScope flashScope )
    {
        UserSession userSession = ( UserSession ) context.getAttribute( "USER_SESSION" );
        Relation rel;

        if ( !StringUtils.isBlank( id ) )
        {
            rel = relationManagerService.getRelation( id );
        }
        else
        {
            rel = relationManagerService.getRelation( sourceId, targetId, objectId, 0 );
        }

        if ( rel != null )
        {
            if ( relationManagerService
                    .checkUserPermissions( userSession, rel.getTrustObject().getId(), rel.getTrustObject().getType() )
                    .contains( Permission.Delete ) )
            {
                relationManagerService.removeRelation( rel );
                flashScope.success( "Deleted successfully." );
            }
            else
{'fraction_non_alphanumeric': 0.10386304333990963, 'fraction_numerical': 0.0007636988480875708, 'mean_word_length': 2.333474756045821, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6819567', 'n_tokens_mistral': 4011, 'n_tokens_neox': 3826, 'n_words': 806}