user roles
This commit is contained in:
@@ -282,6 +282,9 @@ def get_contacts(current_user):
|
|||||||
'company': user.company,
|
'company': user.company,
|
||||||
'position': user.position,
|
'position': user.position,
|
||||||
'is_active': user.is_active,
|
'is_active': user.is_active,
|
||||||
|
'is_admin': user.is_admin,
|
||||||
|
'is_manager': user.is_manager,
|
||||||
|
'role': 'admin' if user.is_admin else 'manager' if user.is_manager else 'user',
|
||||||
'created_at': user.created_at.isoformat()
|
'created_at': user.created_at.isoformat()
|
||||||
} for user in users])
|
} for user in users])
|
||||||
|
|
||||||
@@ -301,6 +304,9 @@ def get_contact(current_user, user_id):
|
|||||||
'company': user.company,
|
'company': user.company,
|
||||||
'position': user.position,
|
'position': user.position,
|
||||||
'is_active': user.is_active,
|
'is_active': user.is_active,
|
||||||
|
'is_admin': user.is_admin,
|
||||||
|
'is_manager': user.is_manager,
|
||||||
|
'role': 'admin' if user.is_admin else 'manager' if user.is_manager else 'user',
|
||||||
'created_at': user.created_at.isoformat()
|
'created_at': user.created_at.isoformat()
|
||||||
})
|
})
|
||||||
|
|
||||||
@@ -309,20 +315,26 @@ def get_contact(current_user, user_id):
|
|||||||
@token_required
|
@token_required
|
||||||
def create_contact(current_user):
|
def create_contact(current_user):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
required_fields = ['username', 'email', 'last_name']
|
required_fields = ['username', 'email', 'last_name', 'role']
|
||||||
if not all(field in data for field in required_fields):
|
if not all(field in data for field in required_fields):
|
||||||
return jsonify({'message': 'Missing required fields'}), 400
|
return jsonify({'message': 'Missing required fields'}), 400
|
||||||
|
|
||||||
if User.query.filter_by(email=data['email']).first():
|
if User.query.filter_by(email=data['email']).first():
|
||||||
return jsonify({'message': 'Email already exists'}), 400
|
return jsonify({'message': 'Email already exists'}), 400
|
||||||
|
|
||||||
|
# Validate role
|
||||||
|
if data['role'] not in ['admin', 'manager', 'user']:
|
||||||
|
return jsonify({'message': 'Invalid role'}), 400
|
||||||
|
|
||||||
user = User(
|
user = User(
|
||||||
username=data['username'],
|
username=data['username'],
|
||||||
email=data['email'],
|
email=data['email'],
|
||||||
last_name=data['last_name'],
|
last_name=data['last_name'],
|
||||||
phone=data.get('phone'),
|
phone=data.get('phone'),
|
||||||
company=data.get('company'),
|
company=data.get('company'),
|
||||||
position=data.get('position')
|
position=data.get('position'),
|
||||||
|
is_admin=data['role'] == 'admin',
|
||||||
|
is_manager=data['role'] == 'manager'
|
||||||
)
|
)
|
||||||
user.set_password(data.get('password', 'changeme'))
|
user.set_password(data.get('password', 'changeme'))
|
||||||
|
|
||||||
@@ -344,6 +356,13 @@ def update_contact(current_user, user_id):
|
|||||||
return jsonify({'message': 'Email already exists'}), 400
|
return jsonify({'message': 'Email already exists'}), 400
|
||||||
user.email = data['email']
|
user.email = data['email']
|
||||||
|
|
||||||
|
# Update role if provided
|
||||||
|
if 'role' in data:
|
||||||
|
if data['role'] not in ['admin', 'manager', 'user']:
|
||||||
|
return jsonify({'message': 'Invalid role'}), 400
|
||||||
|
user.is_admin = data['role'] == 'admin'
|
||||||
|
user.is_manager = data['role'] == 'manager'
|
||||||
|
|
||||||
user.username = data.get('username', user.username)
|
user.username = data.get('username', user.username)
|
||||||
user.last_name = data.get('last_name', user.last_name)
|
user.last_name = data.get('last_name', user.last_name)
|
||||||
user.phone = data.get('phone', user.phone)
|
user.phone = data.get('phone', user.phone)
|
||||||
|
|||||||
@@ -219,6 +219,14 @@
|
|||||||
<label for="position" class="form-label">Position</label>
|
<label for="position" class="form-label">Position</label>
|
||||||
<input type="text" class="form-control" id="position" name="position">
|
<input type="text" class="form-control" id="position" name="position">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="role" class="form-label">Role</label>
|
||||||
|
<select class="form-select" id="role" name="role" required>
|
||||||
|
<option value="user">Standard User</option>
|
||||||
|
<option value="manager">Manager</option>
|
||||||
|
<option value="admin">Administrator</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="status" class="form-label">Status</label>
|
<label for="status" class="form-label">Status</label>
|
||||||
<select class="form-select" id="status" name="status">
|
<select class="form-select" id="status" name="status">
|
||||||
@@ -267,6 +275,14 @@
|
|||||||
<label for="edit-position" class="form-label">Position</label>
|
<label for="edit-position" class="form-label">Position</label>
|
||||||
<input type="text" class="form-control" id="edit-position" name="position">
|
<input type="text" class="form-control" id="edit-position" name="position">
|
||||||
</div>
|
</div>
|
||||||
|
<div class="mb-3">
|
||||||
|
<label for="edit-role" class="form-label">Role</label>
|
||||||
|
<select class="form-select" id="edit-role" name="role" required>
|
||||||
|
<option value="user">Standard User</option>
|
||||||
|
<option value="manager">Manager</option>
|
||||||
|
<option value="admin">Administrator</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
<div class="mb-3">
|
<div class="mb-3">
|
||||||
<label for="edit-status" class="form-label">Status</label>
|
<label for="edit-status" class="form-label">Status</label>
|
||||||
<select class="form-select" id="edit-status" name="status">
|
<select class="form-select" id="edit-status" name="status">
|
||||||
@@ -682,6 +698,7 @@ async function addContact(event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const formData = new FormData(event.target);
|
const formData = new FormData(event.target);
|
||||||
|
|
||||||
const response = await fetch(`{{ instance.main_url }}/api/admin/contacts`, {
|
const response = await fetch(`{{ instance.main_url }}/api/admin/contacts`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
@@ -695,7 +712,8 @@ async function addContact(event) {
|
|||||||
phone: formData.get('phone'),
|
phone: formData.get('phone'),
|
||||||
company: formData.get('company'),
|
company: formData.get('company'),
|
||||||
position: formData.get('position'),
|
position: formData.get('position'),
|
||||||
status: formData.get('status')
|
role: formData.get('role'),
|
||||||
|
is_active: formData.get('status') === 'active'
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -756,6 +774,7 @@ async function editContact(id) {
|
|||||||
document.getElementById('edit-phone').value = contact.phone || '';
|
document.getElementById('edit-phone').value = contact.phone || '';
|
||||||
document.getElementById('edit-company').value = contact.company || '';
|
document.getElementById('edit-company').value = contact.company || '';
|
||||||
document.getElementById('edit-position').value = contact.position || '';
|
document.getElementById('edit-position').value = contact.position || '';
|
||||||
|
document.getElementById('edit-role').value = contact.role;
|
||||||
document.getElementById('edit-status').value = contact.is_active ? 'active' : 'inactive';
|
document.getElementById('edit-status').value = contact.is_active ? 'active' : 'inactive';
|
||||||
|
|
||||||
// Show modal
|
// Show modal
|
||||||
@@ -804,7 +823,8 @@ async function updateContact(event) {
|
|||||||
phone: formData.get('phone'),
|
phone: formData.get('phone'),
|
||||||
company: formData.get('company'),
|
company: formData.get('company'),
|
||||||
position: formData.get('position'),
|
position: formData.get('position'),
|
||||||
status: formData.get('status') === 'active'
|
role: formData.get('role'),
|
||||||
|
is_active: formData.get('status') === 'active'
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user