📦 NEW: Account levels

This commit is contained in:
Austin Ginder 2020-02-01 18:12:23 -05:00
parent e1d0949e45
commit 4709091804
4 changed files with 43 additions and 16 deletions

View file

@ -51,13 +51,23 @@ class Account {
if ( $this->account_id == "" ) {
return [];
}
$record = [
$user_id = get_current_user_id();
$users = $this->users();
$record = [
"account" => $this->account(),
"invites" => $this->invites(),
"users" => $this->users(),
"users" => $users,
"domains" => $this->domains(),
"sites" => $this->sites(),
"owner" => false,
];

foreach ($users as $user) {
if ($user['user_id'] === $user_id && $user['level'] == "Owner" ) {
$record["owner"] = true;
}
}
return $record;
}

@ -136,15 +146,15 @@ class Account {
}

public function users() {
$users = array_column( ( new AccountUser )->where( [ "account_id" => $this->account_id ] ), "user_id" );
$permissions = ( new AccountUser )->where( [ "account_id" => $this->account_id ] );
$results = [];
foreach( $users as $user_id ) {
$user = get_userdata( $user_id );
foreach( $permissions as $permission ) {
$user = get_userdata( $permission->user_id );
$results[] = [
"user_id" => $user->ID,
"name" => $user->display_name,
"email" => $user->user_email,
"level" => ""
"level" => ucfirst( $permission->level ),
];
}
return $results;
@ -231,9 +241,9 @@ class Account {

public function calculate_totals() {
$metrics = [
"sites" => count( $this->sites() ),
"sites" => count( $this->sites() ),
"users" => count( $this->users() ),
"domains" => count( $this->domains() ),
"domains" => count( $this->domains() ),
];
( new Accounts )->update( [ "metrics" => json_encode( $metrics ) ], [ "account_id" => $this->account_id ] );
return [ "message" => "Account metrics updated." ];

View file

@ -38,6 +38,22 @@ class User {
return true;
}

public function verify_account_owner( $account_id ) {

if ( self::is_admin() ) {
return true;
}

$users = ( new Account( $account_id, true ) )->users();

foreach ($users as $user) {
if ( $user['user_id'] === $this->user_id && $user['level'] == "Owner" ) {
return true;
}
}
return false;
}

public function roles() {
return $this->roles;
}

View file

@ -3953,9 +3953,8 @@ function captaincore_ajax_action_callback() {
}

if ( $cmd == 'updateSiteAccount' ) {

$account = (object) $value;
if ( ! $user->verify_accounts( [ $account->account_id ] ) ) {
if ( ! $user->verify_account_owner( $account->account_id ) ) {
echo "Permission denied";
wp_die();
return;

View file

@ -3790,7 +3790,7 @@ if ( $role_check ) {
{{ dialog_account.records.domains.length }} Domains
<v-icon size="20" class="ml-1">mdi-library-books</v-icon>
</v-tab>
<v-tab>
<v-tab v-show="role == 'administrator' || dialog_account.records.owner">
Advanced
<v-icon size="24">mdi-cogs</v-icon>
</v-tab>
@ -3798,7 +3798,7 @@ if ( $role_check ) {
<v-card-text style="max-height:100%;padding:0px;margin:0px">
<v-tabs-items v-model="account_tab">
<v-tab-item>
<v-toolbar dense flat color="grey lighten-4">
<v-toolbar dense flat color="grey lighten-4" v-show="role == 'administrator' || dialog_account.records.owner">
<div class="flex-grow-1"></div>
<v-toolbar-items>
<v-btn text @click="dialog_account.new_invite = true">New Invite <v-icon dark>add</v-icon></v-btn>
@ -3832,13 +3832,15 @@ if ( $role_check ) {
</v-card>
<v-data-table
v-show="typeof dialog_account.records.users == 'object' && dialog_account.records.users.length > 0"
:headers='[{"text":"Name","value":"name"},{"text":"Email","value":"email"},{"text":"","value":"actions"}]'
:headers='[{"text":"Name","value":"name"},{"text":"Email","value":"email"},{"text":"","value":"level"},{"text":"","value":"actions"}]'
:items="dialog_account.records.users"
:sort-by='["level","name"]'
sort-desc
:items-per-page="-1"
hide-default-footer
>
<template v-slot:item.actions="{ item }">
<v-btn text icon color="pink" @click="removeAccountAccess( item.user_id )" v-if="role == 'administrator'">
<v-btn text icon color="pink" @click="removeAccountAccess( item.user_id )" v-if="role == 'administrator' || dialog_account.records.owner && item.level != 'Owner'">
<v-icon>mdi-delete</v-icon>
</v-btn>
</template>
@ -3915,8 +3917,8 @@ if ( $role_check ) {
<v-toolbar dense flat color="grey lighten-4">
<div class="flex-grow-1"></div>
<v-toolbar-items>
<v-btn text @click="editAccount()">Edit account <v-icon dark small>edit</v-icon></v-btn>
<v-btn text @click="deleteAccount()">Delete account <v-icon dark small>delete</v-icon></v-btn>
<v-btn text @click="editAccount()">Edit account <v-icon dark small>edit</v-icon></v-btn>
<v-btn text @click="deleteAccount()" v-show="role =='administrator'">Delete account <v-icon dark small>delete</v-icon></v-btn>
</v-toolbar-items>
</v-toolbar>