[Building Sakai] SAKAI_REALM_FUNCTION

Paul Mungai paulwando at gmail.com
Fri Aug 26 01:36:13 PDT 2011


On Thu, Aug 25, 2011 at 11:59 AM, Paul Mungai <paulwando at gmail.com> wrote:

> When the server starts up, I get a null on the first instance when I call
> (in the init() ):
>
> List currentFunctions =
> functionManager.getRegisteredUserMutableFunctions(prefix);
>
> However, there are values in the database (Table: SAKAI_REALM_FUNCTION)
> with that prefix.
>
> The null leads to registerFunction after which I allowFunction to certain
> roles in a group. All this works perfect, But not desired as this has been
> done before.
>
>
>
Fixed it as shown below:

    private void registerFunction(String function, boolean allow) {
        if (functionManager.getRegisteredFunctions(function) == null
                ||
functionManager.getRegisteredFunctions(function).isEmpty()) {
            functionManager.registerFunction(function, allow);
        }
    }

    public void init() {
        try {
            // List with functions. Implemented with ArrayList
            List<String> funcs = new ArrayList<String>();
            funcs.add(FUNCTION_ADDTOOL);
            funcs.add(FUNCTION_ADDMEMBER);
            funcs.add(FUNCTION_DELETEMEMBER);
            funcs.add(FUNCTION_DELETETOOL);
            funcs.add(FUNCTION_EDITINFO);

            Iterator funcsit = funcs.iterator();

            //Register functions
            while (funcsit.hasNext()) {
                String function = (String) funcsit.next();
                registerFunction(function, true);
            }


            //Create functions if not-exists
            String prefix = FUNCTION_PREFIX;

            List<String> currentfunctions =
functionManager.getRegisteredFunctions(prefix);
            int funcSize = currentfunctions.size();

            //Use the AuthzGroupService to get the AuthzGroup for SITE_USER
            AuthzGroup ag =
authzGroupService.getAuthzGroup(GROUP_TEMPLATE_COURSE);

            //Get roles
            Role roleinstructor = ag.getRole("Instructor");
            Role roleta = ag.getRole("Teaching Assistant");

            //Flag for checking if either TA or Instructor has at least one
permission
            boolean hasPerms = false;

            //Get the iterator
            Iterator itfuncs = funcs.iterator();

            //check for role Instructors & Teaching Assistants
            while (itfuncs.hasNext() && !hasPerms) {
                String function = (String) itfuncs.next();
                hasPerms = roleinstructor.isAllowed(function);
                //If not, check if TA has perms in this function
                if (!hasPerms) {
                    hasPerms = roleta.isAllowed(function);
                }
            }

            //If instructor or TA does not have any permissions, set default
permissions
            if (!hasPerms && authzGroupService.allowUpdate(ag.getId())) {
                //Get the iterator
                Iterator itfunctions = funcs.iterator();
                //Add default permissions to instructor & Teaching assistant
                while (itfunctions.hasNext()) {
                    String function = (String) itfunctions.next();
                    //Set permissions for Instructor
                    roleinstructor.allowFunction(function);
                    //Set permissions for TA
                    roleta.allowFunction(function);
                }

                //Use the AuthzGroupService to save the group
                authzGroupService.save(ag);
                //Log warnings and handle exceptions
                log.info("Added Permissions to group:" +
GROUP_TEMPLATE_COURSE);
            }
        } catch (Exception ex) {
            log.warn("Error with ToolnameController.init()", ex);
        }
    }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://collab.sakaiproject.org/pipermail/sakai-dev/attachments/20110826/d916fcfc/attachment.html 


More information about the sakai-dev mailing list