admins can make others admins

v1.18.x
Baris Usakli 12 years ago
parent ac5b7c02f8
commit 3f3d81de44

@ -2,7 +2,6 @@
var fs = require('fs'),
path = require('path'),
utils = require('./public/src/utils.js'),
args = {};
// Parse any passed-in arguments
@ -74,7 +73,9 @@ fs.readFile(path.join(__dirname, 'config.json'), function(err, data) {
});
// Hardcoding uid 1 as an admin
RDB.sadd('administrators', 1);
//RDB.sadd('administrators', 1);
var user = require('./src/user.js');
user.makeAdministrator(1);
} else {
console.log('Info: Good.');
}

@ -326,7 +326,8 @@ footer.footer {
display:inline-block;
margin-right:20px;
margin-top: 20px;
text-align:center;
text-align:center;
vertical-align:top;
}
a:hover {

@ -14,13 +14,13 @@
</div>
<!-- BEGIN users -->
<div class="users-box well">
<a href="/users/{users.username}">
<img src="{users.picture}" class="user-8080-picture"/>
</a>
<br/>
<div class="users-box well" data-uid="{users.uid}">
<a href="/users/{users.username}">
<img src="{users.picture}" class="user-8080-picture"/>
</a>
<br/>
<a href="/users/{users.username}">{users.username}</a>
<br/>
<br/>
<div title="reputation">
<span id='reputation'>{users.reputation}</span>
<i class='icon-star'></i>
@ -29,15 +29,25 @@
<span id='postcount'>{users.postcount}</span>
<i class='icon-pencil'></i>
</div>
<div>
<a href="#" class="btn make-admin-btn" data-admin="{users.administrator}">Make Admin</a>
</div>
<div>
<a href="#" class="btn remove-admin-btn" data-admin="{users.administrator}">Remove Admin</a>
</div>
</div>
<!-- END users -->
<input type="hidden" template-variable="yourid" value="{yourid}" />
<script type="text/javascript">
//DRY Failure. this needs to go into an ajaxify onready style fn. Currently is copy pasted into every single function so after ACP is off the ground fix asap
(function() {
jQuery('document').ready(function() {
var yourid = templates.get('yourid');
var url = window.location.href,
parts = url.split('/'),
active = parts[parts.length-1];
@ -48,7 +58,94 @@
jQuery(this.parentNode).addClass('active');
return false;
}
})
});
jQuery('.make-admin-btn').each(function(index, element) {
var adminBtn = $(element);
var isAdmin = adminBtn.attr('data-admin') !== "0";
if(isAdmin)
adminBtn.hide();
else
adminBtn.show();
});
jQuery('.remove-admin-btn').each(function(index, element) {
var adminBtn = $(element);
var isAdmin = adminBtn.attr('data-admin') !== "0";
var parent = adminBtn.parents('.users-box');
var uid = parent.attr('data-uid');
console.log(uid);
console.log(yourid);
if(isAdmin && uid != yourid)
adminBtn.show();
else
adminBtn.hide();
});
jQuery('.make-admin-btn').on('click', function() {
var makeBtn = $(this);
var parent = makeBtn.parents('.users-box');
var removeBtn = parent.find('.remove-admin-btn');
var uid = parent.attr('data-uid');
var userData = {
uid:uid
};
$.post('/admin/makeadmin',
userData,
function(data) {
app.alert({
'alert_id': 'user_made_admin',
type: 'success',
title: 'User Modified',
message: 'This user is an administrator now!',
timeout: 2000
});
makeBtn.hide();
removeBtn.show();
}
);
return false;
});
jQuery('.remove-admin-btn').on('click', function() {
var removeBtn = $(this);
var parent = removeBtn.parents('.users-box');
var makeBtn = parent.find('.make-admin-btn');
var uid = parent.attr('data-uid');
var userData = {
uid:uid
};
$.post('/admin/removeadmin',
userData,
function(data) {
app.alert({
'alert_id': 'user_removed_admin',
type: 'success',
title: 'User Modified',
message: 'This user is no longer an administrator!',
timeout: 2000
});
makeBtn.show();
removeBtn.hide();
}
);
return false;
});
});
}());

@ -1,7 +1,5 @@
<div class="well">
<div class="account-username-box">
<span class="account-username">
@ -16,15 +14,15 @@
</div>
<div>
<!-- BEGIN followers -->
<!-- BEGIN followers -->
<div class="users-box well">
<a href="/users/{followers.username}">
<img src="{followers.picture}" class="user-8080-picture"/>
</a>
<br/>
<div class="users-box well">
<a href="/users/{followers.username}">
<img src="{followers.picture}" class="user-8080-picture"/>
</a>
<br/>
<a href="/users/{followers.username}">{followers.username}</a>
<br/>
<br/>
<div title="reputation">
<span class='reputation'>{followers.reputation}</span>
<i class='icon-star'></i>

@ -42,7 +42,8 @@ var user = require('./../user.js'),
res.send(JSON.stringify({search_display: 'block', users: []}))
} else {
user.getUserList(function(data){
res.send(JSON.stringify({search_display: 'none', users:data}));
res.send(JSON.stringify({search_display: 'none', users:data, yourid:req.user.uid}));
});
}
@ -92,6 +93,42 @@ var user = require('./../user.js'),
app.get('/api/admin/:method/:tab?*', api_method);
app.get('/api/admin/:method*', api_method);
app.post('/admin/makeadmin', function(req, res){
if(!req.user)
return res.redirect('/403');
user.isAdministrator(req.user.uid, function(isAdmin) {
if(isAdmin) {
user.makeAdministrator(req.body.uid, function(data) {
res.send(data);
});
}
else
res.redirect('/403');
});
});
app.post('/admin/removeadmin', function(req, res){
if(!req.user)
return res.redirect('/403');
user.isAdministrator(req.user.uid, function(isAdmin) {
if(isAdmin) {
user.removeAdministrator(req.body.uid, function(data) {
res.send(data);
});
}
else
res.redirect('/403');
});
});
};

@ -17,7 +17,7 @@ var user = require('./../user.js'),
app.get('/uid/:uid', function(req, res) {
if(!req.params.uid)
return res.redirect('/403');
return res.redirect('/404');
user.getUserData(req.params.uid, function(data){
if(data)
@ -42,7 +42,7 @@ var user = require('./../user.js'),
user.get_uid_by_username(req.params.username, function(uid) {
if(!uid) {
res.redirect('/403');
res.redirect('/404');
return;
}
@ -51,7 +51,7 @@ var user = require('./../user.js'),
res.send(build_header() + app.create_route('users/'+data.username, 'account') + templates['footer']);
}
else {
res.redirect('/403');
res.redirect('/404');
}
});
});
@ -67,7 +67,7 @@ var user = require('./../user.js'),
if(req.params.username && username === req.params.username)
res.send(build_header() + app.create_route('users/'+req.params.username+'/edit','accountedit') + templates['footer']);
else
return res.redirect('/403');
return res.redirect('/404');
});
});

@ -127,13 +127,9 @@ var utils = require('./../public/src/utils.js'),
}
for(var i=0,ii=userkeys.length; i<ii; ++i) {
RDB.hgetall(userkeys[i], function(err, userdata) {
if(userdata && userdata.password)
delete userdata.password;
data.push(userdata);
var uid = userkeys[i].substr(5);
User.getUserData(uid, function(userData) {
data.push(userData);
if(data.length == userkeys.length)
callback(data);
});
@ -299,7 +295,8 @@ var utils = require('./../public/src/utils.js'),
'uploadedpicture': '',
'reputation': 0,
'postcount': 0,
'lastposttime': 0
'lastposttime': 0,
'administrator': 0
});
RDB.set('username:' + username + ':uid', uid);
@ -602,6 +599,26 @@ var utils = require('./../public/src/utils.js'),
});
}
User.makeAdministrator = function(uid, callback) {
RDB.sadd('administrators', uid, function(err, data){
if(err === null) {
User.setUserField(uid, 'administrator', 1);
}
if(callback)
callback(err === null);
});
}
User.removeAdministrator = function(uid, callback) {
RDB.srem('administrators', uid, function(err, data){
if(err === null) {
User.setUserField(uid, 'administrator', 0);
}
if(callback)
callback(err === null);
});
}
User.reset = {
validate: function(socket, code, callback) {
if (typeof callback !== 'function') callback = undefined;

Loading…
Cancel
Save