Completed
Push — 16146-settings-to-admin ( e90069 )
by Shawn
05:56
created

SettingController::updateUserRoles()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 3
eloc 9
nc 4
nop 1
1
<?php
2
3
namespace SET\Http\Controllers;
4
5
use Illuminate\Http\Request;
6
use Illuminate\Support\Facades\Config;
7
use SET\User;
8
use SET\Setting;
9
10
class SettingController extends Controller
11
{
12
    /**
13
     * Display a listing of the resource.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function index()
18
    {
19
        $this->authorize('edit');
20
21
        $settings = Setting::getAll();
22
        $ldapControllers = $settings['adldap.connections.default.connection_settings.domain_controllers'] ?? config('adldap.connections.default.connection_settings.domain_controllers');
23
        $settings['ldap_controller1'] = $ldapControllers[0];
24
        $settings['ldap_controller2'] = $ldapControllers[1];
25
26
        $users = User::skipSystem()->active()->get()->sortBy('UserFullName');
27
        $userList = $users->pluck('UserFullName', 'id');
28
        $admins = $users->where('role', 'edit')->pluck('id')->all();
29
        $viewers = $users->where('role', 'view')->pluck('id')->all();;
30
        $configAdmins = User::whereIn('username', Config::get('auth.admin'))
31
            ->get()->pluck('userFullName')->implode('; ');
32
33
        return view('setting.index', compact('settings', 'userList', 'admins', 'configAdmins', 'viewers'));
34
    }
35
36
    /**
37
     * Update the specified resource in storage.
38
     *
39
     * @param \Illuminate\Http\Request $request
40
     *
41
     * @return \Illuminate\Http\RedirectResponse
42
     */
43
    public function update(Request $request)
44
    {
45
        $this->authorize('edit');
46
47
        $data = $request->all();
48
        $data = $this->updateUserRoles($data);
49
        $data = $this->saveAdldapKeyFormat($data);
50
51
        unset($data['_method']);
52
        unset($data['_token']);
53
54
        foreach ($data as $key => $value) {
55
            Setting::set($key,$value);
0 ignored issues
show
Bug introduced by
It seems like $value defined by $value on line 54 can also be of type array<integer,?,{"0":"?","1":"?"}>; however, SET\Setting::set() does only seem to accept string|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
56
        }
57
58
        return redirect()->action('SettingController@index');
59
    }
60
61
    /**
62
     * @param $data
63
     * @return mixed
64
     */
65
    private function updateUserRoles($data)
66
    {
67
        User::where('role', '!=', '')->update(['role' => '']);
68
        if (isset($data['viewer'])) {
69
            User::whereIn('id', $data['viewer'])->update(['role' => 'view']);
70
            unset($data['viewer']);
71
        }
72
        if (isset($data['admin'])) {
73
            User::whereIn('id', $data['admin'])->update(['role' => 'edit']);
74
            unset($data['admin']);
75
        }
76
        return $data;
77
    }
78
79
80
    /**
81
     * PHP converts our periods to underscores.
82
     * This causes issues where adldap can't figure out the ADLDAP settings.
83
     * As such, we need to convert the correct underscores back to their dot notation.
84
     * Note: some underscores must be retained.
85
     *
86
     * @param $data
87
     */
88
    private function saveAdldapKeyFormat($data)
89
    {
90
        $adldapData = [
91
            'auth.providers.users.driver' => 'auth_driver',
92
            'adldap.connections.default.connection_settings.base_dn' => 'adldap_connections_default_connection_settings_base_dn',
93
            'adldap.connections.default.connection_settings.admin_username' => 'adldap_connections_default_connection_settings_admin_username',
94
            'adldap.connections.default.connection_settings.admin_password' => 'adldap_connections_default_connection_settings_admin_password',
95
            'adldap.connections.default.connection_settings.account_prefix' => 'adldap_connections_default_connection_settings_account_prefix',
96
            'adldap.connections.default.connection_settings.account_suffix' => 'adldap_connections_default_connection_settings_account_suffix',
97
            'adldap_auth.limitation_filter' => 'adldap_auth_limitation_filter'
98
        ];
99
100
        foreach ($adldapData as $correctKey => $formKey)
101
        {
102
            $data[$correctKey] = $data[$formKey];
103
            unset($data[$formKey]);
104
        }
105
106
        $data['adldap.connections.default.connection_settings.domain_controllers'] = [$data['ldap_controller1'], $data['ldap_controller2']];
107
        unset($data['ldap_controller1']);
108
        unset($data['ldap_controller2']);
109
110
        if ($data['adldap.connections.default.connection_settings.admin_password'] == "") {
111
            $data['adldap.connections.default.connection_settings.admin_password'] = Setting::get('adldap.connections.default.connection_settings.admin_password');
112
        }
113
114
        return $data;
115
    }
116
}
117