1<?php
2/*
3 * vim:set softtabstop=4 shiftwidth=4 expandtab:
4 *
5 * LICENSE: GNU Affero General Public License, version 3 (AGPL-3.0-or-later)
6 * Copyright 2001 - 2020 Ampache.org
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20 *
21 */
22
23declare(strict_types=1);
24
25namespace Ampache\Module\Application\Admin\Access\Lib;
26
27use Ampache\Repository\Model\ModelFactoryInterface;
28use Ampache\Module\Authorization\Access;
29
30final class AccessListItem implements AccessListItemInterface
31{
32    private Access $access;
33
34    private ModelFactoryInterface $modelFactory;
35
36    public function __construct(
37        ModelFactoryInterface $modelFactory,
38        Access $access
39    ) {
40        $this->modelFactory = $modelFactory;
41        $this->access       = $access;
42    }
43
44    /**
45     * take the int level and return a named level
46     */
47    public function getLevelName(): string
48    {
49        $level = (int) $this->access->level;
50
51        if ($level >= 75) {
52            return T_('All');
53        }
54        if ($level == 5) {
55            return T_('View');
56        }
57        if ($level == 25) {
58            return T_('Read');
59        }
60        if ($level == 50) {
61            return T_('Read/Write');
62        }
63
64        return '';
65    }
66
67    /**
68     * Return a name for the users covered by this ACL.
69     */
70    public function getUserName(): string
71    {
72        $userId = (int) $this->access->user;
73
74        if ($userId === -1) {
75            return T_('All');
76        }
77
78        $user = $this->modelFactory->createUser($userId);
79
80        return sprintf('%s (%s)', $user->fullname, $user->username);
81    }
82
83    /**
84     * This function returns the pretty name for our current type.
85     */
86    public function getTypeName(): string
87    {
88        switch ($this->access->type) {
89            case 'rpc':
90                return T_('API/RPC');
91            case 'network':
92                return T_('Local Network Definition');
93            case 'interface':
94                return T_('Web Interface');
95            case 'stream':
96            default:
97                return T_('Stream Access');
98        }
99    }
100
101    /**
102     * Returns a human readable representation of the start ip
103     */
104    public function getStartIp(): string
105    {
106        $result = @inet_ntop($this->access->start);
107        if ($result === false) {
108            return '';
109        }
110
111        return $result;
112    }
113
114    /**
115     * Returns a human readable representation of the end ip
116     */
117    public function getEndIp(): string
118    {
119        $result = @inet_ntop($this->access->end);
120        if ($result === false) {
121            return '';
122        }
123
124        return $result;
125    }
126
127    /**
128     * Returns the acl name
129     */
130    public function getName(): string
131    {
132        return $this->access->name;
133    }
134
135    /**
136     * Returns the acl item id
137     */
138    public function getId(): int
139    {
140        return (int) $this->access->id;
141    }
142
143    /**
144     * Returns the acl item level
145     */
146    public function getLevel(): int
147    {
148        return (int) $this->access->level;
149    }
150
151    /**
152     * Returns the acl item type
153     */
154    public function getType(): string
155    {
156        return $this->access->type;
157    }
158
159    /**
160     * Returns the acl item user id
161     */
162    public function getUserId(): int
163    {
164        return (int) $this->access->user;
165    }
166}
167