drupal|March 25, 2018|1 min read

Drupal: How to block a user by email programatically

TL;DR

Load users by email with user_load_by_mail, set their status to 0, and save with user_save to block spam accounts in bulk.

Drupal: How to block a user by email programatically

Many times, while administering your drupal website, you must have encountered some spam emails. And, you have no way to contact the users. You might want to block the user.

Drupal provides a way to block a user by login from an admin user. But, if you have many users, it will take considerable amount of time to do it one by one.

Blocking User Programmatically

This code assumes, you have email of users you want to block.

Drupal provides an api: user_save(), which takes a loaded user object. And, it can save user in its database.

Code:

``` $mails = array('[emailprotected]', '[emailprotected]');

foreach($mails as $ml) { $user = user_load_by_mail($ml); if (isset($user) && $user->status == 1) { $uObj = new stdClass(); $uObj->uid = $user->uid; user_save($uObj, array(‘status’ => 0)); print “User blocked: “.$user->mail; } print “\n”; }


<h3>Understanding Code</h3>
First, I load user by email by using api: <strong>user_load_by_mail()</strong>. Then, I prepare an object by using user uid, and set its status as 0. In drupal, user status of 1 is considered active user.

The API: user_save() is taking 2 parameters. Since, I want to update user. I just passed the object with its uid set. And, in 2nd parameter, I pass the array of attributes I want to update. This ensures, that I do not set any other parameter accidently.

And, it blocks all the user mails passed.

Related Posts

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

Drupal Code&#58; Fetch Link, Title, Category names, tag names from every article

Drupal Code&#58; Fetch Link, Title, Category names, tag names from every article

See the code below: The output will be 4 columns separated by comma. You can…

Drupal 7&#58; How to save a node programmatically and add an image field from a public URL

Drupal 7&#58; How to save a node programmatically and add an image field from a public URL

Note: I have public URLs of these images, which I want to save. return…

How to add alt attribute of images in all of my drupal articles or other content type

How to add alt attribute of images in all of my drupal articles or other content type

I have a custom content type, and there are around 2000 number of nodes in my…

How to use Docker for Drupal 7 Dev envirnoment

How to use Docker for Drupal 7 Dev envirnoment

I have been using drupal 7 from a long time, and setting up a dev environment…

List all the Node ids which do not have images from my domain

List all the Node ids which do not have images from my domain

I use drupal-7 in my website. I used to write articles and put images in that…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…