'Ensures the current admin user has ADD_LIBRARY permission on the target data library and folder.'

def __ensure_library_add_permission(self, trans, target_library, target_folder):

    current_user_roles = trans.user.all_roles()
    current_user_private_role = trans.app.security_agent.get_private_user_role(trans.user)
    flush_needed = False
    if (not trans.app.security_agent.can_add_library_item(current_user_roles, target_library)):
        lp = trans.model.LibraryPermissions(trans.app.security_agent.permitted_actions.LIBRARY_ADD.action, target_library, current_user_private_role)
        trans.sa_session.add(lp)
        flush_needed = True
    if (not trans.app.security_agent.can_add_library_item(current_user_roles, target_folder)):
        lfp = trans.model.LibraryFolderPermissions(trans.app.security_agent.permitted_actions.LIBRARY_ADD.action, target_folder, current_user_private_role)
        trans.sa_session.add(lfp)
        flush_needed = True
    if flush_needed:
        trans.sa_session.flush()
