Can a university restrict publication of students' work due to plagiarism fears? My question is inspired by this question. Say a student wants to publish on GitHub some code that they've written as assignments for a programming class. The student may be motivated by a desire to show off their programming skills and advance their career this way, or by a wish to provide a useful resource on the internet that others might useful (say, people who are teaching themselves to program, or programmers who may be able to use the code to save themselves some work in connection with a project they're working on), or some combination. 

The student publishes the code, but is then contacted by their department who ask him to take down the code, citing concerns that the code might be used to commit plagiarism by students taking the same class in following years.

First question: is it appropriate and ethical for the department to make such a request of the student? Are they not putting their own selfish interests ahead of the student's own interests and the interests of the community of people who might benefit from having the student's code be made available online?

Second question: Is the student obliged to comply with such a request, or can they simply say they have a right to put their work online in a publicly hosted website if they so desire and be entitled not to suffer any retaliation from the department for this refusal?

Note that even if the student is not obliged to comply with the request, it may still arguably be inappropriate of the department to make it, since the department has authority over the student and under such circumstances it is not clear that the student can make a decision to refuse that is free of duress.
My former department had such restrictions in place prohibiting the dissemination of course work, although very little of the course work involved programming. Students signed an agreement at the beginning of every year. Further, every piece of course work required a signed cover sheet that stated that the students would not share the course work. Sharing course work was treated as academic misconduct and results in either a reduction in the students grade if the offense is discovered while the student is taking the class, a fine (talk about unethical) if the offense is discovered after the student finishes the class, or possible revocation of the degree if the offense is discovered after the student graduates.

While serving on the my department's academic misconduct committee I over saw incidents of all three types. The one case of misconduct after graduation was settled by asking the student to remove the content from the web and the student agreeing. Had they not agreed, it would have gone to a university committee. The couple of cases involving distribution after the class, but prior to graduation, were resolved with fines. In some cases the students paid, in other cases they appealed to the university committee. The university committee sometimes kept the fine and sometimes repealed the fine. The "in class" cases were sometimes appealed to the university committee, but were rarely (if ever) over turned.

As to the ethical question, our policy was reviewed annual at both the departmental level annual and the university level. It was also reviewed every couple of years by the legal department. We often discussed why the policy was so broad. In the end, we always decided that it was easier to make a broad rule and allow students to request exceptions than to try and cover all the creative ways students can cheat.