Is it possible to restrict writing to the .htpasswd file ONLY if users belong to a specific role?

I want to provide protected, fast downloads, and after some investigation I believe this is the best way.

Thanks.

Comments

hmmmstrange’s picture

Yes it is.

In the htpasswd configure form there are radio buttons to select which roles you collect. Then in your htaccess you'd use "require group fidelixfastdownloads"

Fidelix’s picture

No, that's not what I asked.

The way you are suggesting, it seems that it still writes to the htpasswd file ALL users, regardless of role, and then it just uses the htgroup directive to restrict groups.

I don't use apache, I use nginx, and it does not support htgroup directives, only the htpasswd. That's why I only want users of a specific role written to that file.

Is this possible?

hedac’s picture

Issue summary: View changes

I have the same problem using nginx. I think the solution is to maintain both files as they are, the users file with the passwords, and the groups file with the roles. Then, for nginx, add a third file (the one nginx would use instead), that only adds the users that are in the desired group (role)

hedac’s picture

I've made this temporary code added at the end of function _htpasswdsync_updategroup()
it updates a file with the same name and a _nginx suffix. This is the file to use by nginx.
it doesn't use the mail_domain setting but I'm not using that option.
I think it works. But please forgive the bad coding.

// write nginx file only with the users from the defined roles
	$filenginx = _htpasswdsync_passfilename() . "_nginx";
	$passwords2 = array();
	foreach(_htpasswdsync_roles() as $rid) {
        $role = _htpasswdsync_sanatize_name(reset(db_query('SELECT name FROM {role} WHERE rid = :rid', array(':rid' => $rid))->fetchCol()));
        if(!empty($role)) {
            $users = db_query('SELECT u.name, u.mail FROM {users} AS u, {users_roles} AS ur WHERE ur.rid = :rid AND ur.uid = u.uid AND status = 1', array(':rid' => $rid));
            foreach($users as $user) {
				
				$pass = db_query('SELECT ut.username, ut.passwd FROM {htpasswdsync_htpasswd} AS ut WHERE username = :name', array(':name' => $user->name));
				foreach($pass as $p) {
					$passwords2[_htpasswdsync_sanatize_name($user->name)] = $p->passwd;
				}
            }
        }
    }
	_htpasswdsync_write_htfile($passwords2, $filenginx);
hedac’s picture

Status: Active » Needs work