Skip to content

Commit

Permalink
Add an is_owner option to exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
elchininet committed Dec 10, 2024
1 parent 5458aec commit bd72986
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 10 deletions.
22 changes: 15 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -311,11 +311,12 @@ In an exception you can define almost all the options available in [the main con

| Property | Type | Required | Description |
| ------------------- | ----------------- | -------- | ----------- |
| user | String or String[] | no | Home Assistant user name(s) you would like to display this order for. This option can be set alone or combined with `device`, `not_device` or `is_admin`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| not_user | String or String[] | no | Home Assistant user name(s) you wouldn't like to display this order for. This option can be set alone or combined with `device`, `not_device` or `is_admin`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| device | String or String[] | no | Device(s) you would like to display this order for. E.g. ipad, iphone, macintosh, windows, android (it uses the device's [user-agent]). This option can be set alone or combined with `user`, `not_user` or `is_admin`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| not_device | String or String[] | no | Device(s) you wouldn't like to display this order for. E.g. ipad, iphone, macintosh, windows, android (it uses the device's [user-agent]). This option can be set alone or combined with `user`, `not_user` or `is_admin`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| is_admin | Boolean | no | Checks if the user is admin or not. This option can be set alone or combined with `user`, `not_user`, `device` or `not_device`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| user | String or String[] | no | Home Assistant user name(s) you would like to display this order for. This option can be set alone or combined with `device`, `not_device`, `is_admin`, or `is_owner`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| not_user | String or String[] | no | Home Assistant user name(s) you wouldn't like to display this order for. This option can be set alone or combined with `device`, `not_device`, `is_admin`, or `is_owner`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| device | String or String[] | no | Device(s) you would like to display this order for. E.g. ipad, iphone, macintosh, windows, android (it uses the device's [user-agent]). This option can be set alone or combined with `user`, `not_user`, `is_admin`, or `is_owner`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| not_device | String or String[] | no | Device(s) you wouldn't like to display this order for. E.g. ipad, iphone, macintosh, windows, android (it uses the device's [user-agent]). This option can be set alone or combined with `user`, `not_user` `is_admin`, or `is_owner`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| is_admin | Boolean | no | Checks if the user is admin or not. This option can be set alone or combined with `user`, `not_user`, `device`, `not_device`, or `is_owner`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |
| is_owner | Boolean | no | Checks if the user is owner of the system. This option can be set alone or combined with `user`, `not_user`, `device`, `not_device`, or `is_admin`. If it is used together with one of these options they will be taken as conditions separated by a logical `OR` |

Short example in `YAML` format:

Expand All @@ -332,12 +333,14 @@ exceptions:
- not_user:
- John Doe
- Jack Sparrow
is_admin: true
order:
...
- not_device: Android
order:
...
- is_admin: true
order:
...
```

Short example in `JSON` format:
Expand All @@ -356,7 +359,6 @@ Short example in `JSON` format:
},
{
"not_user": ["John Doe", "Jack Sparrow"],
"is_admin": true,
"order": [
...
]
Expand All @@ -366,6 +368,12 @@ Short example in `JSON` format:
"order": [
...
]
},
{
"is_admin": true,
"order": [
...
]
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion dist/custom-sidebar-json.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/custom-sidebar-yaml.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface BaseConfig extends SidebarColorConfig {

export interface ConfigExceptionBase extends BaseConfig {
is_admin?: boolean;
is_owner?: boolean;
}

interface ConfigExceptionUserInclude extends ConfigExceptionBase {
Expand Down
4 changes: 4 additions & 0 deletions src/utilities/modules/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class ConfigFlatter {
(
exception.is_admin !== undefined &&
exception.is_admin === this._user.is_admin
) ||
(
exception.is_owner !== undefined &&
exception.is_owner === this._user.is_owner
);
});
}
Expand Down
6 changes: 5 additions & 1 deletion src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,11 @@ const validateExceptionItem = (exception: ConfigException, config: Config): void

validateBooleanOptions(
exception,
['hide_all', 'is_admin'],
[
'hide_all',
'is_admin',
'is_owner'
],
`${ERROR_PREFIX}, exceptions`
);

Expand Down
232 changes: 232 additions & 0 deletions tests/07 - exceptions-options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -785,6 +785,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'it should match the user name regardless of is_owner being true',
json: {
exceptions: [
{
user: 'Test',
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'it should match the user name regardless of is_owner being false',
json: {
exceptions: [
{
user: 'Test',
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'shuld match a user if it is included in the user option as an array',
json: {
Expand Down Expand Up @@ -823,6 +849,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a user included in an user option as an array regardless of is_owner being true',
json: {
exceptions: [
{
user: ['ElChiniNet', 'Test', 'Palaus'],
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a user included in an user option as an array regardless of is_owner being false',
json: {
exceptions: [
{
user: ['ElChiniNet', 'Test', 'Palaus'],
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a user if it is not equal to the not_user option',
json: {
Expand Down Expand Up @@ -861,6 +913,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a user if the user name doesn\'t match with not_user regardless of is_owner being true',
json: {
exceptions: [
{
not_user: 'ElChiniNet',
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a user if the user name doesn\'t match with not_user regardless of is_owner being false',
json: {
exceptions: [
{
not_user: 'ElChiniNet',
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match an user if it is not included in the not_user option as an array',
json: {
Expand Down Expand Up @@ -899,6 +977,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match an user if it is not included in the not_user option as an array regardless of is_owner being true',
json: {
exceptions: [
{
not_user: ['ElChiniNet', 'Palaus'],
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match an user if it is not included in the not_user option as an array regardless of is_owner being false',
json: {
exceptions: [
{
not_user: ['ElChiniNet', 'Palaus'],
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device using the device option',
json: {
Expand Down Expand Up @@ -937,6 +1041,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device regardless of is_owner being true',
json: {
exceptions: [
{
device: 'Chrome',
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device regardless of is_owner being false',
json: {
exceptions: [
{
device: 'Chrome',
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is included in the device option as an array',
json: {
Expand Down Expand Up @@ -975,6 +1105,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is included in the device option as an array regardless of is_owner being true',
json: {
exceptions: [
{
device: ['Android', 'Chrome', 'iPad'],
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is included in the device option as an array regardless of is_owner being false',
json: {
exceptions: [
{
device: ['Android', 'Chrome', 'iPad'],
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is not equal to the not_device option',
json: {
Expand Down Expand Up @@ -1013,6 +1169,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is not equal to the not_device regardless of is_owner being true',
json: {
exceptions: [
{
not_device: 'iPad',
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is not equal to the not_device regardless of is_owner being false',
json: {
exceptions: [
{
not_device: 'iPad',
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is not included in the not_device option as an array',
json: {
Expand Down Expand Up @@ -1051,6 +1233,32 @@ test.describe('user and device matchers', () => {
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is not included in the not_device option as an array regardless of is_owner being true',
json: {
exceptions: [
{
not_device: ['iPad', 'Android'],
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match a device if it is not included in the not_device option as an array regardless of is_owner being false',
json: {
exceptions: [
{
not_device: ['iPad', 'Android'],
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should match an admin user if is_admin is true',
json: {
Expand All @@ -1074,6 +1282,30 @@ test.describe('user and device matchers', () => {
]
},
snapshot: 'sidebar-exceptions-no-match-admin-false.png'
},
{
title: 'should match an owner user if is_owner is true',
json: {
exceptions: [
{
is_owner: true,
...json
}
]
},
snapshot: 'sidebar-exceptions-match-success.png'
},
{
title: 'should not match an owner user if is_owner is false',
json: {
exceptions: [
{
is_owner: false,
...json
}
]
},
snapshot: 'sidebar-exceptions-no-match-admin-false.png'
}
].forEach(runTest);

Expand Down
Loading

0 comments on commit bd72986

Please sign in to comment.