As long as the person signs the check along with you and/or they give the bank special instructions allowing you to cash the check for them then it's legal.
If the person hasn't given the bank special instructions then you'll need the person who's name is on the check to be present with you and sign the check in front of a bank teller.