RABAT, Morocco—I met Mehdi Alioua on a Friday afternoon in the Agdal neighborhood of the capital city. He was coming from having the traditional weekly meal of couscous with his colleagues at the Groupe Antiraciste d’Accompagnement et de Défense des Étrangers et Migrants (Anti-Racism Group for the Support and Defense of Foreigners and Migrants).

Alioua, a sociologist who studies migration and teaches at the International University of Rabat, currently heads the group, which is known by its French acronym, GADEM. He and others created GADEM in 2006 while he was still finishing his doctoral thesis. The association advocates for the rights of refugees and migrants in Morocco, and was influential in lobbying and shaping a new “regularization” process that has allowed tens of thousands of immigrants to gain legal residency here.

Alioua is an example of an engaged academic—voluble on social media, ready to give interviews and sign petitions, interested in influencing policy and committed to solidarity with the communities he studies. He cites the American sociologist Robert Ezra Park, who formulated groundbreaking work on “urban ecology” and race relations in the United States, as a model.

Alioua moved back to Morocco in 2011, leaving a teaching post at the University of Toulouse II. He studies the intersection of local and transitional dynamics along migration routes from Africa to Europe. He is increasingly focused on the “migration diplomacy” of Morocco—its efforts to expand its influence in West Africa partly by welcoming immigrants from neighboring countries—and on Morocco as a new model of a country of emigration, transit, and immigration.

Alioua says he is interested in migrants’ experiences and points of view—how they organize themselves, how they change the places they live in, how they create connections between different locations. “I start from the bottom and move towards the top, not the reverse,” he says. Today, migrants haven’t just created a dense network of circulation between West Africa, the Sahara, Mediterranean Africa and Europe, he says, but they have become more and more engaged in activism. They defend their rights, lobby for freedom of movement, and campaign against police brutality and racism.

In 2013 the king of Morocco announced a drastic change in Morocco’s migration policy, to make it more humanitarian and welcoming. Along with other academics and civil society actors, Alioua was part of consultations on how to put the new policy into effect.

Eventually, new rules have allowed a majority of Morocco’s undocumented immigrants, who mostly hail from other African countries—about 24,000 people out of 27,000 applicants so far—to gain residency papers. Parliament approved a law against human trafficking, but other proposed laws that would finalize changes for asylum seekers and immigrants seeking residency have yet to be passed.

Alioua blames the delay on political parties who have no expertise or interest in the matter. Nonetheless, he says, the regularization campaign was “a real victory, a real advance. We managed to become a country of immigration.”

Of course there are still drawbacks: Some migrants encounter difficulties renewing their residency status and face corruption, racism and what Alioua calls “the power of the guichet” (the window at which government officials process documents). Yet foreigners are no longer systematically harassed, and in theory they have access to public services such as schools and hospitals.

The regularization process has also allowed for migration in Morocco to be openly studied for the first time. Alioua is a co-author of a study that surveyed more than 1,400 migrants who had applied for residency in Morocco. The study found, among other things, that the majority had arrived in the country by plane and that 92 percent had come from cities. Some 87 percent of the poll’s respondents had completed high school, and 50 percent had a university degree.

These migrants belong to Africa’s “middle class that hasn’t managed to become a middle class,” says Alioua. Sixty-five percent of the respondents said they had come to Morocco thinking of settling there. The kingdom assigns a quota of spots at universities and even scholarships to citizens of neighboring African countries.

While Alioua believes Moroccan officials are moving in the right direction, his biggest concern is a noticeable increase in racism against sub-Saharan Africans. This, he says, is because these migrants are more visible and are increasingly viewed as economic competitors and the beneficiaries of scarce services. He predicts that in 20 years Morocco will have its own anti-immigrant political party, just as many European countries do now.

This is despite the fact, Alioua notes, that many Moroccan citizens themselves are migrants to other nations—at least four million Moroccans live in Europe today. But even people who are used to crossing borders will form their identity on the basis of them.

“We need limits to invent ourselves,” says Alioua, referring to the work of the historian and political scientist Benedict Anderson, famous for his concept of nations as “imagined communities.”

“People want to believe in them [the limits],” says Alioua, “and the media participates in popularizing the notion of inside/outside.”

The goal of his work is to help migrant communities articulate their own problems and needs. “We act together to find solutions, and during the action, I analyze,” he says. He shares these analyses with fellow activists and the communities where he conducts his research first, he says, and sometimes their criticism can be harsh.

While he is vocal on many issues, he says, he tries not to speak in the name of migrants and refugees.

“I’ve lived between France and Morocco since I was 6 years old,” he notes. “I am a migrant myself. I experienced difficulties integrating in Morocco, difficulties integrating in France. I know how difficult it can be to join different worlds; I’m a mediator.”