<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def required_role(req_role):
 """ Decorator applied to functions requiring caller to possess the specified role """

    def dec_wrapper(wfunc):
        @wraps(wfunc)
        def wrapped(*args, **kwargs):
            user_id = kwargs.get("user_id")
            try:
                res = db.DBSession.query(RoleUser).filter(RoleUser.user_id==user_id).join(Role, Role.code==req_role).one()
            except NoResultFound:
                raise PermissionError("Permission denied. User %s does not have role %s"%
                                (user_id, req_role))

            return wfunc(*args, **kwargs)

        return wrapped
    return dec_wrapper