How to let a role handle media without post permissions

I solved it. The problem was, that I called the function inside of another function, called later. The snippet in the question works like it should.

I thought about deleting the question, but I think for other users, that are searching for a way to do something like that, the snippet would be interesting.