ISTANBUL — Turkey’s president signed legislation on Friday that granted more authority to the national spy agency, a measure that critics say will bolster Prime Minister Recep Tayyip Erdogan’s control over state institutions.

The legislation, which passed in Parliament last week after Mr. Erdogan’s party won an indisputable victory in March local elections, allows the agency, the National Intelligence Organization, to have access to public and private information without court orders and to conduct foreign operations in coordination with the military. It also gives the agency broad immunity from prosecution.

Government officials defended the comprehensive changes, saying they would make the agency more efficient and protect personnel doing their duties. They said the legislation provided long overdue overhauls of a law that dates to 1984.

Critics of the government, however, said the changes would turn the spy agency, known by the initials of its Turkish name, M.I.T., into a formidable weapon for Mr. Erdogan in his struggle against a pro-Islamic network led by Fethullah Gulen, a cleric and former ally who lives in Pennsylvania.