/************************************************
 * Copyright (c) Shen Li. All rights reserved.  *
 * http://joyaop.sourceforge.net                *
 * -------------------------------------------- *
 * Distributable under LGPL license.            *
 * See terms of license at gnu.org.             *
 ************************************************/
package net.sf.joyaop.demo.rbac;

import net.sf.joyaop.demo.rbac.model.Permission;
import net.sf.joyaop.demo.rbac.model.Principle;

/**
 * @author Shen Li
 */
public class AuthenticationManagerImpl implements AuthenticationManager {
    private final EntityHome entityHome;
    private final Context context;

    public AuthenticationManagerImpl(EntityHome entityHome, Context context) {
        this.entityHome = entityHome;
        this.context = context;
    }

    public void authenticate(String permissionName) {
        Principle caller = context.getCurrentPrinciple();
        if (permissionName == null) {
            throw new RuntimeException("Can't get permission ame.");
        }        
        if (!caller.hasPermission((Permission) entityHome.loadEntity(Permission.class, "name", permissionName))) {
            throw new AccessDeniedException();
        }
//        String role = (String) invocation.getParameter("role");
//        if (role != null && !caller.hasRole(entityHome.loadRole(role))) {
//            throw new AccessDeniedException();
//        }
    }
}
