Updated roles and permissions logic.

This commit is contained in:
Mike Cao 2022-12-06 18:36:41 -08:00
parent 4eb3140e43
commit b57ecf33e6
63 changed files with 432 additions and 546 deletions

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M216.464 36.465l-7.071 7.07c-4.686 4.686-4.686 12.284 0 16.971L387.887 239H12c-6.627 0-12 5.373-12 12v10c0 6.627 5.373 12 12 12h375.887L209.393 451.494c-4.686 4.686-4.686 12.284 0 16.971l7.071 7.07c4.686 4.686 12.284 4.686 16.97 0l211.051-211.05c4.686-4.686 4.686-12.284 0-16.971L233.434 36.465c-4.686-4.687-12.284-4.687-16.97 0z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="m216.464 36.465-7.071 7.07c-4.686 4.686-4.686 12.284 0 16.971L387.887 239H12c-6.627 0-12 5.373-12 12v10c0 6.627 5.373 12 12 12h375.887L209.393 451.494c-4.686 4.686-4.686 12.284 0 16.971l7.071 7.07c4.686 4.686 12.284 4.686 16.97 0l211.051-211.05c4.686-4.686 4.686-12.284 0-16.971L233.434 36.465c-4.686-4.687-12.284-4.687-16.97 0z"/></svg>

Before

Width:  |  Height:  |  Size: 409 B

After

Width:  |  Height:  |  Size: 408 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!-- Font Awesome Pro 6.0.0-alpha2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) --><path d="M392 320C378.75 320 368 330.75 368 344V456C368 460.406 364.406 464 360 464H56C51.594 464 48 460.406 48 456V152C48 147.594 51.594 144 56 144H168C181.25 144 192 133.25 192 120S181.25 96 168 96H56C25.125 96 0 121.125 0 152V456C0 486.875 25.125 512 56 512H360C390.875 512 416 486.875 416 456V344C416 330.75 405.25 320 392 320ZM488 0H320C306.75 0 296 10.75 296 24S306.75 48 320 48H430.062L183.031 295.031C173.656 304.406 173.656 319.594 183.031 328.969C187.719 333.656 193.844 336 200 336S212.281 333.656 216.969 328.969L464 81.938V192C464 205.25 474.75 216 488 216S512 205.25 512 192V24C512 10.75 501.25 0 488 0Z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M392 320c-13.25 0-24 10.75-24 24v112c0 4.406-3.594 8-8 8H56c-4.406 0-8-3.594-8-8V152c0-4.406 3.594-8 8-8h112c13.25 0 24-10.75 24-24s-10.75-24-24-24H56c-30.875 0-56 25.125-56 56v304c0 30.875 25.125 56 56 56h304c30.875 0 56-25.125 56-56V344c0-13.25-10.75-24-24-24ZM488 0H320c-13.25 0-24 10.75-24 24s10.75 24 24 24h110.062L183.031 295.031c-9.375 9.375-9.375 24.563 0 33.938A23.9 23.9 0 0 0 200 336a23.9 23.9 0 0 0 16.969-7.031L464 81.938V192c0 13.25 10.75 24 24 24s24-10.75 24-24V24c0-13.25-10.75-24-24-24Z"/></svg>

Before

Width:  |  Height:  |  Size: 831 B

After

Width:  |  Height:  |  Size: 583 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><!-- Font Awesome Pro 6.0.0-alpha2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) --><path d="M424 392H24C10.8 392 0 402.8 0 416V416C0 429.2 10.8 440 24 440H424C437.2 440 448 429.2 448 416V416C448 402.8 437.2 392 424 392ZM424 72H24C10.8 72 0 82.8 0 96V96C0 109.2 10.8 120 24 120H424C437.2 120 448 109.2 448 96V96C448 82.8 437.2 72 424 72ZM424 232H24C10.8 232 0 242.8 0 256V256C0 269.2 10.8 280 24 280H424C437.2 280 448 269.2 448 256V256C448 242.8 437.2 232 424 232Z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M424 392H24c-13.2 0-24 10.8-24 24s10.8 24 24 24h400c13.2 0 24-10.8 24-24s-10.8-24-24-24Zm0-320H24C10.8 72 0 82.8 0 96s10.8 24 24 24h400c13.2 0 24-10.8 24-24s-10.8-24-24-24Zm0 160H24c-13.2 0-24 10.8-24 24s10.8 24 24 24h400c13.2 0 24-10.8 24-24s-10.8-24-24-24Z"/></svg>

Before

Width:  |  Height:  |  Size: 594 B

After

Width:  |  Height:  |  Size: 338 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!-- Font Awesome Pro 5.15.1 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) --><path d="M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z"/></svg>

Before

Width:  |  Height:  |  Size: 885 B

After

Width:  |  Height:  |  Size: 748 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M435.848 83.466L172.804 346.51l-96.652-96.652c-4.686-4.686-12.284-4.686-16.971 0l-28.284 28.284c-4.686 4.686-4.686 12.284 0 16.971l133.421 133.421c4.686 4.686 12.284 4.686 16.971 0l299.813-299.813c4.686-4.686 4.686-12.284 0-16.971l-28.284-28.284c-4.686-4.686-12.284-4.686-16.97 0z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M435.848 83.466 172.804 346.51l-96.652-96.652c-4.686-4.686-12.284-4.686-16.971 0l-28.284 28.284c-4.686 4.686-4.686 12.284 0 16.971l133.421 133.421c4.686 4.686 12.284 4.686 16.971 0l299.813-299.813c4.686-4.686 4.686-12.284 0-16.971l-28.284-28.284c-4.686-4.686-12.284-4.686-16.97 0z"/></svg>

Before

Width:  |  Height:  |  Size: 360 B

After

Width:  |  Height:  |  Size: 360 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="M441.9 167.3l-19.8-19.8c-4.7-4.7-12.3-4.7-17 0L224 328.2 42.9 147.5c-4.7-4.7-12.3-4.7-17 0L6.1 167.3c-4.7 4.7-4.7 12.3 0 17l209.4 209.4c4.7 4.7 12.3 4.7 17 0l209.4-209.4c4.7-4.7 4.7-12.3 0-17z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512"><path d="m441.9 167.3-19.8-19.8c-4.7-4.7-12.3-4.7-17 0L224 328.2 42.9 147.5c-4.7-4.7-12.3-4.7-17 0L6.1 167.3c-4.7 4.7-4.7 12.3 0 17l209.4 209.4c4.7 4.7 12.3 4.7 17 0l209.4-209.4c4.7-4.7 4.7-12.3 0-17z"/></svg>

Before

Width:  |  Height:  |  Size: 272 B

After

Width:  |  Height:  |  Size: 271 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M234.8 511.7L196 500.4c-4.2-1.2-6.7-5.7-5.5-9.9L331.3 5.8c1.2-4.2 5.7-6.7 9.9-5.5L380 11.6c4.2 1.2 6.7 5.7 5.5 9.9L244.7 506.2c-1.2 4.3-5.6 6.7-9.9 5.5zm-83.2-121.1l27.2-29c3.1-3.3 2.8-8.5-.5-11.5L72.2 256l106.1-94.1c3.4-3 3.6-8.2.5-11.5l-27.2-29c-3-3.2-8.1-3.4-11.3-.4L2.5 250.2c-3.4 3.2-3.4 8.5 0 11.7L140.3 391c3.2 3 8.2 2.8 11.3-.4zm284.1.4l137.7-129.1c3.4-3.2 3.4-8.5 0-11.7L435.7 121c-3.2-3-8.3-2.9-11.3.4l-27.2 29c-3.1 3.3-2.8 8.5.5 11.5L503.8 256l-106.1 94.1c-3.4 3-3.6 8.2-.5 11.5l27.2 29c3.1 3.2 8.1 3.4 11.3.4z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M234.8 511.7 196 500.4c-4.2-1.2-6.7-5.7-5.5-9.9L331.3 5.8c1.2-4.2 5.7-6.7 9.9-5.5L380 11.6c4.2 1.2 6.7 5.7 5.5 9.9L244.7 506.2c-1.2 4.3-5.6 6.7-9.9 5.5zm-83.2-121.1 27.2-29c3.1-3.3 2.8-8.5-.5-11.5L72.2 256l106.1-94.1c3.4-3 3.6-8.2.5-11.5l-27.2-29c-3-3.2-8.1-3.4-11.3-.4L2.5 250.2c-3.4 3.2-3.4 8.5 0 11.7L140.3 391c3.2 3 8.2 2.8 11.3-.4zm284.1.4 137.7-129.1c3.4-3.2 3.4-8.5 0-11.7L435.7 121c-3.2-3-8.3-2.9-11.3.4l-27.2 29c-3.1 3.3-2.8 8.5.5 11.5L503.8 256l-106.1 94.1c-3.4 3-3.6 8.2-.5 11.5l27.2 29c3.1 3.2 8.1 3.4 11.3.4z"/></svg>

Before

Width:  |  Height:  |  Size: 601 B

After

Width:  |  Height:  |  Size: 601 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M497.6,0,334.4.17A14.4,14.4,0,0,0,320,14.57V47.88a14.4,14.4,0,0,0,14.69,14.4l73.63-2.72,2.06,2.06L131.52,340.49a12,12,0,0,0,0,17l23,23a12,12,0,0,0,17,0L450.38,101.62l2.06,2.06-2.72,73.63A14.4,14.4,0,0,0,464.12,192h33.31a14.4,14.4,0,0,0,14.4-14.4L512,14.4A14.4,14.4,0,0,0,497.6,0ZM432,288H416a16,16,0,0,0-16,16V458a6,6,0,0,1-6,6H54a6,6,0,0,1-6-6V118a6,6,0,0,1,6-6H208a16,16,0,0,0,16-16V80a16,16,0,0,0-16-16H48A48,48,0,0,0,0,112V464a48,48,0,0,0,48,48H400a48,48,0,0,0,48-48V304A16,16,0,0,0,432,288Z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M497.6 0 334.4.17a14.4 14.4 0 0 0-14.4 14.4v33.31a14.4 14.4 0 0 0 14.69 14.4l73.63-2.72 2.06 2.06-278.86 278.87a12 12 0 0 0 0 17l23 23a12 12 0 0 0 17 0l278.86-278.87 2.06 2.06-2.72 73.63a14.4 14.4 0 0 0 14.4 14.69h33.31a14.4 14.4 0 0 0 14.4-14.4L512 14.4A14.4 14.4 0 0 0 497.6 0ZM432 288h-16a16 16 0 0 0-16 16v154a6 6 0 0 1-6 6H54a6 6 0 0 1-6-6V118a6 6 0 0 1 6-6h154a16 16 0 0 0 16-16V80a16 16 0 0 0-16-16H48a48 48 0 0 0-48 48v352a48 48 0 0 0 48 48h352a48 48 0 0 0 48-48V304a16 16 0 0 0-16-16Z"/></svg>

Before

Width:  |  Height:  |  Size: 575 B

After

Width:  |  Height:  |  Size: 573 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><!-- Font Awesome Pro 6.0.0-alpha2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) --><path d="M504.265 315.978C504.265 307.326 499.658 299.134 491.906 294.586L458.998 275.615C459.643 269.099 459.966 262.549 459.966 256S459.643 242.901 458.998 236.385L491.906 217.414C499.658 212.866 504.265 204.674 504.265 196.022C504.265 174.755 454.947 67.846 419.746 67.846C415.502 67.846 411.236 68.939 407.379 71.203L374.599 90.172C363.888 82.43 352.533 75.848 340.531 70.428V32.488C340.531 21.262 333.047 11.453 322.205 8.613C300.654 2.871 278.425 0 256.181 0C233.935 0 211.675 2.871 190.06 8.613C179.218 11.453 171.734 21.262 171.734 32.488V70.428C159.732 75.848 148.377 82.43 137.666 90.172L104.886 71.203C101.031 68.939 96.763 67.846 92.519 67.846C92.517 67.846 92.514 67.846 92.512 67.846C60.048 67.846 8 169.591 8 196.022C8 204.674 12.607 212.866 20.359 217.414L53.267 236.385C52.622 242.901 52.299 249.451 52.299 256S52.622 269.099 53.267 275.615L20.359 294.586C12.607 299.134 8 307.326 8 315.978C8 337.245 57.318 444.154 92.519 444.154C96.763 444.154 101.029 443.061 104.886 440.797L137.666 421.828C148.377 429.57 159.732 436.152 171.734 441.572V479.512C171.734 490.738 179.218 500.547 190.06 503.387C211.611 509.129 233.84 512 256.084 512C278.33 512 300.59 509.129 322.205 503.387C333.047 500.547 340.531 490.738 340.531 479.512V441.572C352.533 436.152 363.888 429.57 374.599 421.828L407.379 440.797C411.234 443.061 415.502 444.154 419.746 444.154C452.209 444.154 504.265 342.423 504.265 315.978ZM415.361 389.959C391.561 376.186 404.101 383.444 371.705 364.695C329.649 395.09 339.375 389.426 292.531 410.582V460.82C279.236 463.161 266.948 464 256.093 464C240.669 464 228.14 462.306 219.734 460.824V410.582C172.779 389.376 182.552 395.044 140.56 364.695C108.748 383.105 117.896 377.811 96.924 389.949C81.181 371.256 68.849 349.895 60.517 326.84C81.643 314.663 72.361 320.014 104.088 301.723C101.549 276.083 100.277 266.079 100.277 256.04C100.277 246.018 101.545 235.96 104.088 210.277C72.198 191.892 81.571 197.295 60.504 185.152C68.818 162.109 81.187 140.686 96.904 122.041C120.704 135.814 108.164 128.556 140.56 147.305C182.616 116.91 172.89 122.574 219.734 101.418V51.18C233.029 48.839 245.318 48 256.172 48C271.597 48 284.126 49.694 292.531 51.176V101.418C339.486 122.624 329.713 116.956 371.705 147.305C405.655 127.657 394.228 134.27 415.343 122.051C431.084 140.744 443.416 162.105 451.748 185.16C430.622 197.337 439.904 191.986 408.177 210.277C410.716 235.917 411.988 245.921 411.988 255.96C411.988 265.982 410.72 276.04 408.177 301.723C440.067 320.108 430.694 314.705 451.761 326.848C443.447 349.891 431.078 371.314 415.361 389.959ZM256.133 160C203.258 160 160.133 203.125 160.133 256S203.258 352 256.133 352S352.133 308.875 352.133 256S309.008 160 256.133 160ZM256.133 304C229.666 304 208.133 282.467 208.133 256S229.666 208 256.133 208S304.133 229.533 304.133 256S282.599 304 256.133 304Z "></path></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M504.265 315.978c0-8.652-4.607-16.844-12.359-21.392l-32.908-18.971a199.182 199.182 0 0 0 0-39.23l32.908-18.971c7.752-4.548 12.359-12.74 12.359-21.392 0-21.267-49.318-128.176-84.519-128.176-4.244 0-8.51 1.093-12.367 3.357l-32.78 18.969a195.058 195.058 0 0 0-34.068-19.744v-37.94c0-11.226-7.484-21.035-18.326-23.875C300.654 2.871 278.425 0 256.181 0a257.698 257.698 0 0 0-66.121 8.613c-10.842 2.84-18.326 12.649-18.326 23.875v37.94a195.058 195.058 0 0 0-34.068 19.744l-32.78-18.969a24.36 24.36 0 0 0-12.367-3.357h-.007C60.048 67.846 8 169.591 8 196.022c0 8.652 4.607 16.844 12.359 21.392l32.908 18.971a199.182 199.182 0 0 0 0 39.23l-32.908 18.971C12.607 299.134 8 307.326 8 315.978c0 21.267 49.318 128.176 84.519 128.176 4.244 0 8.51-1.093 12.367-3.357l32.78-18.969a195.058 195.058 0 0 0 34.068 19.744v37.94c0 11.226 7.484 21.035 18.326 23.875 21.551 5.742 43.78 8.613 66.024 8.613 22.246 0 44.506-2.871 66.121-8.613 10.842-2.84 18.326-12.649 18.326-23.875v-37.94a195.058 195.058 0 0 0 34.068-19.744l32.78 18.969a24.36 24.36 0 0 0 12.367 3.357c32.463 0 84.519-101.731 84.519-128.176Zm-88.904 73.981c-23.8-13.773-11.26-6.515-43.656-25.264-42.056 30.395-32.33 24.731-79.174 45.887v50.238a210.138 210.138 0 0 1-36.438 3.18 208.924 208.924 0 0 1-36.359-3.176v-50.242c-46.955-21.206-37.182-15.538-79.174-45.887l-43.636 25.254a207.379 207.379 0 0 1-36.407-63.109c21.126-12.177 11.844-6.826 43.571-25.117-2.539-25.64-3.811-35.644-3.811-45.683 0-10.022 1.268-20.08 3.811-45.763-31.89-18.385-22.517-12.982-43.584-25.125a207.107 207.107 0 0 1 36.4-63.111c23.8 13.773 11.26 6.515 43.656 25.264 42.056-30.395 32.33-24.731 79.174-45.887V51.18A210.146 210.146 0 0 1 256.172 48c15.425 0 27.954 1.694 36.359 3.176v50.242c46.955 21.206 37.182 15.538 79.174 45.887l43.638-25.254a207.414 207.414 0 0 1 36.405 63.109c-21.126 12.177-11.844 6.826-43.571 25.117 2.539 25.64 3.811 35.644 3.811 45.683 0 10.022-1.268 20.08-3.811 45.763 31.89 18.385 22.517 12.982 43.584 25.125a207.107 207.107 0 0 1-36.4 63.111ZM256.133 160c-52.875 0-96 43.125-96 96s43.125 96 96 96 96-43.125 96-96-43.125-96-96-96Zm0 144c-26.467 0-48-21.533-48-48s21.533-48 48-48 48 21.533 48 48-21.534 48-48 48Z"/></svg>

Before

Width:  |  Height:  |  Size: 3.0 KiB

After

Width:  |  Height:  |  Size: 2.2 KiB

View File

@ -1,2 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 428 389.11" width="20" height="20"> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 428 389.11" width="20" height="20"><circle cx="214.15" cy="181" r="171" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="20"/><path d="M413 134.11H15.29a15 15 0 0 0-15 15v15.3C.12 168 0 171.52 0 175.11c0 118.19 95.81 214 214 214 116.4 0 211.1-92.94 213.93-208.67 0-.44.07-.88.07-1.33v-30a15 15 0 0 0-15-15Z"/></svg>
<circle cx="214.15" cy="181" r="171" fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="20"/><path d="M413,134.11H15.29a15,15,0,0,0-15,15v15.3C.12,168,0,171.52,0,175.11c0,118.19,95.81,214,214,214,116.4,0,211.1-92.94,213.93-208.67,0-.44.07-.88.07-1.33v-30A15,15,0,0,0,413,134.11Z"/></svg>

Before

Width:  |  Height:  |  Size: 401 B

After

Width:  |  Height:  |  Size: 390 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1399.98 1400"><path d="M562.44,837.55C335.89,611,288.08,273.54,418.71,0A734.31,734.31,0,0,0,215.54,143.73c-287.39,287.39-287.39,753.33,0,1040.72s753.33,287.4,1040.74,0A733.8,733.8,0,0,0,1400,981.29C1126.45,1111.92,789,1064.09,562.44,837.55Z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1399.98 1400"><path d="M562.44 837.55C335.89 611 288.08 273.54 418.71 0a734.31 734.31 0 0 0-203.17 143.73c-287.39 287.39-287.39 753.33 0 1040.72s753.33 287.4 1040.74 0A733.8 733.8 0 0 0 1400 981.29c-273.55 130.63-611 82.8-837.56-143.74Z"/></svg>

Before

Width:  |  Height:  |  Size: 302 B

After

Width:  |  Height:  |  Size: 298 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M493.26 56.26l-37.51-37.51C443.25 6.25 426.87 0 410.49 0s-32.76 6.25-45.25 18.74l-74.49 74.49L256 127.98 12.85 371.12.15 485.34C-1.45 499.72 9.88 512 23.95 512c.89 0 1.79-.05 2.69-.15l114.14-12.61L384.02 256l34.74-34.74 74.49-74.49c25-25 25-65.52.01-90.51zM118.75 453.39l-67.58 7.46 7.53-67.69 231.24-231.24 31.02-31.02 60.14 60.14-31.02 31.02-231.33 231.33zm340.56-340.57l-44.28 44.28-60.13-60.14 44.28-44.28c4.08-4.08 8.84-4.69 11.31-4.69s7.24.61 11.31 4.69l37.51 37.51c6.24 6.25 6.24 16.4 0 22.63z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="m493.26 56.26-37.51-37.51C443.25 6.25 426.87 0 410.49 0s-32.76 6.25-45.25 18.74l-74.49 74.49L256 127.98 12.85 371.12.15 485.34C-1.45 499.72 9.88 512 23.95 512c.89 0 1.79-.05 2.69-.15l114.14-12.61L384.02 256l34.74-34.74 74.49-74.49c25-25 25-65.52.01-90.51zM118.75 453.39l-67.58 7.46 7.53-67.69 231.24-231.24 31.02-31.02 60.14 60.14-31.02 31.02-231.33 231.33zm340.56-340.57-44.28 44.28-60.13-60.14 44.28-44.28c4.08-4.08 8.84-4.69 11.31-4.69s7.24.61 11.31 4.69l37.51 37.51c6.24 6.25 6.24 16.4 0 22.63z"/></svg>

Before

Width:  |  Height:  |  Size: 580 B

After

Width:  |  Height:  |  Size: 578 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1400 1400"><path d="M367.43,422.13a54.44,54.44,0,0,1-38.66-16L205,282.35A54.69,54.69,0,0,1,282.37,205L406.11,328.79a54.68,54.68,0,0,1-38.68,93.34Z"/><path d="M1156.3,1211a54.51,54.51,0,0,1-38.67-16L993.89,1071.21a54.68,54.68,0,1,1,77.34-77.33L1195,1117.65A54.7,54.7,0,0,1,1156.3,1211Z"/><path d="M243.7,1211A54.7,54.7,0,0,1,205,1117.65L328.74,993.89a54.69,54.69,0,0,1,77.36,77.32L282.37,1195A54.51,54.51,0,0,1,243.7,1211Z"/><path d="M1032.57,422.13a54.68,54.68,0,0,1-38.68-93.34L1117.61,205A54.69,54.69,0,0,1,1195,282.35L1071.23,406.11A54.44,54.44,0,0,1,1032.57,422.13Z"/><path d="M229.69,754.69h-175a54.69,54.69,0,0,1,0-109.38h175a54.69,54.69,0,0,1,0,109.38Z"/><path d="M1345.31,754.69h-175a54.69,54.69,0,0,1,0-109.38h175a54.69,54.69,0,0,1,0,109.38Z"/><path d="M700,1400a54.68,54.68,0,0,1-54.69-54.69v-175a54.69,54.69,0,0,1,109.38,0v175A54.68,54.68,0,0,1,700,1400Z"/><path d="M700,284.38a54.7,54.7,0,0,1-54.69-54.69v-175a54.69,54.69,0,0,1,109.38,0v175A54.7,54.7,0,0,1,700,284.38Z"/><circle cx="700" cy="700" r="306.25"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1400 1400"><path d="M367.43 422.13a54.44 54.44 0 0 1-38.66-16L205 282.35A54.69 54.69 0 0 1 282.37 205l123.74 123.79a54.68 54.68 0 0 1-38.68 93.34ZM1156.3 1211a54.51 54.51 0 0 1-38.67-16l-123.74-123.79a54.68 54.68 0 1 1 77.34-77.33L1195 1117.65a54.7 54.7 0 0 1-38.7 93.35ZM243.7 1211a54.7 54.7 0 0 1-38.7-93.35l123.74-123.76a54.69 54.69 0 0 1 77.36 77.32L282.37 1195a54.51 54.51 0 0 1-38.67 16ZM1032.57 422.13a54.68 54.68 0 0 1-38.68-93.34L1117.61 205a54.69 54.69 0 0 1 77.39 77.35l-123.77 123.76a54.44 54.44 0 0 1-38.66 16.02ZM229.69 754.69h-175a54.69 54.69 0 0 1 0-109.38h175a54.69 54.69 0 0 1 0 109.38ZM1345.31 754.69h-175a54.69 54.69 0 0 1 0-109.38h175a54.69 54.69 0 0 1 0 109.38ZM700 1400a54.68 54.68 0 0 1-54.69-54.69v-175a54.69 54.69 0 0 1 109.38 0v175A54.68 54.68 0 0 1 700 1400ZM700 284.38a54.7 54.7 0 0 1-54.69-54.69v-175a54.69 54.69 0 0 1 109.38 0v175A54.7 54.7 0 0 1 700 284.38Z"/><circle cx="700" cy="700" r="306.25"/></svg>

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 989 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path d="M207.6 256l107.72-107.72c6.23-6.23 6.23-16.34 0-22.58l-25.03-25.03c-6.23-6.23-16.34-6.23-22.58 0L160 208.4 52.28 100.68c-6.23-6.23-16.34-6.23-22.58 0L4.68 125.7c-6.23 6.23-6.23 16.34 0 22.58L112.4 256 4.68 363.72c-6.23 6.23-6.23 16.34 0 22.58l25.03 25.03c6.23 6.23 16.34 6.23 22.58 0L160 303.6l107.72 107.72c6.23 6.23 16.34 6.23 22.58 0l25.03-25.03c6.23-6.23 6.23-16.34 0-22.58L207.6 256z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path d="m207.6 256 107.72-107.72c6.23-6.23 6.23-16.34 0-22.58l-25.03-25.03c-6.23-6.23-16.34-6.23-22.58 0L160 208.4 52.28 100.68c-6.23-6.23-16.34-6.23-22.58 0L4.68 125.7c-6.23 6.23-6.23 16.34 0 22.58L112.4 256 4.68 363.72c-6.23 6.23-6.23 16.34 0 22.58l25.03 25.03c6.23 6.23 16.34 6.23 22.58 0L160 303.6l107.72 107.72c6.23 6.23 16.34 6.23 22.58 0l25.03-25.03c6.23-6.23 6.23-16.34 0-22.58L207.6 256z"/></svg>

Before

Width:  |  Height:  |  Size: 468 B

After

Width:  |  Height:  |  Size: 468 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1652 1652"><title>Asset 1</title><g id="Layer_2" data-name="Layer 2"><g id="Layer_1-2" data-name="Layer 1"><path d="M1587.07,504.47A828.56,828.56,0,1,0,1652,826,823.13,823.13,0,0,0,1587.07,504.47ZM826,1577a747.29,747.29,0,0,1-464.48-161.26,39.94,39.94,0,0,0,2.8-11.35,458.82,458.82,0,0,1,34.29-135.74,464.15,464.15,0,0,1,854.78,0,458.82,458.82,0,0,1,34.29,135.74,39.94,39.94,0,0,0,2.8,11.35A747.29,747.29,0,0,1,826,1577ZM719.81,866.57A274,274,0,1,1,826,888,272.1,272.1,0,0,1,719.81,866.57Zm641.28,485.87c-36.11-201.1-182.78-363.82-374.86-423,114.28-58.37,192.53-177.22,192.53-314.35,0-194.83-157.94-352.76-352.76-352.76S473.24,420.29,473.24,615.12c0,137.13,78.25,256,192.53,314.35-192.08,59.15-338.75,221.87-374.86,423C157.46,1216.81,75,1030.86,75,826,75,411.9,411.9,75,826,75s751,336.9,751,751C1577,1030.86,1494.54,1216.81,1361.09,1352.44Z"/></g></g></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1652 1652"><g data-name="Layer 2"><path d="M1587.07 504.47A828.56 828.56 0 1 0 1652 826a823.13 823.13 0 0 0-64.93-321.53ZM826 1577a747.29 747.29 0 0 1-464.48-161.26 39.94 39.94 0 0 0 2.8-11.35 458.82 458.82 0 0 1 34.29-135.74 464.15 464.15 0 0 1 854.78 0 458.82 458.82 0 0 1 34.29 135.74 39.94 39.94 0 0 0 2.8 11.35A747.29 747.29 0 0 1 826 1577ZM719.81 866.57A274 274 0 1 1 826 888a272.1 272.1 0 0 1-106.19-21.43Zm641.28 485.87c-36.11-201.1-182.78-363.82-374.86-423 114.28-58.37 192.53-177.22 192.53-314.35 0-194.83-157.94-352.76-352.76-352.76S473.24 420.29 473.24 615.12c0 137.13 78.25 256 192.53 314.35-192.08 59.15-338.75 221.87-374.86 423C157.46 1216.81 75 1030.86 75 826 75 411.9 411.9 75 826 75s751 336.9 751 751c0 204.86-82.46 390.81-215.91 526.44Z" data-name="Layer 1"/></g></svg>

Before

Width:  |  Height:  |  Size: 910 B

After

Width:  |  Height:  |  Size: 841 B

View File

@ -1 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><!-- Font Awesome Pro 6.0.0-alpha2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) --><path d="M312.973 375.032C322.342 384.401 322.342 399.604 312.973 408.973S288.401 418.342 279.032 408.973L160 289.941L40.968 408.973C31.599 418.342 16.396 418.342 7.027 408.973S-2.342 384.401 7.027 375.032L126.059 256L7.027 136.968C-2.342 127.599 -2.342 112.396 7.027 103.027S31.599 93.658 40.968 103.027L160 222.059L279.032 103.027C288.401 93.658 303.604 93.658 312.973 103.027S322.342 127.599 312.973 136.968L193.941 256L312.973 375.032Z"/></svg> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512"><path d="M312.973 375.032c9.369 9.369 9.369 24.572 0 33.941s-24.572 9.369-33.941 0L160 289.941 40.968 408.973c-9.369 9.369-24.572 9.369-33.941 0s-9.369-24.572 0-33.941L126.059 256 7.027 136.968c-9.369-9.369-9.369-24.572 0-33.941s24.572-9.369 33.941 0L160 222.059l119.032-119.032c9.369-9.369 24.572-9.369 33.941 0s9.369 24.572 0 33.941L193.941 256l119.032 119.032Z"/></svg>

Before

Width:  |  Height:  |  Size: 653 B

After

Width:  |  Height:  |  Size: 434 B

View File

@ -1,4 +1,3 @@
import React from 'react';
import Head from 'next/head'; import Head from 'next/head';
import Header from 'components/layout/Header'; import Header from 'components/layout/Header';
import Footer from 'components/layout/Footer'; import Footer from 'components/layout/Footer';
@ -10,9 +9,8 @@ export default function Layout({ title, children, header = true, footer = true }
return ( return (
<> <>
<Head> <Head>
<title>umami{title && ` - ${title}`}</title> <title>{title ? `${title} | umami` : 'umami'}</title>
</Head> </Head>
{header && <Header />} {header && <Header />}
<main>{children}</main> <main>{children}</main>
{footer && <Footer />} {footer && <Footer />}

View File

@ -8,7 +8,6 @@ import GridLayout, { GridRow, GridColumn } from 'components/layout/GridLayout';
import MenuLayout from 'components/layout/MenuLayout'; import MenuLayout from 'components/layout/MenuLayout';
import Link from 'components/common/Link'; import Link from 'components/common/Link';
import Loading from 'components/common/Loading'; import Loading from 'components/common/Loading';
import Arrow from 'assets/arrow-right.svg';
import PagesTable from 'components/metrics/PagesTable'; import PagesTable from 'components/metrics/PagesTable';
import ReferrersTable from 'components/metrics/ReferrersTable'; import ReferrersTable from 'components/metrics/ReferrersTable';
import BrowsersTable from 'components/metrics/BrowsersTable'; import BrowsersTable from 'components/metrics/BrowsersTable';
@ -20,11 +19,12 @@ import EventsTable from 'components/metrics/EventsTable';
import EventsChart from 'components/metrics/EventsChart'; import EventsChart from 'components/metrics/EventsChart';
import ScreenTable from 'components/metrics/ScreenTable'; import ScreenTable from 'components/metrics/ScreenTable';
import QueryParametersTable from 'components/metrics/QueryParametersTable'; import QueryParametersTable from 'components/metrics/QueryParametersTable';
import EventDataButton from 'components/common/EventDataButton';
import useFetch from 'hooks/useFetch'; import useFetch from 'hooks/useFetch';
import usePageQuery from 'hooks/usePageQuery'; import usePageQuery from 'hooks/usePageQuery';
import { DEFAULT_ANIMATION_DURATION } from 'lib/constants'; import { DEFAULT_ANIMATION_DURATION } from 'lib/constants';
import Arrow from 'assets/arrow-right.svg';
import styles from './WebsiteDetails.module.css'; import styles from './WebsiteDetails.module.css';
import EventDataButton from 'components/common/EventDataButton';
const messages = defineMessages({ const messages = defineMessages({
pages: { id: 'metrics.pages', defaultMessage: 'Pages' }, pages: { id: 'metrics.pages', defaultMessage: 'Pages' },

View File

@ -1,8 +0,0 @@
export interface Auth {
user: {
id: string;
username: string;
isAdmin: boolean;
};
shareToken: string;
}

View File

@ -1,66 +0,0 @@
export interface Website {
id: string;
userId: string;
revId: number;
name: string;
domain: string;
shareId: string;
createdAt: Date;
}
export interface Share {
id: string;
token: string;
}
export interface Empty {}
export interface WebsiteActive {
x: number;
}
export interface WebsiteEventDataMetric {
[key: string]: number;
}
export interface WebsiteMetric {
x: string;
y: number;
}
export interface WebsiteEventMetric {
x: string;
t: string;
y: number;
}
export interface WebsitePageviews {
pageviews: {
t: string;
y: number;
};
sessions: {
t: string;
y: number;
};
}
export interface WebsiteStats {
pageviews: { value: number; change: number };
uniques: { value: number; change: number };
bounces: { value: number; change: number };
totalTime: { value: number; change: number };
}
export interface RealtimeInit {
websites: Website[];
token: string;
data: RealtimeUpdate;
}
export interface RealtimeUpdate {
pageviews: any[];
sessions: any[];
events: any[];
timestamp: number;
}

View File

@ -1,14 +0,0 @@
import { NextApiRequest } from 'next';
import { Auth } from './auth';
export interface NextApiRequestQueryBody<TQuery = any, TBody = any> extends NextApiRequest {
auth?: Auth;
query: TQuery & { [key: string]: string | string[] };
body: TBody;
headers: any;
}
export interface NextApiRequestAuth extends NextApiRequest {
auth?: Auth;
headers: any;
}

View File

View File

@ -1,11 +1,9 @@
import { parseSecureToken, parseToken } from 'next-basics'; import { parseSecureToken, parseToken, ensureArray } from 'next-basics';
import { UserRole } from '@prisma/client';
import debug from 'debug'; import debug from 'debug';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { SHARE_TOKEN_HEADER } from 'lib/constants'; import { SHARE_TOKEN_HEADER, PERMISSIONS, ROLE_PERMISSIONS } from 'lib/constants';
import { secret } from 'lib/crypto'; import { secret } from 'lib/crypto';
import { Permission, Roles } from 'lib/types'; import { getTeamUser } from 'queries';
import { getTeamUser, getUserRoles } from 'queries';
const log = debug('umami:auth'); const log = debug('umami:auth');
@ -58,12 +56,10 @@ export async function canViewWebsite(userId: string, websiteId: string) {
} }
if (website.teamId) { if (website.teamId) {
const teamUser = await getTeamUser({ userId, teamId: website.teamId }); return getTeamUser(website.teamId, userId);
checkPermission(Permission.websiteUpdate, teamUser.role);
} }
return checkAdmin(userId); return false;
} }
export async function canUpdateWebsite(userId: string, websiteId: string) { export async function canUpdateWebsite(userId: string, websiteId: string) {
@ -74,12 +70,12 @@ export async function canUpdateWebsite(userId: string, websiteId: string) {
} }
if (website.teamId) { if (website.teamId) {
const teamUser = await getTeamUser({ userId, teamId: website.teamId }); const teamUser = await getTeamUser(website.teamId, userId);
checkPermission(Permission.websiteUpdate, teamUser.role); return hasPermission(teamUser.role, PERMISSIONS.websiteUpdate);
} }
return checkAdmin(userId); return false;
} }
export async function canDeleteWebsite(userId: string, websiteId: string) { export async function canDeleteWebsite(userId: string, websiteId: string) {
@ -90,14 +86,12 @@ export async function canDeleteWebsite(userId: string, websiteId: string) {
} }
if (website.teamId) { if (website.teamId) {
const teamUser = await getTeamUser({ userId, teamId: website.teamId }); const teamUser = await getTeamUser(website.teamId, userId);
if (checkPermission(Permission.websiteDelete, teamUser.role)) { return hasPermission(teamUser.role, PERMISSIONS.websiteDelete);
return true;
}
} }
return checkAdmin(userId); return false;
} }
// To-do: Implement when payments are setup. // To-do: Implement when payments are setup.
@ -107,66 +101,29 @@ export async function canCreateTeam(userId: string) {
// To-do: Implement when payments are setup. // To-do: Implement when payments are setup.
export async function canViewTeam(userId: string, teamId) { export async function canViewTeam(userId: string, teamId) {
const teamUser = await getTeamUser({ userId, teamId }); return getTeamUser(teamId, userId);
return !!teamUser;
} }
export async function canUpdateTeam(userId: string, teamId: string) { export async function canUpdateTeam(userId: string, teamId: string) {
const teamUser = await getTeamUser({ userId, teamId }); const teamUser = await getTeamUser(teamId, userId);
if (checkPermission(Permission.teamUpdate, teamUser.role)) { return hasPermission(teamUser.role, PERMISSIONS.teamUpdate);
return true;
}
} }
export async function canDeleteTeam(userId: string, teamId: string) { export async function canDeleteTeam(userId: string, teamId: string) {
const teamUser = await getTeamUser({ userId, teamId }); const teamUser = await getTeamUser(teamId, userId);
if (checkPermission(Permission.teamDelete, teamUser.role)) { return hasPermission(teamUser.role, PERMISSIONS.teamDelete);
return true;
}
}
export async function canCreateUser(userId: string) {
return checkAdmin(userId);
} }
export async function canViewUser(userId: string, viewedUserId: string) { export async function canViewUser(userId: string, viewedUserId: string) {
if (userId === viewedUserId) { return userId === viewedUserId;
return true;
}
return checkAdmin(userId);
}
export async function canViewUsers(userId: string) {
return checkAdmin(userId);
} }
export async function canUpdateUser(userId: string, viewedUserId: string) { export async function canUpdateUser(userId: string, viewedUserId: string) {
if (userId === viewedUserId) { return userId === viewedUserId;
return true;
}
return checkAdmin(userId);
} }
export async function canUpdateUserRole(userId: string) { export async function hasPermission(role: string, permission: string | string[]) {
return checkAdmin(userId); return ensureArray(permission).some(e => ROLE_PERMISSIONS[role]?.includes(e));
}
export async function canDeleteUser(userId: string) {
return checkAdmin(userId);
}
export async function checkPermission(permission: Permission, role: string) {
return Roles[role].permissions.some(a => a === permission);
}
export async function checkAdmin(userId: string, userRoles?: UserRole[]) {
if (!userRoles) {
userRoles = await getUserRoles({ userId });
}
return userRoles.some(a => a.role === Roles.admin.name);
} }

View File

@ -22,6 +22,52 @@ export const DEFAULT_WEBSITE_LIMIT = 10;
export const REALTIME_RANGE = 30; export const REALTIME_RANGE = 30;
export const REALTIME_INTERVAL = 3000; export const REALTIME_INTERVAL = 3000;
export const EVENT_TYPE = {
pageView: 1,
customEvent: 2,
};
export const ROLES = {
admin: 'admin',
user: 'user',
teamOwner: 'team-owner',
teamMember: 'team-member',
teamGuest: 'team-guest',
};
export const PERMISSIONS = {
all: 'all',
websiteCreate: 'website:create',
websiteUpdate: 'website:update',
websiteDelete: 'website:delete',
teamCreate: 'team:create',
teamUpdate: 'team:update',
teamDelete: 'team:delete',
};
export const ROLE_PERMISSIONS = {
[ROLES.admin]: [PERMISSIONS.all],
[ROLES.user]: [
PERMISSIONS.websiteCreate,
PERMISSIONS.websiteUpdate,
PERMISSIONS.websiteDelete,
PERMISSIONS.teamCreate,
],
[ROLES.teamOwner]: [
PERMISSIONS.teamUpdate,
PERMISSIONS.teamDelete,
PERMISSIONS.websiteCreate,
PERMISSIONS.websiteUpdate,
PERMISSIONS.websiteDelete,
],
[ROLES.teamMember]: [
PERMISSIONS.websiteCreate,
PERMISSIONS.websiteUpdate,
PERMISSIONS.websiteDelete,
],
[ROLES.teamGuest]: [],
};
export const THEME_COLORS = { export const THEME_COLORS = {
light: { light: {
primary: '#2680eb', primary: '#2680eb',

View File

@ -6,6 +6,7 @@ import { findSession } from 'lib/session';
import { getAuthToken, parseShareToken } from 'lib/auth'; import { getAuthToken, parseShareToken } from 'lib/auth';
import { secret } from 'lib/crypto'; import { secret } from 'lib/crypto';
import redis from 'lib/redis'; import redis from 'lib/redis';
import { ROLES } from 'lib/constants';
import { getUser } from '../queries'; import { getUser } from '../queries';
const log = debug('umami:middleware'); const log = debug('umami:middleware');
@ -45,6 +46,10 @@ export const useAuth = createMiddleware(async (req, res, next) => {
return unauthorized(res); return unauthorized(res);
} }
if (user) {
user.isAdmin = user.role === ROLES.admin;
}
req.auth = { user, token, shareToken, key }; req.auth = { user, token, shareToken, key };
next(); next();
}); });

View File

@ -32,8 +32,6 @@ async function get(key) {
const data = await redis.get(key); const data = await redis.get(key);
log({ key, data });
try { try {
return JSON.parse(data); return JSON.parse(data);
} catch { } catch {

View File

@ -40,7 +40,7 @@ export async function findSession(req) {
website = await getWebsite({ id: websiteId }); website = await getWebsite({ id: websiteId });
} }
if (!website || website.isDeleted) { if (!website || website.deletedAt) {
throw new Error(`Website not found: ${websiteId}`); throw new Error(`Website not found: ${websiteId}`);
} }

View File

@ -1,57 +1,90 @@
/* eslint-disable no-unused-vars */ import { NextApiRequest } from 'next';
export enum EventType {
Pageview = 1, export interface Auth {
Event = 2, user?: {
id: string;
username: string;
role: string;
isAdmin: boolean;
};
shareToken?: string;
} }
export enum AuthType { export interface NextApiRequestQueryBody<TQuery = any, TBody = any> extends NextApiRequest {
Website, auth?: Auth;
User, query: TQuery & { [key: string]: string | string[] };
Team, body: TBody;
headers: any;
} }
export enum Permission { export interface NextApiRequestAuth extends NextApiRequest {
all = 'all', auth?: Auth;
websiteCreate = 'website:create', headers: any;
websiteUpdate = 'website:update',
websiteDelete = 'website:delete',
teamCreate = 'team:create',
teamUpdate = 'team:update',
teamDelete = 'team:delete',
} }
export enum Role { export interface Website {
Admin = 'admin', id: string;
User = 'user', userId: string;
TeamOwner = 'team-owner', revId: number;
TeamMember = 'team-member', name: string;
TeamGuest = 'team-guest', domain: string;
shareId: string;
createdAt: Date;
} }
export const Roles = { export interface Share {
admin: { name: Role.Admin, permissions: [Permission.all] }, id: string;
member: { token: string;
name: Role.User, }
permissions: [
Permission.websiteCreate, export interface Empty {}
Permission.websiteUpdate,
Permission.websiteDelete, export interface WebsiteActive {
Permission.teamCreate, x: number;
], }
},
teamOwner: { export interface WebsiteEventDataMetric {
name: Role.TeamOwner, [key: string]: number;
permissions: [ }
Permission.teamUpdate,
Permission.teamDelete, export interface WebsiteMetric {
Permission.websiteCreate, x: string;
Permission.websiteUpdate, y: number;
Permission.websiteDelete, }
],
}, export interface WebsiteEventMetric {
teamMember: { x: string;
name: Role.TeamMember, t: string;
permissions: [Permission.websiteCreate, Permission.websiteUpdate, Permission.websiteDelete], y: number;
}, }
teamGuest: { name: Role.TeamGuest, permissions: [] },
}; export interface WebsitePageviews {
pageviews: {
t: string;
y: number;
};
sessions: {
t: string;
y: number;
};
}
export interface WebsiteStats {
pageviews: { value: number; change: number };
uniques: { value: number; change: number };
bounces: { value: number; change: number };
totalTime: { value: number; change: number };
}
export interface RealtimeInit {
websites: Website[];
token: string;
data: RealtimeUpdate;
}
export interface RealtimeUpdate {
pageviews: any[];
sessions: any[];
events: any[];
timestamp: number;
}

View File

@ -10,7 +10,7 @@ import {
import { getUser, User } from 'queries'; import { getUser, User } from 'queries';
import { secret } from 'lib/crypto'; import { secret } from 'lib/crypto';
import redis from 'lib/redis'; import redis from 'lib/redis';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
export interface LoginRequestBody { export interface LoginRequestBody {

View File

@ -1,4 +1,4 @@
import { NextApiRequestAuth } from 'interface/api/nextApi'; import { NextApiRequestAuth } from 'lib/types';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { ok } from 'next-basics'; import { ok } from 'next-basics';

View File

@ -1,6 +1,6 @@
import { subMinutes } from 'date-fns'; import { subMinutes } from 'date-fns';
import { RealtimeInit } from 'interface/api/models'; import { RealtimeInit } from 'lib/types';
import { NextApiRequestAuth } from 'interface/api/nextApi'; import { NextApiRequestAuth } from 'lib/types';
import { secret } from 'lib/crypto'; import { secret } from 'lib/crypto';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -3,9 +3,9 @@ import { useAuth } from 'lib/middleware';
import { getRealtimeData } from 'queries'; import { getRealtimeData } from 'queries';
import { SHARE_TOKEN_HEADER } from 'lib/constants'; import { SHARE_TOKEN_HEADER } from 'lib/constants';
import { secret } from 'lib/crypto'; import { secret } from 'lib/crypto';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { RealtimeUpdate } from 'interface/api/models'; import { RealtimeUpdate } from 'lib/types';
export interface InitUpdateRequestQuery { export interface InitUpdateRequestQuery {
start_at: string; start_at: string;

View File

@ -1,4 +1,4 @@
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { secret } from 'lib/crypto'; import { secret } from 'lib/crypto';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { createToken, methodNotAllowed, notFound, ok } from 'next-basics'; import { createToken, methodNotAllowed, notFound, ok } from 'next-basics';

View File

@ -1,5 +1,5 @@
import { Team } from '@prisma/client'; import { Team } from '@prisma/client';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canDeleteTeam, canUpdateTeam, canViewTeam } from 'lib/auth'; import { canDeleteTeam, canUpdateTeam, canViewTeam } from 'lib/auth';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
@ -26,7 +26,7 @@ export default async (
const { id: teamId } = req.query; const { id: teamId } = req.query;
if (req.method === 'GET') { if (req.method === 'GET') {
if (await canViewTeam(userId, teamId)) { if (!(await canViewTeam(userId, teamId))) {
return unauthorized(res); return unauthorized(res);
} }
@ -38,7 +38,7 @@ export default async (
if (req.method === 'POST') { if (req.method === 'POST') {
const { name } = req.body; const { name } = req.body;
if (await canUpdateTeam(userId, teamId)) { if (!(await canUpdateTeam(userId, teamId))) {
return unauthorized(res, 'You must be the owner of this team.'); return unauthorized(res, 'You must be the owner of this team.');
} }
@ -48,7 +48,7 @@ export default async (
} }
if (req.method === 'DELETE') { if (req.method === 'DELETE') {
if (await canDeleteTeam(userId, teamId)) { if (!(await canDeleteTeam(userId, teamId))) {
return unauthorized(res, 'You must be the owner of this team.'); return unauthorized(res, 'You must be the owner of this team.');
} }

View File

@ -1,9 +1,9 @@
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canUpdateTeam, canViewTeam } from 'lib/auth'; import { canUpdateTeam, canViewTeam } from 'lib/auth';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeamUser, deleteTeamUser, getUser, getUsersByTeamId } from 'queries'; import { createTeamUser, deleteTeamUser, getUser, getTeamUsers } from 'queries';
export interface TeamUserRequestQuery { export interface TeamUserRequestQuery {
id: string; id: string;
@ -27,17 +27,17 @@ export default async (
const { id: teamId } = req.query; const { id: teamId } = req.query;
if (req.method === 'GET') { if (req.method === 'GET') {
if (await canViewTeam(userId, teamId)) { if (!(await canViewTeam(userId, teamId))) {
return unauthorized(res); return unauthorized(res);
} }
const user = await getUsersByTeamId({ teamId }); const users = await getTeamUsers(teamId);
return ok(res, user); return ok(res, users);
} }
if (req.method === 'POST') { if (req.method === 'POST') {
if (await canUpdateTeam(userId, teamId)) { if (!(await canUpdateTeam(userId, teamId))) {
return unauthorized(res, 'You must be the owner of this team.'); return unauthorized(res, 'You must be the owner of this team.');
} }

View File

@ -1,9 +1,9 @@
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
import { canViewTeam } from 'lib/auth';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok, unauthorized } from 'next-basics'; import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { getWebsitesByTeamId } from 'queries'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewTeam } from 'lib/auth';
import { useAuth } from 'lib/middleware';
import { getTeamWebsites } from 'queries/admin/team';
export interface TeamWebsiteRequestQuery { export interface TeamWebsiteRequestQuery {
id: string; id: string;
@ -30,9 +30,9 @@ export default async (
return unauthorized(res); return unauthorized(res);
} }
const website = await getWebsitesByTeamId({ teamId }); const websites = await getTeamWebsites(teamId);
return ok(res, website); return ok(res, websites);
} }
return methodNotAllowed(res); return methodNotAllowed(res);

View File

@ -1,14 +1,14 @@
import { Team } from '@prisma/client'; import { Team } from '@prisma/client';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canCreateTeam } from 'lib/auth'; import { canCreateTeam } from 'lib/auth';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createTeam, getTeam, getTeamsByUserId } from 'queries'; import { createTeam, getUserTeams } from 'queries';
export interface TeamsRequestBody { export interface TeamsRequestBody {
name: string; name: string;
description: string;
} }
export default async ( export default async (
@ -22,27 +22,22 @@ export default async (
} = req.auth; } = req.auth;
if (req.method === 'GET') { if (req.method === 'GET') {
const teams = await getTeamsByUserId(userId); const teams = await getUserTeams(userId);
return ok(res, teams); return ok(res, teams);
} }
if (req.method === 'POST') { if (req.method === 'POST') {
if (await canCreateTeam(userId)) { if (!(await canCreateTeam(userId))) {
return unauthorized(res); return unauthorized(res);
} }
const { name } = req.body; const { name } = req.body;
const team = await getTeam({ name });
if (team) {
return badRequest(res, 'Team already exists');
}
const created = await createTeam({ const created = await createTeam({
id: uuid(), id: uuid(),
name, name,
userId,
}); });
return ok(res, created); return ok(res, created);

View File

@ -1,5 +1,5 @@
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canDeleteUser, canUpdateUser, canViewUser, checkAdmin } from 'lib/auth'; import { canUpdateUser, canViewUser } from 'lib/auth';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
@ -21,7 +21,7 @@ export default async (
await useAuth(req, res); await useAuth(req, res);
const { const {
user: { id: userId }, user: { id: userId, isAdmin },
} = req.auth; } = req.auth;
const { id } = req.query; const { id } = req.query;
@ -51,7 +51,7 @@ export default async (
} }
// Only admin can change these fields // Only admin can change these fields
if (username && (await checkAdmin(userId))) { if (username && isAdmin) {
data.username = username; data.username = username;
} }
@ -70,7 +70,7 @@ export default async (
} }
if (req.method === 'DELETE') { if (req.method === 'DELETE') {
if (canDeleteUser(userId)) { if (isAdmin) {
return unauthorized(res); return unauthorized(res);
} }

View File

@ -1,4 +1,4 @@
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canUpdateUser } from 'lib/auth'; import { canUpdateUser } from 'lib/auth';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,62 +0,0 @@
import { UserRole } from '@prisma/client';
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
import { canUpdateUserRole } from 'lib/auth';
import { Role } from 'lib/types';
import { useAuth } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { badRequest, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { deleteUserRole, getUserRole, getUserRoles, updateUserRole } from 'queries';
export interface UserRoleRequestQuery {
id: string;
}
export interface UserRoleRequestBody {
role: Role;
userRoleId?: string;
}
export default async (
req: NextApiRequestQueryBody<UserRoleRequestQuery, UserRoleRequestBody>,
res: NextApiResponse<UserRole>,
) => {
await useAuth(req, res);
const {
user: { id: userId },
} = req.auth;
const { id } = req.query;
if (await canUpdateUserRole(userId)) {
return unauthorized(res);
}
if (req.method === 'GET') {
const userRole = await getUserRoles({ userId: id });
return ok(res, userRole);
}
if (req.method === 'POST') {
const { role } = req.body;
const userRole = await getUserRole({ userId: id });
if (userRole && userRole.role === role) {
return badRequest(res, 'Role already exists for User.');
} else {
const updated = await updateUserRole({ role }, { id: userRole.id });
return ok(res, updated);
}
}
if (req.method === 'DELETE') {
const { userRoleId } = req.body;
const updated = await deleteUserRole(userRoleId);
return ok(res, updated);
}
return methodNotAllowed(res);
};

View File

@ -0,0 +1,57 @@
import { Prisma } from '@prisma/client';
import { NextApiRequestQueryBody } from 'lib/types';
import { uuid } from 'lib/crypto';
import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next';
import { methodNotAllowed, ok } from 'next-basics';
import { createWebsite, getUserWebsites } from 'queries';
export interface WebsitesRequestQuery {}
export interface WebsitesRequestBody {
name: string;
domain: string;
shareId: string;
teamId?: string;
}
export default async (
req: NextApiRequestQueryBody<WebsitesRequestQuery, WebsitesRequestBody>,
res: NextApiResponse,
) => {
await useCors(req, res);
await useAuth(req, res);
const {
user: { id: userId },
} = req.auth;
if (req.method === 'GET') {
const websites = await getUserWebsites(userId);
return ok(res, websites);
}
if (req.method === 'POST') {
const { name, domain, shareId, teamId } = req.body;
const data: Prisma.WebsiteUncheckedCreateInput = {
id: uuid(),
name,
domain,
shareId,
};
if (teamId) {
data.teamId = teamId;
} else {
data.userId = userId;
}
const website = await createWebsite(data);
return ok(res, website);
}
return methodNotAllowed(res);
};

View File

@ -1,7 +1,7 @@
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canCreateUser, canViewUsers } from 'lib/auth';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { useAuth } from 'lib/middleware'; import { useAuth } from 'lib/middleware';
import { ROLES } from 'lib/constants';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics'; import { badRequest, hashPassword, methodNotAllowed, ok, unauthorized } from 'next-basics';
import { createUser, getUser, getUsers, User } from 'queries'; import { createUser, getUser, getUsers, User } from 'queries';
@ -19,11 +19,11 @@ export default async (
await useAuth(req, res); await useAuth(req, res);
const { const {
user: { id: userId }, user: { isAdmin },
} = req.auth; } = req.auth;
if (req.method === 'GET') { if (req.method === 'GET') {
if (canViewUsers(userId)) { if (isAdmin) {
return unauthorized(res); return unauthorized(res);
} }
@ -33,15 +33,15 @@ export default async (
} }
if (req.method === 'POST') { if (req.method === 'POST') {
if (canCreateUser(userId)) { if (isAdmin) {
return unauthorized(res); return unauthorized(res);
} }
const { username, password, id } = req.body; const { username, password, id } = req.body;
const user = await getUser({ username }); const existingUser = await getUser({ username });
if (user) { if (existingUser) {
return badRequest(res, 'User already exists'); return badRequest(res, 'User already exists');
} }
@ -49,6 +49,7 @@ export default async (
id: id || uuid(), id: id || uuid(),
username, username,
password: hashPassword(password), password: hashPassword(password),
role: ROLES.user,
}); });
return ok(res, created); return ok(res, created);

View File

@ -1,5 +1,5 @@
import { WebsiteActive } from 'interface/api/models'; import { WebsiteActive } from 'lib/types';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,5 +1,5 @@
import { WebsiteMetric } from 'interface/api/models'; import { WebsiteMetric } from 'lib/types';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,5 +1,5 @@
import { WebsiteMetric } from 'interface/api/models'; import { WebsiteMetric } from 'lib/types';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import moment from 'moment-timezone'; import moment from 'moment-timezone';

View File

@ -1,5 +1,5 @@
import { Website } from 'interface/api/models'; import { Website } from 'lib/types';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth'; import { canViewWebsite, canUpdateWebsite, canDeleteWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,5 +1,5 @@
import { WebsiteMetric } from 'interface/api/models'; import { WebsiteMetric } from 'lib/types';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { FILTER_IGNORED } from 'lib/constants'; import { FILTER_IGNORED } from 'lib/constants';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';

View File

@ -1,5 +1,4 @@
import { WebsitePageviews } from 'interface/api/models'; import { NextApiRequestQueryBody, WebsitePageviews } from 'lib/types';
import { NextApiRequestQueryBody } from 'interface/api/nextApi';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import moment from 'moment-timezone'; import moment from 'moment-timezone';

View File

@ -1,4 +1,4 @@
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,5 +1,5 @@
import { WebsiteStats } from 'interface/api/models'; import { WebsiteStats } from 'lib/types';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { canViewWebsite } from 'lib/auth'; import { canViewWebsite } from 'lib/auth';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';

View File

@ -1,15 +1,12 @@
import { Prisma } from '@prisma/client'; import { Prisma } from '@prisma/client';
import { NextApiRequestQueryBody } from 'interface/api/nextApi'; import { NextApiRequestQueryBody } from 'lib/types';
import { checkAdmin } from 'lib/auth';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { useAuth, useCors } from 'lib/middleware'; import { useAuth, useCors } from 'lib/middleware';
import { NextApiResponse } from 'next'; import { NextApiResponse } from 'next';
import { methodNotAllowed, ok } from 'next-basics'; import { methodNotAllowed, ok } from 'next-basics';
import { createWebsite, getAllWebsites, getWebsitesByUserId } from 'queries'; import { createWebsite, getUserWebsites } from 'queries';
export interface WebsitesRequestQuery { export interface WebsitesRequestQuery {}
include_all?: boolean;
}
export interface WebsitesRequestBody { export interface WebsitesRequestBody {
name: string; name: string;
@ -30,12 +27,7 @@ export default async (
} = req.auth; } = req.auth;
if (req.method === 'GET') { if (req.method === 'GET') {
const { include_all } = req.query; const websites = await getUserWebsites(userId);
const isAdmin = await checkAdmin(userId);
const websites =
isAdmin && include_all ? await getAllWebsites() : await getWebsitesByUserId(userId);
return ok(res, websites); return ok(res, websites);
} }

View File

@ -1,14 +1,8 @@
import { Prisma, Team } from '@prisma/client'; import { Prisma, Team } from '@prisma/client';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto';
export async function createTeam( import { ROLES } from 'lib/constants';
data: Prisma.TeamCreateInput, import { Website } from 'lib/types';
searchDeleted = false,
): Promise<Team> {
return prisma.client.team.create({
data: { ...data, isDeleted: searchDeleted ? null : false },
});
}
export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> { export async function getTeam(where: Prisma.TeamWhereInput): Promise<Team> {
return prisma.client.team.findFirst({ return prisma.client.team.findFirst({
@ -22,19 +16,35 @@ export async function getTeams(where: Prisma.TeamWhereInput): Promise<Team[]> {
}); });
} }
export async function getTeamsByUserId(userId: string): Promise<Team[]> { export async function getTeamWebsites(teamId: string): Promise<Website[]> {
return prisma.client.teamUser return prisma.client.website.findMany({
.findMany({ where: {
where: { teamId,
},
orderBy: [
{
name: 'asc',
},
],
});
}
export async function createTeam(data: Prisma.TeamCreateInput): Promise<Team> {
const { id, userId } = data;
return prisma.transaction([
prisma.client.team.create({
data,
}),
prisma.client.teamUser.create({
data: {
id: uuid(),
teamId: id,
userId, userId,
role: ROLES.teamOwner,
}, },
include: { }),
team: true, ]);
},
})
.then(data => {
return data.map(a => a.team);
});
} }
export async function updateTeam( export async function updateTeam(
@ -42,7 +52,10 @@ export async function updateTeam(
where: Prisma.TeamWhereUniqueInput, where: Prisma.TeamWhereUniqueInput,
): Promise<Team> { ): Promise<Team> {
return prisma.client.team.update({ return prisma.client.team.update({
data, data: {
...data,
updatedAt: new Date(),
},
where, where,
}); });
} }
@ -50,7 +63,7 @@ export async function updateTeam(
export async function deleteTeam(teamId: string): Promise<Team> { export async function deleteTeam(teamId: string): Promise<Team> {
return prisma.client.team.update({ return prisma.client.team.update({
data: { data: {
isDeleted: true, deletedAt: new Date(),
}, },
where: { where: {
id: teamId, id: teamId,

View File

@ -2,6 +2,23 @@ import { Prisma, TeamUser } from '@prisma/client';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
export async function getTeamUser(teamId: string, userId: string): Promise<TeamUser> {
return prisma.client.teamUser.findFirst({
where: {
teamId,
userId,
},
});
}
export async function getTeamUsers(teamId: string): Promise<TeamUser[]> {
return prisma.client.teamUser.findMany({
where: {
teamId,
},
});
}
export async function createTeamUser( export async function createTeamUser(
userId: string, userId: string,
teamId: string, teamId: string,
@ -17,18 +34,6 @@ export async function createTeamUser(
}); });
} }
export async function getTeamUser(where: Prisma.TeamUserWhereInput): Promise<TeamUser> {
return prisma.client.teamUser.findFirst({
where,
});
}
export async function getTeamUsers(where: Prisma.TeamUserWhereInput): Promise<TeamUser[]> {
return prisma.client.teamUser.findMany({
where,
});
}
export async function updateTeamUser( export async function updateTeamUser(
data: Prisma.TeamUserUpdateInput, data: Prisma.TeamUserUpdateInput,
where: Prisma.TeamUserWhereUniqueInput, where: Prisma.TeamUserWhereUniqueInput,
@ -42,7 +47,7 @@ export async function updateTeamUser(
export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> { export async function deleteTeamUser(teamUserId: string): Promise<TeamUser> {
return prisma.client.teamUser.update({ return prisma.client.teamUser.update({
data: { data: {
isDeleted: true, deletedAt: new Date(),
}, },
where: { where: {
id: teamUserId, id: teamUserId,

View File

@ -1,6 +1,7 @@
import { Prisma } from '@prisma/client'; import { Prisma, Team } from '@prisma/client';
import cache from 'lib/cache'; import cache from 'lib/cache';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { Website } from 'lib/types';
export interface User { export interface User {
id: string; id: string;
@ -9,36 +10,19 @@ export interface User {
createdAt?: Date; createdAt?: Date;
} }
export async function createUser(data: {
id: string;
username: string;
password: string;
}): Promise<{
id: string;
username: string;
}> {
return prisma.client.user.create({
data,
select: {
id: true,
username: true,
},
});
}
export async function getUser( export async function getUser(
where: Prisma.UserWhereUniqueInput, where: Prisma.UserWhereUniqueInput,
includePassword = false, options: { includePassword?: boolean } = {},
): Promise<User> { ): Promise<User> {
const { includePassword = false } = options;
return prisma.client.user.findUnique({ return prisma.client.user.findUnique({
where, where,
select: { select: {
id: true, id: true,
username: true, username: true,
userRole: {
select: { role: true },
},
password: includePassword, password: includePassword,
role: true,
}, },
}); });
} }
@ -53,24 +37,56 @@ export async function getUsers(): Promise<User[]> {
select: { select: {
id: true, id: true,
username: true, username: true,
role: true,
createdAt: true, createdAt: true,
}, },
}); });
} }
export async function getUsersByTeamId(teamId): Promise<User[]> { export async function getUserTeams(userId: string): Promise<Team[]> {
return prisma.client.user.findMany({ return prisma.client.teamUser
where: { .findMany({
teamUser: { where: {
every: { userId,
teamId,
},
}, },
include: {
team: true,
},
})
.then(data => {
return data.map(a => a.team);
});
}
export async function getUserWebsites(userId: string): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
userId,
}, },
orderBy: [
{
name: 'asc',
},
],
});
}
export async function createUser(data: {
id: string;
username: string;
password: string;
role: string;
}): Promise<{
id: string;
username: string;
role: string;
}> {
return prisma.client.user.create({
data,
select: { select: {
id: true, id: true,
username: true, username: true,
createdAt: true, role: true,
}, },
}); });
} }
@ -85,8 +101,8 @@ export async function updateUser(
select: { select: {
id: true, id: true,
username: true, username: true,
role: true,
createdAt: true, createdAt: true,
userRole: true,
}, },
}); });
} }
@ -106,8 +122,8 @@ export async function deleteUser(
websiteIds = websites.map(a => a.id); websiteIds = websites.map(a => a.id);
} }
return client return prisma
.$transaction([ .transaction([
client.websiteEvent.deleteMany({ client.websiteEvent.deleteMany({
where: { websiteId: { in: websiteIds } }, where: { websiteId: { in: websiteIds } },
}), }),
@ -116,13 +132,13 @@ export async function deleteUser(
}), }),
client.website.updateMany({ client.website.updateMany({
data: { data: {
isDeleted: true, deletedAt: new Date(),
}, },
where: { id: { in: websiteIds } }, where: { id: { in: websiteIds } },
}), }),
client.user.update({ client.user.update({
data: { data: {
isDeleted: true, deletedAt: new Date(),
}, },
where: { where: {
id: userId, id: userId,

View File

@ -1,51 +0,0 @@
import { Prisma, UserRole } from '@prisma/client';
import prisma from 'lib/prisma';
export async function createUserRole(
data: Prisma.UserRoleCreateInput | Prisma.UserRoleUncheckedCreateInput,
): Promise<UserRole> {
return prisma.client.userRole.create({
data,
});
}
export async function getUserRole(where: Prisma.UserRoleWhereInput): Promise<UserRole> {
return prisma.client.userRole.findFirst({
where,
});
}
export async function getUserRoles(where: Prisma.UserRoleWhereInput): Promise<UserRole[]> {
return prisma.client.userRole.findMany({
where,
});
}
export async function getUserRolesByUserId(userId: string): Promise<UserRole[]> {
return prisma.client.userRole.findMany({
where: {
userId,
},
});
}
export async function updateUserRole(
data: Prisma.UserRoleUpdateInput,
where: Prisma.UserRoleWhereUniqueInput,
): Promise<UserRole> {
return prisma.client.userRole.update({
data,
where,
});
}
export async function deleteUserRole(userRoleId: string): Promise<UserRole> {
return prisma.client.userRole.update({
data: {
isDeleted: true,
},
where: {
id: userRoleId,
},
});
}

View File

@ -3,6 +3,20 @@ import cache from 'lib/cache';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
return prisma.client.website.findUnique({
where,
});
}
export async function getWebsites(): Promise<Website[]> {
return prisma.client.website.findMany({
orderBy: {
name: 'asc',
},
});
}
export async function createWebsite( export async function createWebsite(
data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput, data: Prisma.WebsiteCreateInput | Prisma.WebsiteUncheckedCreateInput,
): Promise<Website> { ): Promise<Website> {
@ -55,44 +69,6 @@ export async function resetWebsite(
}); });
} }
export async function getWebsite(where: Prisma.WebsiteWhereUniqueInput): Promise<Website> {
return prisma.client.website.findUnique({
where,
});
}
export async function getWebsitesByUserId(userId): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
userId,
},
orderBy: {
name: 'asc',
},
});
}
export async function getWebsitesByTeamId(teamId): Promise<Website[]> {
return prisma.client.website.findMany({
where: {
teamId,
},
orderBy: {
name: 'asc',
},
});
}
export async function getAllWebsites(): Promise<Website[]> {
return await prisma.client.website.findMany({
orderBy: [
{
name: 'asc',
},
],
});
}
export async function deleteWebsite(websiteId: string) { export async function deleteWebsite(websiteId: string) {
return runQuery({ return runQuery({
[PRISMA]: () => deleteWebsiteRelationalQuery(websiteId), [PRISMA]: () => deleteWebsiteRelationalQuery(websiteId),
@ -127,7 +103,7 @@ async function deleteWebsiteRelationalQuery(
async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> { async function deleteWebsiteClickhouseQuery(websiteId): Promise<Website> {
return prisma.client.website.update({ return prisma.client.website.update({
data: { data: {
isDeleted: true, deletedAt: new Date(),
}, },
where: { id: websiteId }, where: { id: websiteId },
}); });

View File

@ -2,8 +2,8 @@ import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { WebsiteMetric } from 'interface/api/models'; import { WebsiteMetric } from 'lib/types';
import { EventType } from 'lib/types'; import { EVENT_TYPE } from 'lib/constants';
export async function getEventData( export async function getEventData(
...args: [ ...args: [
@ -47,7 +47,7 @@ async function relationalQuery(
from website_event from website_event
where website_id ='${websiteId}' where website_id ='${websiteId}'
and created_at between $1 and $2 and created_at between $1 and $2
and event_type = ${EventType.Event} and event_type = ${EVENT_TYPE.customEvent}
${eventName ? `and eventName = ${eventName}` : ''} ${eventName ? `and eventName = ${eventName}` : ''}
${ ${
Object.keys(filters).length > 0 Object.keys(filters).length > 0
@ -80,7 +80,7 @@ async function clickhouseQuery(
from event from event
where website_id = $1 where website_id = $1
and rev_id = $2 and rev_id = $2
and event_type = ${EventType.Event} and event_type = ${EVENT_TYPE.customEvent}
${eventName ? `and eventName = ${eventName}` : ''} ${eventName ? `and eventName = ${eventName}` : ''}
and ${getBetweenDates('created_at', startDate, endDate)} and ${getBetweenDates('created_at', startDate, endDate)}
${ ${

View File

@ -2,8 +2,8 @@ import prisma from 'lib/prisma';
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { WebsiteEventMetric } from 'interface/api/models'; import { WebsiteEventMetric } from 'lib/types';
import { EventType } from 'lib/types'; import { EVENT_TYPE } from 'lib/constants';
export async function getEventMetrics( export async function getEventMetrics(
...args: [ ...args: [
@ -56,7 +56,7 @@ async function relationalQuery(
from website_event from website_event
where website_id='${websiteId}' where website_id='${websiteId}'
and created_at between $1 and $2 and created_at between $1 and $2
and event_type = ${EventType.Event} and event_type = ${EVENT_TYPE.customEvent}
${getFilterQuery(filters, params)} ${getFilterQuery(filters, params)}
group by 1, 2 group by 1, 2
order by 2`, order by 2`,
@ -95,7 +95,7 @@ async function clickhouseQuery(
from event from event
where website_id = $1 where website_id = $1
and rev_id = $2 and rev_id = $2
and event_type = ${EventType.Event} and event_type = ${EVENT_TYPE.customEvent}
and ${getBetweenDates('created_at', startDate, endDate)} and ${getBetweenDates('created_at', startDate, endDate)}
${getFilterQuery(filters, params)} ${getFilterQuery(filters, params)}
group by x, t group by x, t

View File

@ -1,10 +1,9 @@
import { EVENT_NAME_LENGTH, URL_LENGTH } from 'lib/constants'; import { EVENT_NAME_LENGTH, URL_LENGTH, EVENT_TYPE } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import kafka from 'lib/kafka'; import kafka from 'lib/kafka';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { EventType } from 'lib/types';
export async function saveEvent(args: { export async function saveEvent(args: {
id: string; id: string;
@ -43,7 +42,7 @@ async function relationalQuery(data: {
sessionId, sessionId,
url: url?.substring(0, URL_LENGTH), url: url?.substring(0, URL_LENGTH),
referrer: referrer?.substring(0, URL_LENGTH), referrer: referrer?.substring(0, URL_LENGTH),
eventType: EventType.Event, eventType: EVENT_TYPE.customEvent,
eventName: eventName?.substring(0, EVENT_NAME_LENGTH), eventName: eventName?.substring(0, EVENT_NAME_LENGTH),
eventData, eventData,
}; };
@ -77,7 +76,7 @@ async function clickhouseQuery(data: {
session_id: sessionId, session_id: sessionId,
event_id: uuid(), event_id: uuid(),
url: url?.substring(0, URL_LENGTH), url: url?.substring(0, URL_LENGTH),
event_type: EventType.Event, event_type: EVENT_TYPE.customEvent,
event_name: eventName?.substring(0, EVENT_NAME_LENGTH), event_name: eventName?.substring(0, EVENT_NAME_LENGTH),
event_data: eventData ? JSON.stringify(eventData) : null, event_data: eventData ? JSON.stringify(eventData) : null,
rev_id: website?.revId || 0, rev_id: website?.revId || 0,

View File

@ -3,7 +3,7 @@ import clickhouse from 'lib/clickhouse';
import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db'; import { runQuery, CLICKHOUSE, PRISMA } from 'lib/db';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { Prisma } from '@prisma/client'; import { Prisma } from '@prisma/client';
import { EventType } from 'lib/types'; import { EVENT_TYPE } from 'lib/constants';
export async function getPageviewMetrics( export async function getPageviewMetrics(
...args: [ ...args: [
@ -43,7 +43,7 @@ async function relationalQuery(
${joinSession} ${joinSession}
where website_id='${websiteId}' where website_id='${websiteId}'
and website_event.created_at between $1 and $2 and website_event.created_at between $1 and $2
and event_type = ${EventType.Pageview} and event_type = ${EVENT_TYPE.pageView}
${filterQuery} ${filterQuery}
group by 1 group by 1
order by 2 desc`, order by 2 desc`,
@ -71,9 +71,9 @@ async function clickhouseQuery(
from event from event
where website_id = $1 where website_id = $1
and rev_id = $2 and rev_id = $2
and event_type = ${EventType.Pageview} and event_type = ${EVENT_TYPE.pageView}
${column !== 'event_name' ? `and event_name = ''` : `and event_name != ''`} ${column !== 'event_name' ? `and event_name = ''` : `and event_name != ''`}
and ${getBetweenDates('created_at', startDate, endDate)} and ${getBetweenDates('created_at', startDate, endDate)}n
${filterQuery} ${filterQuery}
group by x group by x
order by y desc`, order by y desc`,

View File

@ -2,7 +2,7 @@ import cache from 'lib/cache';
import clickhouse from 'lib/clickhouse'; import clickhouse from 'lib/clickhouse';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import { EventType } from 'lib/types'; import { EVENT_TYPE } from 'lib/constants';
export async function getPageviewStats( export async function getPageviewStats(
...args: [ ...args: [
@ -56,7 +56,7 @@ async function relationalQuery(
${joinSession} ${joinSession}
where website.website_id='${websiteId}' where website.website_id='${websiteId}'
and pageview.created_at between $1 and $2 and pageview.created_at between $1 and $2
and event_type = ${EventType.Pageview} and event_type = ${EVENT_TYPE.pageView}
${filterQuery} ${filterQuery}
group by 1`, group by 1`,
params, params,
@ -92,7 +92,7 @@ async function clickhouseQuery(
from event from event
where website_id = $1 where website_id = $1
and rev_id = $2 and rev_id = $2
and event_type = ${EventType.Pageview} and event_type = ${EVENT_TYPE.pageView}
and ${getBetweenDates('created_at', startDate, endDate)} and ${getBetweenDates('created_at', startDate, endDate)}
${filterQuery} ${filterQuery}
group by t) g group by t) g

View File

@ -1,10 +1,9 @@
import { URL_LENGTH } from 'lib/constants'; import { URL_LENGTH, EVENT_TYPE } from 'lib/constants';
import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db'; import { CLICKHOUSE, PRISMA, runQuery } from 'lib/db';
import kafka from 'lib/kafka'; import kafka from 'lib/kafka';
import prisma from 'lib/prisma'; import prisma from 'lib/prisma';
import cache from 'lib/cache'; import cache from 'lib/cache';
import { uuid } from 'lib/crypto'; import { uuid } from 'lib/crypto';
import { EventType } from 'lib/types';
export async function savePageView(args: { export async function savePageView(args: {
id: string; id: string;
@ -40,7 +39,7 @@ async function relationalQuery(data: {
sessionId, sessionId,
url: url?.substring(0, URL_LENGTH), url: url?.substring(0, URL_LENGTH),
referrer: referrer?.substring(0, URL_LENGTH), referrer: referrer?.substring(0, URL_LENGTH),
eventType: EventType.Pageview, eventType: EVENT_TYPE.pageView,
}, },
}); });
} }
@ -58,7 +57,7 @@ async function clickhouseQuery(data) {
rev_id: website?.revId || 0, rev_id: website?.revId || 0,
created_at: getDateFormat(new Date()), created_at: getDateFormat(new Date()),
country: country ? country : null, country: country ? country : null,
event_type: EventType.Pageview, event_type: EVENT_TYPE.pageView,
...args, ...args,
}; };

View File

@ -1,7 +1,6 @@
export * from './admin/team'; export * from './admin/team';
export * from './admin/teamUser'; export * from './admin/teamUser';
export * from './admin/user'; export * from './admin/user';
export * from './admin/userRole';
export * from './admin/website'; export * from './admin/website';
export * from './analytics/event/getEventMetrics'; export * from './analytics/event/getEventMetrics';
export * from './analytics/event/getEvents'; export * from './analytics/event/getEvents';