You can check the user role in retrieve action, see the screenshot below for example
Hope this helps!
For your question, how to check if the user is anonymous.
After retrieving the User, check for the boolean IsAnonymous.
All anonymous users will have this boolean set to true.
If you want to retrieve all roles of the user, use the association “UserRoles” from User entity.
BTW, I see that you are using DB calls to retrieve the User. This is not needed. You can use $currentUser which is already a System.User entity. From $currentuser you can also access associations. You could save one database call, as database calls are always costly (performance wise).