refactoring permission handling and adding tests
Merge pull request #52 from epantry/pr/50

richer permissions