Show More
The requested changes are too big and content was truncated. Show full diff
@@ -0,0 +1,60 b'' | |||
|
1 | |RCE| 4.18.1 |RNS| | |
|
2 | ------------------ | |
|
3 | ||
|
4 | Release Date | |
|
5 | ^^^^^^^^^^^^ | |
|
6 | ||
|
7 | - 2020-01-20 | |
|
8 | ||
|
9 | ||
|
10 | New Features | |
|
11 | ^^^^^^^^^^^^ | |
|
12 | ||
|
13 | ||
|
14 | ||
|
15 | General | |
|
16 | ^^^^^^^ | |
|
17 | ||
|
18 | - API: invalidate license cache on set_license_key call. | |
|
19 | - API: add send_email flag for comments api to allow commenting without email notification. | |
|
20 | - API: added pull requests versions into returned API data. | |
|
21 | - Dashboard: fixed jumping of text in grid loading by new loading indicator. | |
|
22 | - Installation: add few extra defaults that makes RhodeCode nicer out of the box. | |
|
23 | - Pull Requests: small code cleanup to define other type of merge username. | |
|
24 | RC_MERGE_USER_NAME_ATTR env variable defines what should be used from user as merge username. | |
|
25 | - Gists: cleanup UI and make the gist access id use monospace according to the new UI. | |
|
26 | ||
|
27 | ||
|
28 | Security | |
|
29 | ^^^^^^^^ | |
|
30 | ||
|
31 | - Repository permission: properly flush permission caches on set private mode of repository. | |
|
32 | Otherwise we get cached values still in place until it expires. | |
|
33 | - Repository permission: add set/un-set of private repository from permissions page. | |
|
34 | - Permissions: flush all user permissions in case of default user permission changes. | |
|
35 | ||
|
36 | ||
|
37 | Performance | |
|
38 | ^^^^^^^^^^^ | |
|
39 | ||
|
40 | - Caches: used more efficient way of fetching all users for permissions invalidation. | |
|
41 | - Issue trackers: optimized performance of fetching issue tracker patterns. | |
|
42 | ||
|
43 | ||
|
44 | Fixes | |
|
45 | ^^^^^ | |
|
46 | ||
|
47 | - SSH: fixed SSH problems with EE edition. | |
|
48 | - Branch permissions: remove emtpy tooltips on branch permission entries. | |
|
49 | - Core: fixed cython compat inspect that caused some API calls to not work correctly in EE release. | |
|
50 | - Audit logger: use copy of params we later modify to prevent from modification by the store | |
|
51 | function of parameters that we only use for reading. | |
|
52 | - Users: fixed wrong mention of readme in user description help block. | |
|
53 | - Issue trackers: fixed wrong examples in patterns. | |
|
54 | - Issue trackers: fixed missing option to get back to inherited settings. | |
|
55 | ||
|
56 | ||
|
57 | Upgrade notes | |
|
58 | ^^^^^^^^^^^^^ | |
|
59 | ||
|
60 | - Scheduled release addressing problems in 4.18.X releases. |
@@ -0,0 +1,49 b'' | |||
|
1 | |RCE| 4.18.2 |RNS| | |
|
2 | ------------------ | |
|
3 | ||
|
4 | Release Date | |
|
5 | ^^^^^^^^^^^^ | |
|
6 | ||
|
7 | - 2020-01-28 | |
|
8 | ||
|
9 | ||
|
10 | New Features | |
|
11 | ^^^^^^^^^^^^ | |
|
12 | ||
|
13 | ||
|
14 | ||
|
15 | General | |
|
16 | ^^^^^^^ | |
|
17 | ||
|
18 | - Permissions: add better help text about default permissions, and correlation with anonymous access enabled. | |
|
19 | - Mentions: markdown renderer now wraps username in hovercard logic allowing checking the mentioned user. | |
|
20 | - Documentation: added note about hard restart due to celery update. | |
|
21 | - Maintenance: run rebuildfncache for Mercurial in maintenance command. | |
|
22 | ||
|
23 | ||
|
24 | Security | |
|
25 | ^^^^^^^^ | |
|
26 | ||
|
27 | ||
|
28 | ||
|
29 | Performance | |
|
30 | ^^^^^^^^^^^ | |
|
31 | ||
|
32 | - Authentication: cache plugins for auth and their settings in the auth_registry for single request. | |
|
33 | This heavily influences SVN performance on multiple-file commits. | |
|
34 | ||
|
35 | ||
|
36 | Fixes | |
|
37 | ^^^^^ | |
|
38 | ||
|
39 | - Descriptions: fixed rendering problem with certain meta-tags in repo description. | |
|
40 | - Emails: fixed fonts rendering problems in Outlook. | |
|
41 | - Emails: fixed bug in test email sending. | |
|
42 | - Summary: fixed styling of readme indicator. | |
|
43 | - Flash: fixed display problem with flash messages on error pages. | |
|
44 | ||
|
45 | ||
|
46 | Upgrade notes | |
|
47 | ^^^^^^^^^^^^^ | |
|
48 | ||
|
49 | - Scheduled release addressing problems in 4.18.X releases. |
@@ -0,0 +1,64 b'' | |||
|
1 | |RCE| 4.18.3 |RNS| | |
|
2 | ------------------ | |
|
3 | ||
|
4 | Release Date | |
|
5 | ^^^^^^^^^^^^ | |
|
6 | ||
|
7 | - 2020-03-24 | |
|
8 | ||
|
9 | ||
|
10 | New Features | |
|
11 | ^^^^^^^^^^^^ | |
|
12 | ||
|
13 | - LDAP: added nested user groups sync which was planned in 4.18.X but didn't | |
|
14 | make it to the release. New option for sync is available in the LDAP configuration. | |
|
15 | ||
|
16 | ||
|
17 | General | |
|
18 | ^^^^^^^ | |
|
19 | ||
|
20 | - API: added branch permissions functions. | |
|
21 | - Pull requests: added creating indicator to let users know they should wait until PR is creating. | |
|
22 | - Pull requests: allow super-admins to force change state of locked PRs. | |
|
23 | - Users/User groups: in edit mode we now show the actual name of what we're editing. | |
|
24 | - SSH: allow generation of legacy SSH keys for older systems and Windows users. | |
|
25 | - File store: don't response with cookie data on file-store download response. | |
|
26 | - File store: use our own logic for setting content-type. This solves a problem | |
|
27 | when previously used resolver set different content-type+content-encoding which | |
|
28 | is an incorrect behaviour. | |
|
29 | - My Account: show info about password usage for external accounts e.g github/google etc | |
|
30 | We now recommend using auth-tokens instead of actual passwords. | |
|
31 | - Repositories: in description field we now show mention of metatags only if they | |
|
32 | are enabled. | |
|
33 | ||
|
34 | ||
|
35 | Security | |
|
36 | ^^^^^^^^ | |
|
37 | ||
|
38 | - Remote sync: don't expose credentials in displayed URLs. | |
|
39 | Remote links url had visible credentials displayed in the link. | |
|
40 | This was used for web-view and not needed anymore. | |
|
41 | ||
|
42 | ||
|
43 | Performance | |
|
44 | ^^^^^^^^^^^ | |
|
45 | ||
|
46 | - Full text search: significantly improved GIT commit indexing performance by reducing | |
|
47 | number of calls to the vcsserver. | |
|
48 | ||
|
49 | ||
|
50 | Fixes | |
|
51 | ^^^^^ | |
|
52 | ||
|
53 | - Mercurial: fixed cases of lookup of branches that are exactly 20 character long. | |
|
54 | - SVN: allow legacy (pre SVN 1.7) extraction of post commit data. | |
|
55 | - GIT: use non-unicode author extraction as it's returned as bytes from backend, and | |
|
56 | we can get an unicode errors while there's some non-ascii characters. | |
|
57 | - GIT: use safe configparser for git submodules to prevent from errors on submodules with % sign. | |
|
58 | - System info: fixed UI problem with new version update info screen. | |
|
59 | ||
|
60 | ||
|
61 | Upgrade notes | |
|
62 | ^^^^^^^^^^^^^ | |
|
63 | ||
|
64 | - Scheduled release addressing problems in 4.18.X releases. |
@@ -1,60 +1,64 b'' | |||
|
1 | 1 | 1bd3e92b7e2e2d2024152b34bb88dff1db544a71 v4.0.0 |
|
2 | 2 | 170c5398320ea6cddd50955e88d408794c21d43a v4.0.1 |
|
3 | 3 | c3fe200198f5aa34cf2e4066df2881a9cefe3704 v4.1.0 |
|
4 | 4 | 7fd5c850745e2ea821fb4406af5f4bff9b0a7526 v4.1.1 |
|
5 | 5 | 41c87da28a179953df86061d817bc35533c66dd2 v4.1.2 |
|
6 | 6 | baaf9f5bcea3bae0ef12ae20c8b270482e62abb6 v4.2.0 |
|
7 | 7 | 32a70c7e56844a825f61df496ee5eaf8c3c4e189 v4.2.1 |
|
8 | 8 | fa695cdb411d294679ac081d595ac654e5613b03 v4.3.0 |
|
9 | 9 | 0e4dc11b58cad833c513fe17bac39e6850edf959 v4.3.1 |
|
10 | 10 | 8a876f48f5cb1d018b837db28ff928500cb32cfb v4.4.0 |
|
11 | 11 | 8dd86b410b1aac086ffdfc524ef300f896af5047 v4.4.1 |
|
12 | 12 | d2514226abc8d3b4f6fb57765f47d1b6fb360a05 v4.4.2 |
|
13 | 13 | 27d783325930af6dad2741476c0d0b1b7c8415c2 v4.5.0 |
|
14 | 14 | 7f2016f352abcbdba4a19d4039c386e9629449da v4.5.1 |
|
15 | 15 | 416fec799314c70a5c780fb28b3357b08869333a v4.5.2 |
|
16 | 16 | 27c3b85fafc83143e6678fbc3da69e1615bcac55 v4.6.0 |
|
17 | 17 | 5ad13deb9118c2a5243d4032d4d9cc174e5872db v4.6.1 |
|
18 | 18 | 2be921e01fa24bb102696ada596f87464c3666f6 v4.7.0 |
|
19 | 19 | 7198bdec29c2872c974431d55200d0398354cdb1 v4.7.1 |
|
20 | 20 | bd1c8d230fe741c2dfd7100a0ef39fd0774fd581 v4.7.2 |
|
21 | 21 | 9731914f89765d9628dc4dddc84bc9402aa124c8 v4.8.0 |
|
22 | 22 | c5a2b7d0e4bbdebc4a62d7b624befe375207b659 v4.9.0 |
|
23 | 23 | d9aa3b27ac9f7e78359775c75fedf7bfece232f1 v4.9.1 |
|
24 | 24 | 4ba4d74981cec5d6b28b158f875a2540952c2f74 v4.10.0 |
|
25 | 25 | 0a6821cbd6b0b3c21503002f88800679fa35ab63 v4.10.1 |
|
26 | 26 | 434ad90ec8d621f4416074b84f6e9ce03964defb v4.10.2 |
|
27 | 27 | 68baee10e698da2724c6e0f698c03a6abb993bf2 v4.10.3 |
|
28 | 28 | 00821d3afd1dce3f4767cc353f84a17f7d5218a1 v4.10.4 |
|
29 | 29 | 22f6744ad8cc274311825f63f953e4dee2ea5cb9 v4.10.5 |
|
30 | 30 | 96eb24bea2f5f9258775245e3f09f6fa0a4dda01 v4.10.6 |
|
31 | 31 | 3121217a812c956d7dd5a5875821bd73e8002a32 v4.11.0 |
|
32 | 32 | fa98b454715ac5b912f39e84af54345909a2a805 v4.11.1 |
|
33 | 33 | 3982abcfdcc229a723cebe52d3a9bcff10bba08e v4.11.2 |
|
34 | 34 | 33195f145db9172f0a8f1487e09207178a6ab065 v4.11.3 |
|
35 | 35 | 194c74f33e32bbae6fc4d71ec5a999cff3c13605 v4.11.4 |
|
36 | 36 | 8fbd8b0c3ddc2fa4ac9e4ca16942a03eb593df2d v4.11.5 |
|
37 | 37 | f0609aa5d5d05a1ca2f97c3995542236131c9d8a v4.11.6 |
|
38 | 38 | b5b30547d90d2e088472a70c84878f429ffbf40d v4.12.0 |
|
39 | 39 | 9072253aa8894d20c00b4a43dc61c2168c1eff94 v4.12.1 |
|
40 | 40 | 6a517543ea9ef9987d74371bd2a315eb0b232dc9 v4.12.2 |
|
41 | 41 | 7fc0731b024c3114be87865eda7ab621cc957e32 v4.12.3 |
|
42 | 42 | 6d531c0b068c6eda62dddceedc9f845ecb6feb6f v4.12.4 |
|
43 | 43 | 3d6bf2d81b1564830eb5e83396110d2a9a93eb1e v4.13.0 |
|
44 | 44 | 5468fc89e708bd90e413cd0d54350017abbdbc0e v4.13.1 |
|
45 | 45 | 610d621550521c314ee97b3d43473ac0bcf06fb8 v4.13.2 |
|
46 | 46 | 7dc62c090881fb5d03268141e71e0940d7c3295d v4.13.3 |
|
47 | 47 | 9151328c1c46b72ba6f00d7640d9141e75aa1ca2 v4.14.0 |
|
48 | 48 | a47eeac5dfa41fa6779d90452affba4091c3ade8 v4.14.1 |
|
49 | 49 | 4b34ce0d2c3c10510626b3b65044939bb7a2cddf v4.15.0 |
|
50 | 50 | 14502561d22e6b70613674cd675ae9a604b7989f v4.15.1 |
|
51 | 51 | 4aaa40b605b01af78a9f6882eca561c54b525ef0 v4.15.2 |
|
52 | 52 | 797744642eca86640ed20bef2cd77445780abaec v4.16.0 |
|
53 | 53 | 6c3452c7c25ed35ff269690929e11960ed6ad7d3 v4.16.1 |
|
54 | 54 | 5d8057df561c4b6b81b6401aed7d2f911e6e77f7 v4.16.2 |
|
55 | 55 | 13acfc008896ef4c62546bab5074e8f6f89b4fa7 v4.17.0 |
|
56 | 56 | 45b9b610976f483877142fe75321808ce9ebac59 v4.17.1 |
|
57 | 57 | ad5bd0c4bd322fdbd04bb825a3d027e08f7a3901 v4.17.2 |
|
58 | 58 | 037f5794b55a6236d68f6485a485372dde6566e0 v4.17.3 |
|
59 | 59 | 83bc3100cfd6094c1d04f475ddb299b7dc3d0b33 v4.17.4 |
|
60 | 60 | e3de8c95baf8cc9109ca56aee8193a2cb6a54c8a v4.17.4 |
|
61 | f37a3126570477543507f0bc9d245ce75546181a v4.18.0 | |
|
62 | 71d8791463e87b64c1a18475de330ee600d37561 v4.18.1 | |
|
63 | 4bd6b75dac1d25c64885d4d49385e5533f21c525 v4.18.2 | |
|
64 | 12ed92fe57f2e9fc7b71dc0b65e26c2da5c7085f v4.18.3 |
@@ -1,61 +1,61 b'' | |||
|
1 | 1 | .. _repo-admin-tasks: |
|
2 | 2 | |
|
3 | 3 | Common Admin Tasks for Repositories |
|
4 | 4 | ----------------------------------- |
|
5 | 5 | |
|
6 | 6 | |
|
7 | 7 | Manually Force Delete Repository |
|
8 | 8 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
9 | 9 | |
|
10 | 10 | In case of attached forks or pull-requests repositories should be archived. |
|
11 | 11 | Here is how to force delete a repository and remove all dependent objects |
|
12 | 12 | |
|
13 | 13 | |
|
14 | 14 | .. code-block:: bash |
|
15 | 15 | :dedent: 1 |
|
16 | 16 | |
|
17 | 17 | # starts the ishell interactive prompt |
|
18 | 18 | $ rccontrol ishell enterprise-1 |
|
19 | 19 | |
|
20 | 20 | .. code-block:: python |
|
21 | 21 | :dedent: 1 |
|
22 | 22 | |
|
23 | 23 | In [4]: from rhodecode.model.repo import RepoModel |
|
24 | 24 | In [3]: repo = Repository.get_by_repo_name('test_repos/repo_with_prs') |
|
25 | 25 | In [5]: RepoModel().delete(repo, forks='detach', pull_requests='delete') |
|
26 | 26 | In [6]: Session().commit() |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | Below is a fully automated example to force delete repositories reading from a |
|
30 | 30 | file where each line is a repository name. This can be executed via simple CLI command |
|
31 | 31 | without entering the interactive shell. |
|
32 | 32 | |
|
33 | 33 | Save the below content as a file named `repo_delete_task.py` |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | .. code-block:: python |
|
37 | 37 | :dedent: 1 |
|
38 | 38 | |
|
39 | 39 | from rhodecode.model.db import * |
|
40 | 40 | from rhodecode.model.repo import RepoModel |
|
41 | 41 | with open('delete_repos.txt', 'rb') as f: |
|
42 | 42 | # read all lines from file |
|
43 | 43 | repos = f.readlines() |
|
44 | 44 | for repo_name in repos: |
|
45 | 45 | repo_name = repo_name.strip() # cleanup the name just in case |
|
46 | 46 | repo = Repository.get_by_repo_name(repo_name) |
|
47 | 47 | if not repo: |
|
48 | 48 | raise Exception('Repo with name {} not found'.format(repo_name)) |
|
49 | 49 | RepoModel().delete(repo, forks='detach', pull_requests='delete') |
|
50 | 50 | Session().commit() |
|
51 | 51 | print('Removed repository {}'.format(repo_name)) |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | The code above will read the names of repositories from a file called `delete_repos.txt` |
|
55 | 55 | Each lines should represent a single name e.g `repo_name_1` or `repo_group/repo_name_2` |
|
56 | 56 | |
|
57 | 57 | Run this line from CLI to execute the code from the `repo_delete_task.py` file and |
|
58 | 58 | exit the ishell after the execution:: |
|
59 | 59 | |
|
60 |
echo "%run repo_delete_task.py" | rccontrol ishell |
|
|
60 | echo "%run repo_delete_task.py" | rccontrol ishell enterprise-1 | |
|
61 | 61 |
@@ -1,207 +1,208 b'' | |||
|
1 | 1 | .. _svn-http: |
|
2 | 2 | |
|
3 | 3 | |svn| With Write Over HTTP |
|
4 | 4 | ^^^^^^^^^^^^^^^^^^^^^^^^^^ |
|
5 | 5 | |
|
6 | 6 | To use |svn| with read/write support over the |svn| HTTP protocol, you have to |
|
7 | 7 | configure the HTTP |svn| backend. |
|
8 | 8 | |
|
9 | 9 | Prerequisites |
|
10 | 10 | ============= |
|
11 | 11 | |
|
12 | 12 | - Enable HTTP support inside the admin VCS settings on your |RCE| instance |
|
13 | 13 | - You need to install the following tools on the machine that is running an |
|
14 | 14 | instance of |RCE|: |
|
15 | 15 | ``Apache HTTP Server`` and ``mod_dav_svn``. |
|
16 | 16 | |
|
17 | 17 | |
|
18 | 18 | .. tip:: |
|
19 | 19 | |
|
20 | 20 | We recommend using Wandisco repositories which provide latest SVN versions |
|
21 | 21 | for most platforms. If you skip this version you'll have to ensure the Client version |
|
22 | 22 | is compatible with installed SVN version which might differ depending on the operating system. |
|
23 | 23 | Here is an example how to add the Wandisco repositories for Ubuntu. |
|
24 | 24 | |
|
25 | 25 | .. code-block:: bash |
|
26 | 26 | |
|
27 | 27 | $ sudo sh -c 'echo "deb http://opensource.wandisco.com/ubuntu `lsb_release -cs` svn110" >> /etc/apt/sources.list.d/subversion110.list' |
|
28 | 28 | $ sudo wget -q http://opensource.wandisco.com/wandisco-debian-new.gpg -O- | sudo apt-key add - |
|
29 | 29 | $ sudo apt-get update |
|
30 | 30 | |
|
31 | 31 | Here is an example how to add the Wandisco repositories for Centos/Redhat. Using |
|
32 | 32 | a yum config |
|
33 | 33 | |
|
34 | 34 | .. code-block:: bash |
|
35 | 35 | |
|
36 | 36 | [wandisco-Git] |
|
37 | 37 | name=CentOS-6 - Wandisco Git |
|
38 | 38 | baseurl=http://opensource.wandisco.com/centos/6/git/$basearch/ |
|
39 | 39 | enabled=1 |
|
40 | 40 | gpgcheck=1 |
|
41 | 41 | gpgkey=http://opensource.wandisco.com/RPM-GPG-KEY-WANdisco |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | Example installation of required components for Ubuntu platform: |
|
46 | 46 | |
|
47 | 47 | .. code-block:: bash |
|
48 | 48 | |
|
49 | 49 | $ sudo apt-get install apache2 |
|
50 | 50 | $ sudo apt-get install libapache2-svn |
|
51 | 51 | |
|
52 | 52 | Once installed you need to enable ``dav_svn`` on Ubuntu: |
|
53 | 53 | |
|
54 | 54 | .. code-block:: bash |
|
55 | 55 | |
|
56 | 56 | $ sudo a2enmod dav_svn |
|
57 | 57 | $ sudo a2enmod headers |
|
58 | 58 | $ sudo a2enmod authn_anon |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | Example installation of required components for RedHat/CentOS platform: |
|
62 | 62 | |
|
63 | 63 | .. code-block:: bash |
|
64 | 64 | |
|
65 | 65 | $ sudo yum install httpd |
|
66 | 66 | $ sudo yum install subversion mod_dav_svn |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | Once installed you need to enable ``dav_svn`` on RedHat/CentOS: |
|
70 | 70 | |
|
71 | 71 | .. code-block:: bash |
|
72 | 72 | |
|
73 | 73 | sudo vi /etc/httpd/conf.modules.d/10-subversion.conf |
|
74 | 74 | ## The file should read: |
|
75 | 75 | |
|
76 | 76 | LoadModule dav_svn_module modules/mod_dav_svn.so |
|
77 | 77 | LoadModule headers_module modules/mod_headers.so |
|
78 | 78 | LoadModule authn_anon_module modules/mod_authn_anon.so |
|
79 | 79 | |
|
80 | 80 | .. tip:: |
|
81 | 81 | |
|
82 | 82 | To check the installed mod_dav_svn module version, you can use such command. |
|
83 | 83 | |
|
84 | 84 | `strings /usr/lib/apache2/modules/mod_dav_svn.so | grep 'Powered by'` |
|
85 | 85 | |
|
86 | 86 | |
|
87 | 87 | Configuring Apache Setup |
|
88 | 88 | ======================== |
|
89 | 89 | |
|
90 | 90 | .. tip:: |
|
91 | 91 | |
|
92 | 92 | It is recommended to run Apache on a port other than 80, due to possible |
|
93 | 93 | conflicts with other HTTP servers like nginx. To do this, set the |
|
94 | 94 | ``Listen`` parameter in the ``/etc/apache2/ports.conf`` file, for example |
|
95 | 95 | ``Listen 8090``. |
|
96 | 96 | |
|
97 | 97 | |
|
98 | 98 | .. warning:: |
|
99 | 99 | |
|
100 | 100 | Make sure your Apache instance which runs the mod_dav_svn module is |
|
101 | 101 | only accessible by |RCE|. Otherwise everyone is able to browse |
|
102 | 102 | the repositories or run subversion operations (checkout/commit/etc.). |
|
103 | 103 | |
|
104 | 104 | It is also recommended to run apache as the same user as |RCE|, otherwise |
|
105 | 105 | permission issues could occur. To do this edit the ``/etc/apache2/envvars`` |
|
106 | 106 | |
|
107 | 107 | .. code-block:: apache |
|
108 | 108 | |
|
109 | 109 | export APACHE_RUN_USER=rhodecode |
|
110 | 110 | export APACHE_RUN_GROUP=rhodecode |
|
111 | 111 | |
|
112 | 112 | 1. To configure Apache, create and edit a virtual hosts file, for example |
|
113 | 113 | :file:`/etc/apache2/sites-enabled/default.conf`. Below is an example |
|
114 | 114 | how to use one with auto-generated config ```mod_dav_svn.conf``` |
|
115 | 115 | from configured |RCE| instance. |
|
116 | 116 | |
|
117 | 117 | .. code-block:: apache |
|
118 | 118 | |
|
119 | 119 | <VirtualHost *:8090> |
|
120 | 120 | ServerAdmin rhodecode-admin@localhost |
|
121 | 121 | DocumentRoot /var/www/html |
|
122 | 122 | ErrorLog ${'${APACHE_LOG_DIR}'}/error.log |
|
123 | 123 | CustomLog ${'${APACHE_LOG_DIR}'}/access.log combined |
|
124 | 124 | LogLevel info |
|
125 | 125 | # allows custom host names, prevents 400 errors on checkout |
|
126 | 126 | HttpProtocolOptions Unsafe |
|
127 | # Most likely this will be: /home/user/.rccontrol/enterprise-1/mod_dav_svn.conf | |
|
127 | 128 | Include /home/user/.rccontrol/enterprise-1/mod_dav_svn.conf |
|
128 | 129 | </VirtualHost> |
|
129 | 130 | |
|
130 | 131 | |
|
131 | 132 | 2. Go to the :menuselection:`Admin --> Settings --> VCS` page, and |
|
132 | 133 | enable :guilabel:`Proxy Subversion HTTP requests`, and specify the |
|
133 | 134 | :guilabel:`Subversion HTTP Server URL`. |
|
134 | 135 | |
|
135 | 136 | 3. Open the |RCE| configuration file, |
|
136 | 137 | :file:`/home/{user}/.rccontrol/{instance-id}/rhodecode.ini` |
|
137 | 138 | |
|
138 | 139 | 4. Add the following configuration option in the ``[app:main]`` |
|
139 | 140 | section if you don't have it yet. |
|
140 | 141 | |
|
141 | 142 | This enables mapping of the created |RCE| repo groups into special |
|
142 | 143 | |svn| paths. Each time a new repository group is created, the system will |
|
143 | 144 | update the template file and create new mapping. Apache web server needs to |
|
144 | 145 | be reloaded to pick up the changes on this file. |
|
145 | 146 | To do this, simply configure `svn.proxy.reload_cmd` inside the .ini file. |
|
146 | 147 | Example configuration: |
|
147 | 148 | |
|
148 | 149 | |
|
149 | 150 | .. code-block:: ini |
|
150 | 151 | |
|
151 | 152 | ############################################################ |
|
152 | 153 | ### Subversion proxy support (mod_dav_svn) ### |
|
153 | 154 | ### Maps RhodeCode repo groups into SVN paths for Apache ### |
|
154 | 155 | ############################################################ |
|
155 | 156 | ## Enable or disable the config file generation. |
|
156 | 157 | svn.proxy.generate_config = true |
|
157 | 158 | ## Generate config file with `SVNListParentPath` set to `On`. |
|
158 | 159 | svn.proxy.list_parent_path = true |
|
159 | 160 | ## Set location and file name of generated config file. |
|
160 | 161 | svn.proxy.config_file_path = %(here)s/mod_dav_svn.conf |
|
161 | 162 | ## Used as a prefix to the <Location> block in the generated config file. |
|
162 | 163 | ## In most cases it should be set to `/`. |
|
163 | 164 | svn.proxy.location_root = / |
|
164 | 165 | ## Command to reload the mod dav svn configuration on change. |
|
165 | 166 | ## Example: `/etc/init.d/apache2 reload` |
|
166 | 167 | svn.proxy.reload_cmd = /etc/init.d/apache2 reload |
|
167 | 168 | ## If the timeout expires before the reload command finishes, the command will |
|
168 | 169 | ## be killed. Setting it to zero means no timeout. Defaults to 10 seconds. |
|
169 | 170 | #svn.proxy.reload_timeout = 10 |
|
170 | 171 | |
|
171 | 172 | |
|
172 | 173 | This would create a special template file called ```mod_dav_svn.conf```. We |
|
173 | 174 | used that file path in the apache config above inside the Include statement. |
|
174 | 175 | It's also possible to manually generate the config from the |
|
175 | 176 | :menuselection:`Admin --> Settings --> VCS` page by clicking a |
|
176 | 177 | `Generate Apache Config` button. |
|
177 | 178 | |
|
178 | 179 | 5. Now only things left is to enable svn support, and generate the initial |
|
179 | 180 | configuration. |
|
180 | 181 | |
|
181 | 182 | - Select `Proxy subversion HTTP requests` checkbox |
|
182 | 183 | - Enter http://localhost:8090 into `Subversion HTTP Server URL` |
|
183 | 184 | - Click the `Generate Apache Config` button. |
|
184 | 185 | |
|
185 | 186 | This config will be automatically re-generated once an user-groups is added |
|
186 | 187 | to properly map the additional paths generated. |
|
187 | 188 | |
|
188 | 189 | |
|
189 | 190 | |
|
190 | 191 | Using |svn| |
|
191 | 192 | =========== |
|
192 | 193 | |
|
193 | 194 | Once |svn| has been enabled on your instance, you can use it with the |
|
194 | 195 | following examples. For more |svn| information, see the `Subversion Red Book`_ |
|
195 | 196 | |
|
196 | 197 | .. code-block:: bash |
|
197 | 198 | |
|
198 | 199 | # To clone a repository |
|
199 | 200 | svn checkout http://my-svn-server.example.com/my-svn-repo |
|
200 | 201 | |
|
201 | 202 | # svn commit |
|
202 | 203 | svn commit |
|
203 | 204 | |
|
204 | 205 | |
|
205 | 206 | .. _Subversion Red Book: http://svnbook.red-bean.com/en/1.7/svn-book.html#svn.ref.svn |
|
206 | 207 | |
|
207 | 208 | .. _Ask Ubuntu: http://askubuntu.com/questions/162391/how-do-i-fix-my-locale-issue No newline at end of file |
@@ -1,67 +1,67 b'' | |||
|
1 | 1 | .. _user-session-ref: |
|
2 | 2 | |
|
3 | 3 | User Session Performance |
|
4 | 4 | ------------------------ |
|
5 | 5 | |
|
6 | 6 | The default file-based sessions are only suitable for smaller setups, or |
|
7 | 7 | instances that doesn't have a lot of users or traffic. |
|
8 | 8 | They are set as default option because it's setup-free solution. |
|
9 | 9 | |
|
10 | 10 | The most common issue of file based sessions are file limit errors which occur |
|
11 | 11 | if there are lots of session files. |
|
12 | 12 | |
|
13 | 13 | Therefore, in a large scale deployment, to give better performance, |
|
14 | 14 | scalability, and maintainability we recommend switching from file-based |
|
15 | 15 | sessions to database-based user sessions or Redis based sessions. |
|
16 | 16 | |
|
17 | 17 | To switch to database-based user sessions uncomment the following section in |
|
18 | 18 | your :file:`/home/{user}/.rccontrol/{instance-id}/rhodecode.ini` file. |
|
19 | 19 | |
|
20 | 20 | |
|
21 | 21 | .. code-block:: ini |
|
22 | 22 | |
|
23 | 23 | ## db based session, fast, and allows easy management over logged in users |
|
24 | 24 | beaker.session.type = ext:database |
|
25 | 25 | beaker.session.table_name = db_session |
|
26 | 26 | |
|
27 | 27 | # use just one of the following according to the type of database |
|
28 | 28 | beaker.session.sa.url = postgresql://postgres:secret@localhost/rhodecode |
|
29 | 29 | # or |
|
30 | 30 | beaker.session.sa.url = mysql://root:secret@127.0.0.1/rhodecode |
|
31 | 31 | |
|
32 | 32 | beaker.session.sa.pool_recycle = 3600 |
|
33 | 33 | beaker.session.sa.echo = false |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | and make sure you comment out the file based sessions. |
|
37 | 37 | |
|
38 | 38 | .. code-block:: ini |
|
39 | 39 | |
|
40 | 40 | ## types are file, ext:memcached, ext:database, and memory (default). |
|
41 | 41 | #beaker.session.type = file |
|
42 | 42 | #beaker.session.data_dir = %(here)s/data/sessions/data |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | The `table_name` will be automatically created on specified database if it isn't yet existing. |
|
46 | 46 | Database specified in the `beaker.session.sa.url` can be the same that RhodeCode |
|
47 | 47 | uses, or if required it can be a different one. We recommend to use the same database. |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | |
|
51 | To switch to reds-based user sessions uncomment the following section in | |
|
51 | To switch to redis-based user sessions uncomment the following section in | |
|
52 | 52 | your :file:`/home/{user}/.rccontrol/{instance-id}/rhodecode.ini` file. |
|
53 | 53 | |
|
54 | 54 | .. code-block:: ini |
|
55 | 55 | |
|
56 | 56 | ## redis sessions |
|
57 | 57 | beaker.session.type = ext:redis |
|
58 | 58 | beaker.session.url = localhost:6379 |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | and make sure you comment out the file based sessions. |
|
62 | 62 | |
|
63 | 63 | .. code-block:: ini |
|
64 | 64 | |
|
65 | 65 | ## types are file, ext:memcached, ext:database, and memory (default). |
|
66 | 66 | #beaker.session.type = file |
|
67 | 67 | #beaker.session.data_dir = %(here)s/data/sessions/data No newline at end of file |
@@ -1,113 +1,122 b'' | |||
|
1 | 1 | .. _quick-start: |
|
2 | 2 | |
|
3 | 3 | Quick Start Installation Guide |
|
4 | 4 | ============================== |
|
5 | 5 | |
|
6 | 6 | .. important:: |
|
7 | 7 | |
|
8 | 8 | These are quick start instructions. To optimize your |RCE|, |
|
9 | 9 | |RCC|, and |RCT| usage, read the more detailed instructions in our guides. |
|
10 | 10 | For detailed installation instructions, see |
|
11 | 11 | :ref:`RhodeCode Control Documentation <control:rcc>` |
|
12 | 12 | |
|
13 | 13 | .. tip:: |
|
14 | 14 | |
|
15 | 15 | If using a non-SQLite database, install and configure the database, create |
|
16 | 16 | a new user, and grant permissions. You will be prompted for this user's |
|
17 | 17 | credentials during |RCE| installation. See the relevant database |
|
18 | 18 | documentation for more details. |
|
19 | 19 | |
|
20 | 20 | To get |RCE| up and running, run through the below steps: |
|
21 | 21 | |
|
22 | 22 | 1. Download the latest |RCC| installer from `rhodecode.com/download`_. |
|
23 | 23 | If you don't have an account, sign up at `rhodecode.com/register`_. |
|
24 | 24 | |
|
25 | 25 | 2. Run the |RCC| installer and accept the End User Licence using the |
|
26 | 26 | following example: |
|
27 | 27 | |
|
28 | 28 | .. code-block:: bash |
|
29 | 29 | |
|
30 | 30 | $ chmod +x RhodeCode-installer-linux-* |
|
31 | 31 | $ ./RhodeCode-installer-linux-* |
|
32 | 32 | |
|
33 | 33 | Do you accept the RhodeCode Control license? |
|
34 | 34 | Press [Y] to accept license and [V] to view license text: y |
|
35 | 35 | |
|
36 | ||
|
37 | .. important:: | |
|
38 | ||
|
39 | We recommend running RhodeCode as a non-root user, such as `rhodecode`; | |
|
40 | this user must have a proper home directory. | |
|
41 | Either log in as that user to install the software, or do it as root | |
|
42 | with `sudo -i -u rhodecode ./RhodeCode-installer-linux-*` | |
|
43 | ||
|
44 | ||
|
36 | 45 | 3. Install a VCS Server, and configure it to start at boot. |
|
37 | 46 | |
|
38 | 47 | .. code-block:: bash |
|
39 | 48 | |
|
40 | 49 | $ rccontrol install VCSServer |
|
41 | 50 | |
|
42 | 51 | Agree to the licence agreement? [y/N]: y |
|
43 | 52 | IP to start the server on [127.0.0.1]: |
|
44 | 53 | Port for the server to start [10005]: |
|
45 | 54 | Creating new instance: vcsserver-1 |
|
46 | 55 | Installing RhodeCode VCSServer |
|
47 | 56 | Configuring RhodeCode VCS Server ... |
|
48 | 57 | Supervisord state is: RUNNING |
|
49 | 58 | Added process group vcsserver-1 |
|
50 | 59 | |
|
51 | 60 | |
|
52 | 61 | 4. Install |RCEE| or |RCCE|. If using MySQL or PostgreSQL, during |
|
53 | 62 | installation you'll be asked for your database credentials, so have them at hand. |
|
54 | 63 | Mysql or Postgres needs to be running and a new database needs to be created. |
|
55 | 64 | You don't need any credentials or to create a database for SQLite. |
|
56 | 65 | |
|
57 | 66 | .. code-block:: bash |
|
58 | 67 | :emphasize-lines: 11-16 |
|
59 | 68 | |
|
60 | 69 | $ rccontrol install Community |
|
61 | 70 | |
|
62 | 71 | or |
|
63 | 72 | |
|
64 | 73 | $ rccontrol install Enterprise |
|
65 | 74 | |
|
66 | 75 | Username [admin]: username |
|
67 | 76 | Password (min 6 chars): |
|
68 | 77 | Repeat for confirmation: |
|
69 | 78 | Email: your@mail.com |
|
70 | 79 | Respositories location [/home/brian/repos]: |
|
71 | 80 | IP to start the Enterprise server on [127.0.0.1]: |
|
72 | 81 | Port for the Enterprise server to use [10004]: |
|
73 | 82 | Database type - [s]qlite, [m]ysql, [p]ostresql: |
|
74 | 83 | PostgreSQL selected |
|
75 | 84 | Database host [127.0.0.1]: |
|
76 | 85 | Database port [5432]: |
|
77 | 86 | Database username: db-user-name |
|
78 | 87 | Database password: somepassword |
|
79 | 88 | Database name: example-db-name |
|
80 | 89 | |
|
81 | 90 | 5. Check the status of your installation. You |RCEE|/|RCCE| instance runs |
|
82 | 91 | on the URL displayed in the status message. |
|
83 | 92 | |
|
84 | 93 | .. code-block:: bash |
|
85 | 94 | |
|
86 | 95 | $ rccontrol status |
|
87 | 96 | |
|
88 | 97 | - NAME: enterprise-1 |
|
89 | 98 | - STATUS: RUNNING |
|
90 | 99 | - TYPE: Enterprise |
|
91 | 100 | - VERSION: 4.1.0 |
|
92 | 101 | - URL: http://127.0.0.1:10003 |
|
93 | 102 | |
|
94 | 103 | - NAME: vcsserver-1 |
|
95 | 104 | - STATUS: RUNNING |
|
96 | 105 | - TYPE: VCSServer |
|
97 | 106 | - VERSION: 4.1.0 |
|
98 | 107 | - URL: http://127.0.0.1:10001 |
|
99 | 108 | |
|
100 | 109 | .. note:: |
|
101 | 110 | |
|
102 | 111 | Recommended post quick start install instructions: |
|
103 | 112 | |
|
104 | 113 | * Read the documentation |
|
105 | 114 | * Carry out the :ref:`rhodecode-post-instal-ref` |
|
106 | 115 | * Set up :ref:`indexing-ref` |
|
107 | 116 | * Familiarise yourself with the :ref:`rhodecode-admin-ref` section. |
|
108 | 117 | |
|
109 | 118 | .. _rhodecode.com/download/: https://rhodecode.com/download/ |
|
110 | 119 | .. _rhodecode.com: https://rhodecode.com/ |
|
111 | 120 | .. _rhodecode.com/register: https://rhodecode.com/register/ |
|
112 | 121 | .. _rhodecode.com/download: https://rhodecode.com/download/ |
|
113 | 122 |
@@ -1,230 +1,239 b'' | |||
|
1 | 1 | |RCE| 4.18.0 |RNS| |
|
2 | 2 | ------------------ |
|
3 | 3 | |
|
4 | 4 | Release Date |
|
5 | 5 | ^^^^^^^^^^^^ |
|
6 | 6 | |
|
7 | 7 | - 2020-01-05 |
|
8 | 8 | |
|
9 | 9 | |
|
10 | 10 | New Features |
|
11 | 11 | ^^^^^^^^^^^^ |
|
12 | 12 | |
|
13 | 13 | - Artifacts: are no longer in BETA. New info page is available for uploaded artifacts |
|
14 | 14 | which exposes some useful information like sha256, various access urls etc, and also |
|
15 | 15 | allows deletion of artifacts, and updating their description. |
|
16 | 16 | - Artifacts: support new download url based on access to artifacts using new auth-token types. |
|
17 | 17 | - Artifacts: added ability to store artifacts using API, and internal cli upload. |
|
18 | 18 | This allows uploading of artifacts that can have 100s of GBs in size efficiently. |
|
19 | 19 | - Artifacts: added metadata logic to store various extra custom data for artifacts. |
|
20 | 20 | - Comments: added support for adding comment attachments using the artifacts logic. |
|
21 | 21 | Logged in users can now pick or drag and drop attachments into comment forms. |
|
22 | 22 | - Comments: enable linkification of certain patterns on comments in repo/pull request scopes. |
|
23 | 23 | This will render now active links to commits, pull-requests mentioned in comments body. |
|
24 | 24 | - Jira: new update integration plugin. |
|
25 | 25 | Plugin now fetches possible transitions from tickets and show them to users in the interface. |
|
26 | 26 | Allow sending extra attributes during a transition like `resolution` message. |
|
27 | 27 | - Navigation: Added new consistent and contextual way of creating new objects |
|
28 | 28 | likes gists, repositories, and repository groups using dedicated action (with a `+` sign) |
|
29 | 29 | available in the top navigation. |
|
30 | 30 | - Hovercards: added new tooltips and hovercards to expose certain information for objects shown in UI. |
|
31 | 31 | RhodeCode usernames, issues, pull-requests will have active hovercard logic that will |
|
32 | 32 | load extra information about them and exposing them to users. |
|
33 | 33 | - Files: all readme files found in repository file browser will be now rendered, allowing having readme per directory. |
|
34 | 34 | - Search: expose line counts in search files information. |
|
35 | 35 | - Audit-logs: expose download user audit logs as JSON file. |
|
36 | 36 | - Users: added description field for users. |
|
37 | 37 | Allows users to write a short BIO, or description of their role in the organization. |
|
38 | 38 | - Users: allow super-admins to change bound authentication type for users. |
|
39 | 39 | E.g internal rhodecode accounts can be changed to ldap easily from user settings page. |
|
40 | 40 | - Pull requests: simplified the UI for display view, hide less important information and expose the most important ones. |
|
41 | 41 | - Pull requests: add merge check that detects WIP marker in title. |
|
42 | 42 | Usually WIP in title means unfinished task that needs still some work, such marker will prevent accidental merges. |
|
43 | 43 | - Pull requests: TODO comments have now a dedicated box below reviewers to keep track |
|
44 | 44 | of important TODOs that still need attention before review process is finalized. |
|
45 | 45 | - Pull requests: participants of pull request will receive an email about update of a |
|
46 | 46 | pull requests with a small summary of changes made. |
|
47 | 47 | - Pull requests: change the naming from #NUM into !NUM. |
|
48 | 48 | !NUM format is now parsed and linkified in comments and commit messages. |
|
49 | 49 | - Pull requests: pull requests which state is changing can now be viewed with a limited view. |
|
50 | 50 | - Pull requests: re-organize merge/close buttons and merge checks according to the new UI. |
|
51 | 51 | - Pull requests: update commits button allows a force-refresh update now using dropdown option. |
|
52 | 52 | - Pull requests: added quick filter to grid view to filter/search pull requests in a repository. |
|
53 | 53 | - Pull requests: closing a pull-request without a merge requires additional confirmation now. |
|
54 | 54 | - Pull requests: merge checks will now show which files caused conflicts and are blocking the merge. |
|
55 | 55 | - Emails: updated all generated emails design and cleanup the data fields they expose. |
|
56 | 56 | a) More consistent UI for all types of emails. b) Improved formatting of plaintext emails |
|
57 | 57 | c) Added reply link to comment type emails for quicker response action. |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | General |
|
61 | 61 | ^^^^^^^ |
|
62 | 62 | |
|
63 | 63 | - Artifacts: don't show hidden artifacts, allow showing them via a GET ?hidden=1 flag. |
|
64 | 64 | Hidden artifacts are for example comment attachments. |
|
65 | 65 | - UI: new commits page, according to the new design, which started on 4.17.X release lines |
|
66 | 66 | - UI: use explicit named actions like "create user" instead of generic "save" which is bad UX. |
|
67 | 67 | - UI: fixed problems with generating last change in repository groups. |
|
68 | 68 | There's now a new logic that checks all objects inside group for latest update time. |
|
69 | 69 | - API: add artifact `get_info`, and `store_metadata` methods. |
|
70 | 70 | - API: allowed to specify extra recipients for pr/commit comments api methods. |
|
71 | 71 | - Vcsserver: set file based cache as default for vcsserver which can be shared |
|
72 | 72 | across multiple workers saving memory usage. |
|
73 | 73 | - Vcsserver: added redis as possible cache backend for even greater performance. |
|
74 | 74 | - Dependencies: bumped GIT version to 2.23.0 |
|
75 | 75 | - Dependencies: bumped SVN version to 1.12.2 |
|
76 | 76 | - Dependencies: bumped Mercurial version to 5.1.1 and hg-evolve to 9.1.0 |
|
77 | 77 | - Search: added logic for sorting ElasticSearch6 backend search results. |
|
78 | 78 | - User bookmarks: make it easier to re-organize existing entries. |
|
79 | 79 | - Data grids: hide pagination for single pages in grids. |
|
80 | 80 | - Gists: UX, removed private/public gist buttons and replaced them with radio group. |
|
81 | 81 | - Gunicorn: moved all configuration of gunicorn workers to .ini files. |
|
82 | 82 | - Gunicorn: added worker memory management allowing setting maximum per-worker memory usage. |
|
83 | 83 | - Automation: moved update groups task into celery task |
|
84 | 84 | - Cache commits: add option to refresh caches manually from advanced pages. |
|
85 | 85 | - Pull requests: add indication of state change in list of pull-requests and actually show them in the list. |
|
86 | 86 | - Cache keys: register and self cleanup cache keys used for invalidation to prevent leaking lot of them into DB on worker recycle |
|
87 | 87 | - Repo groups: removed locking inheritance flag from repo-groups. We'll deprecate this soon and this only brings in confusion |
|
88 | 88 | - System snapshot: improved formatting for better readability |
|
89 | 89 | - System info: expose data about vcsserver. |
|
90 | 90 | - Packages: updated celery to 4.3.0 and switch default backend to redis instead of RabbitMQ. |
|
91 | 91 | Redis is stable enough and easier to install. Having Redis simplifies the stack as it's used in other parts of RhodeCode. |
|
92 | 92 | - Dependencies: bumped alembic to 1.2.1 |
|
93 | 93 | - Dependencies: bumped amqp==2.5.2 and kombu==4.6.6 |
|
94 | 94 | - Dependencies: bumped atomicwrites==1.3.0 |
|
95 | 95 | - Dependencies: bumped cffi==1.12.3 |
|
96 | 96 | - Dependencies: bumped configparser==4.0.2 |
|
97 | 97 | - Dependencies: bumped deform==2.0.8 |
|
98 | 98 | - Dependencies: bumped dogpile.cache==0.9.0 |
|
99 | 99 | - Dependencies: bumped hupper==1.8.1 |
|
100 | 100 | - Dependencies: bumped mako to 1.1.0 |
|
101 | 101 | - Dependencies: bumped markupsafe to 1.1.1 |
|
102 | 102 | - Dependencies: bumped packaging==19.2 |
|
103 | 103 | - Dependencies: bumped paste==3.2.1 |
|
104 | 104 | - Dependencies: bumped pastescript==3.2.0 |
|
105 | 105 | - Dependencies: bumped pathlib2 to 2.3.4 |
|
106 | 106 | - Dependencies: bumped pluggy==0.13.0 |
|
107 | 107 | - Dependencies: bumped psutil to 5.6.3 |
|
108 | 108 | - Dependencies: bumped psutil==5.6.5 |
|
109 | 109 | - Dependencies: bumped psycopg2==2.8.4 |
|
110 | 110 | - Dependencies: bumped pycurl to 7.43.0.3 |
|
111 | 111 | - Dependencies: bumped pyotp==2.3.0 |
|
112 | 112 | - Dependencies: bumped pyparsing to 2.4.2 |
|
113 | 113 | - Dependencies: bumped pyramid-debugtoolbar==4.5.1 |
|
114 | 114 | - Dependencies: bumped pyramid-mako to 1.1.0 |
|
115 | 115 | - Dependencies: bumped redis to 3.3.8 |
|
116 | 116 | - Dependencies: bumped sqlalchemy to 1.3.8 |
|
117 | 117 | - Dependencies: bumped sqlalchemy==1.3.11 |
|
118 | 118 | - Dependencies: bumped test libraries. |
|
119 | 119 | - Dependencies: freeze alembic==1.3.1 |
|
120 | 120 | - Dependencies: freeze python-dateutil |
|
121 | 121 | - Dependencies: freeze redis==3.3.11 |
|
122 | 122 | - Dependencies: freeze supervisor==4.1.0 |
|
123 | 123 | |
|
124 | 124 | |
|
125 | 125 | Security |
|
126 | 126 | ^^^^^^^^ |
|
127 | 127 | |
|
128 | 128 | - Security: fixed issues with exposing wrong http status (403) indicating repository with |
|
129 | 129 | given name exists and we don't have permissions to it. This was exposed in the redirection |
|
130 | 130 | logic of the global pull-request page. In case of redirection we also exposed |
|
131 | 131 | repository name in the URL. |
|
132 | 132 | |
|
133 | 133 | |
|
134 | 134 | Performance |
|
135 | 135 | ^^^^^^^^^^^ |
|
136 | 136 | |
|
137 | 137 | - Core: many various small improvements and optimizations to make rhodecode faster then before. |
|
138 | 138 | - VCSServer: new cache implementation for remote functions. |
|
139 | 139 | Single worker shared caches that can use redis/file-cache. |
|
140 | 140 | This greatly improves performance on larger instances, and doesn't trigger cache |
|
141 | 141 | re-calculation on worker restarts. |
|
142 | 142 | - GIT: switched internal git operations from Dulwich to libgit2 in order to obtain better performance and scalability. |
|
143 | 143 | - SSH: skip loading unneeded application parts for SSH to make execution of ssh commands faster. |
|
144 | 144 | - Main page: main page will now load repositories and repositories groups using partial DB calls instead of big JSON files. |
|
145 | 145 | In case of many repositories in root this could lead to very slow page rendering. |
|
146 | 146 | - Admin pages: made all grids use same DB based partial loading logic. We'll no longer fetch |
|
147 | 147 | all objects into JSON for display purposes. This significantly improves speed of those pages in case |
|
148 | 148 | of many objects shown in them. |
|
149 | 149 | - Summary page: use non-memory cache for readme, and cleanup cache for repo stats. |
|
150 | 150 | This change won't re-cache after worker restarts and can be shared across all workers |
|
151 | 151 | - Files: only check for git_lfs/hg_largefiles if they are enabled. |
|
152 | 152 | This speeds up fetching of files if they are not LF and very big. |
|
153 | 153 | - Vcsserver: added support for streaming data from the remote methods. This allows |
|
154 | 154 | to stream very large files without taking up memory, mostly for usage in SVN when |
|
155 | 155 | downloading large binaries from vcs system. |
|
156 | 156 | - Files: added streaming remote attributes for vcsserver. |
|
157 | 157 | This change enables streaming raw content or raw downloads of large files without |
|
158 | 158 | transferring them over to enterprise for pack & repack using msgpack. |
|
159 | 159 | Msgpack has a limit of 2gb and generally pack+repack for ~2gb is very slow. |
|
160 | 160 | - Files: ensure over size limit files never do any content fetching when viewing such files. |
|
161 | 161 | - VCSServer: skip host verification to speed up pycurl calls. |
|
162 | 162 | - User-bookmarks: cache fetching of bookmarks since this is quite expensive query to |
|
163 | 163 | make with joinedload on repos/repo groups. |
|
164 | 164 | - Goto-switcher: reduce query data to only required attributes for speedups. |
|
165 | 165 | - My account: owner/watched repos are now loaded only using DB queries. |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | Fixes |
|
169 | 169 | ^^^^^ |
|
170 | 170 | |
|
171 | 171 | - Mercurial: move imports from top-level to prevent from loading mercurial code on hook execution for svn/git. |
|
172 | 172 | - GIT: limit sync-fetch logic to only retrieve tags/ and heads/ with default execution arguments. |
|
173 | 173 | - GIT: fixed issue with git submodules detection. |
|
174 | 174 | - SVN: fix checkout url for ssh+svn backend not having special prefix resulting in incorrect command shown. |
|
175 | 175 | - SVN: fixed problem with showing empty directories. |
|
176 | 176 | - OAuth: use a vendored version of `authomatic` library, and switch Bitbucket authentication to use oauth2. |
|
177 | 177 | - Diffs: handle paths with quotes in diffs. |
|
178 | 178 | - Diffs: fixed outdated files in pull-requests re-using the filediff raw_id for anchor generation. Fixes #5567 |
|
179 | 179 | - Diffs: toggle race condition on sticky vs wide-diff-mode that caused some display problems on larger diffs. |
|
180 | 180 | - Pull requests: handle exceptions in state change and improve logging. |
|
181 | 181 | - Pull requests: fixed title/description generation for single commits which are numbers. |
|
182 | 182 | - Pull requests: changed the source of changes to be using shadow repos if it exists. |
|
183 | 183 | In case of `git push -f` and rebase we lost commits in the repo resulting in |
|
184 | 184 | problems of displaying versions of pull-requests. |
|
185 | 185 | - Pull requests: handle case when removing existing files from a repository in compare versions diff. |
|
186 | 186 | - Files: don't expose copy content helper in case of binary files. |
|
187 | 187 | - Registration: properly expose first_name/last_name into email on user registration. |
|
188 | 188 | - Markup renderers: fixed broken code highlight for rst files. |
|
189 | 189 | - Ui: make super admin be named consistently across ui. |
|
190 | 190 | - Audit logs: fixed search cases with special chars such as `-`. |
|
191 | 191 | |
|
192 | 192 | |
|
193 | 193 | Upgrade notes |
|
194 | 194 | ^^^^^^^^^^^^^ |
|
195 | 195 | |
|
196 | - Major Celery Version upgrade. The 4.18.X release includes a major Celery version. | |
|
197 | It's recommended to run `rccontrol self-stop && rccontrol self-init` after the | |
|
198 | upgrade to ensure celery workers are restarted and updated. | |
|
199 | ||
|
196 | 200 | - New Automation task. We've changed the logic for updating latest change inside repository group. |
|
197 | 201 | New logic includes scanning for changes in all nested objects. Since this is a heavy task |
|
198 | 202 | a new dedicated scheduler task has been created to update it automatically on a scheduled base. |
|
199 | 203 | Please review in `admin > settings > automation` to enable this task. |
|
200 | 204 | |
|
201 | 205 | - New safer encryption algorithm. Some setting values are encrypted before storing it inside the database. |
|
202 | 206 | To keep full backward compatibility old AES algorithm is used. |
|
203 | 207 | If you wish to enable a safer option set fernet encryption instead inside rhodecode.ini |
|
204 | 208 | `rhodecode.encrypted_values.algorithm = fernet` |
|
205 | 209 | |
|
206 | 210 | - Pull requests UI changes. We've simplified the UI on pull requests page. |
|
207 | 211 | Please review the new UI to prevent surprises. All actions from old UI should be still possible with the new one. |
|
208 | 212 | |
|
209 | 213 | - Redis is now a default recommended backend for Celery and replaces previous rabbitmq. |
|
210 | 214 | Redis is generally easier to manage and install, and it's also very stable for usage |
|
211 | 215 | in the scheduler/celery async tasks. Since we also recommend Redis for caches the application |
|
212 | 216 | stack can be simplified by removing rabbitmq and replacing it with single Redis instance. |
|
213 | 217 | |
|
214 | 218 | - Recommendation for using Redis as the new cache backend on vcsserver. |
|
215 | 219 | Since Version 4.18.0 VCSServer has a new cache implementation for VCS data. |
|
216 | 220 | By default, for simplicity the cache type is file based. We strongly recommend using |
|
217 | 221 | Redis instead for better Performance and scalability |
|
218 | 222 | Please review vcsserver.ini settings under: |
|
219 | 223 | `rc_cache.repo_object.backend = dogpile.cache.rc.redis_msgpack` |
|
220 | 224 | |
|
225 | - Gunicorn configuration now moved to .ini files. | |
|
226 | Upgrading to 4.18.X will overwrite the gunicorn_conf.py file. If there are any custom changes in that file | |
|
227 | they will be lost. Recommended way to configure gunicorn is now via the .ini files. Please check `rhodecode.template.ini` file | |
|
228 | for example gunicorn configuration. | |
|
229 | ||
|
221 | 230 | - New memory monitoring for Gunicorn workers. Starting from 4.18 release a option was added |
|
222 | 231 | to limit the maximum amount of memory used by a worker. |
|
223 | 232 | Please review new settings in `[server:main]` section for memory management in both |
|
224 | 233 | rhodecode.ini and vcsserver.ini:: |
|
225 | 234 | |
|
226 | 235 | ; Maximum memory usage that each worker can use before it will receive a |
|
227 | 236 | ; graceful restart signal 0 = memory monitoring is disabled |
|
228 | 237 | ; Examples: 268435456 (256MB), 536870912 (512MB) |
|
229 | 238 | ; 1073741824 (1GB), 2147483648 (2GB), 4294967296 (4GB) |
|
230 | 239 | memory_max_usage = 0 |
@@ -1,137 +1,140 b'' | |||
|
1 | 1 | .. _rhodecode-release-notes-ref: |
|
2 | 2 | |
|
3 | 3 | Release Notes |
|
4 | 4 | ============= |
|
5 | 5 | |
|
6 | 6 | |RCE| 4.x Versions |
|
7 | 7 | ------------------ |
|
8 | 8 | |
|
9 | 9 | .. toctree:: |
|
10 | 10 | :maxdepth: 1 |
|
11 | 11 | |
|
12 | release-notes-4.18.3.rst | |
|
13 | release-notes-4.18.2.rst | |
|
14 | release-notes-4.18.1.rst | |
|
12 | 15 | release-notes-4.18.0.rst |
|
13 | 16 | release-notes-4.17.4.rst |
|
14 | 17 | release-notes-4.17.3.rst |
|
15 | 18 | release-notes-4.17.2.rst |
|
16 | 19 | release-notes-4.17.1.rst |
|
17 | 20 | release-notes-4.17.0.rst |
|
18 | 21 | release-notes-4.16.2.rst |
|
19 | 22 | release-notes-4.16.1.rst |
|
20 | 23 | release-notes-4.16.0.rst |
|
21 | 24 | release-notes-4.15.2.rst |
|
22 | 25 | release-notes-4.15.1.rst |
|
23 | 26 | release-notes-4.15.0.rst |
|
24 | 27 | release-notes-4.14.1.rst |
|
25 | 28 | release-notes-4.14.0.rst |
|
26 | 29 | release-notes-4.13.3.rst |
|
27 | 30 | release-notes-4.13.2.rst |
|
28 | 31 | release-notes-4.13.1.rst |
|
29 | 32 | release-notes-4.13.0.rst |
|
30 | 33 | release-notes-4.12.4.rst |
|
31 | 34 | release-notes-4.12.3.rst |
|
32 | 35 | release-notes-4.12.2.rst |
|
33 | 36 | release-notes-4.12.1.rst |
|
34 | 37 | release-notes-4.12.0.rst |
|
35 | 38 | release-notes-4.11.6.rst |
|
36 | 39 | release-notes-4.11.5.rst |
|
37 | 40 | release-notes-4.11.4.rst |
|
38 | 41 | release-notes-4.11.3.rst |
|
39 | 42 | release-notes-4.11.2.rst |
|
40 | 43 | release-notes-4.11.1.rst |
|
41 | 44 | release-notes-4.11.0.rst |
|
42 | 45 | release-notes-4.10.6.rst |
|
43 | 46 | release-notes-4.10.5.rst |
|
44 | 47 | release-notes-4.10.4.rst |
|
45 | 48 | release-notes-4.10.3.rst |
|
46 | 49 | release-notes-4.10.2.rst |
|
47 | 50 | release-notes-4.10.1.rst |
|
48 | 51 | release-notes-4.10.0.rst |
|
49 | 52 | release-notes-4.9.1.rst |
|
50 | 53 | release-notes-4.9.0.rst |
|
51 | 54 | release-notes-4.8.0.rst |
|
52 | 55 | release-notes-4.7.2.rst |
|
53 | 56 | release-notes-4.7.1.rst |
|
54 | 57 | release-notes-4.7.0.rst |
|
55 | 58 | release-notes-4.6.1.rst |
|
56 | 59 | release-notes-4.6.0.rst |
|
57 | 60 | release-notes-4.5.2.rst |
|
58 | 61 | release-notes-4.5.1.rst |
|
59 | 62 | release-notes-4.5.0.rst |
|
60 | 63 | release-notes-4.4.2.rst |
|
61 | 64 | release-notes-4.4.1.rst |
|
62 | 65 | release-notes-4.4.0.rst |
|
63 | 66 | release-notes-4.3.1.rst |
|
64 | 67 | release-notes-4.3.0.rst |
|
65 | 68 | release-notes-4.2.1.rst |
|
66 | 69 | release-notes-4.2.0.rst |
|
67 | 70 | release-notes-4.1.2.rst |
|
68 | 71 | release-notes-4.1.1.rst |
|
69 | 72 | release-notes-4.1.0.rst |
|
70 | 73 | release-notes-4.0.1.rst |
|
71 | 74 | release-notes-4.0.0.rst |
|
72 | 75 | |
|
73 | 76 | |RCE| 3.x Versions |
|
74 | 77 | ------------------ |
|
75 | 78 | |
|
76 | 79 | .. toctree:: |
|
77 | 80 | :maxdepth: 1 |
|
78 | 81 | |
|
79 | 82 | release-notes-3.8.4.rst |
|
80 | 83 | release-notes-3.8.3.rst |
|
81 | 84 | release-notes-3.8.2.rst |
|
82 | 85 | release-notes-3.8.1.rst |
|
83 | 86 | release-notes-3.8.0.rst |
|
84 | 87 | release-notes-3.7.1.rst |
|
85 | 88 | release-notes-3.7.0.rst |
|
86 | 89 | release-notes-3.6.1.rst |
|
87 | 90 | release-notes-3.6.0.rst |
|
88 | 91 | release-notes-3.5.2.rst |
|
89 | 92 | release-notes-3.5.1.rst |
|
90 | 93 | release-notes-3.5.0.rst |
|
91 | 94 | release-notes-3.4.1.rst |
|
92 | 95 | release-notes-3.4.0.rst |
|
93 | 96 | release-notes-3.3.4.rst |
|
94 | 97 | release-notes-3.3.3.rst |
|
95 | 98 | release-notes-3.3.2.rst |
|
96 | 99 | release-notes-3.3.1.rst |
|
97 | 100 | release-notes-3.3.0.rst |
|
98 | 101 | release-notes-3.2.3.rst |
|
99 | 102 | release-notes-3.2.2.rst |
|
100 | 103 | release-notes-3.2.1.rst |
|
101 | 104 | release-notes-3.2.0.rst |
|
102 | 105 | release-notes-3.1.1.rst |
|
103 | 106 | release-notes-3.1.0.rst |
|
104 | 107 | release-notes-3.0.2.rst |
|
105 | 108 | release-notes-3.0.1.rst |
|
106 | 109 | release-notes-3.0.0.rst |
|
107 | 110 | |
|
108 | 111 | |RCE| 2.x Versions |
|
109 | 112 | ------------------ |
|
110 | 113 | |
|
111 | 114 | .. toctree:: |
|
112 | 115 | :maxdepth: 1 |
|
113 | 116 | |
|
114 | 117 | release-notes-2.2.8.rst |
|
115 | 118 | release-notes-2.2.7.rst |
|
116 | 119 | release-notes-2.2.6.rst |
|
117 | 120 | release-notes-2.2.5.rst |
|
118 | 121 | release-notes-2.2.4.rst |
|
119 | 122 | release-notes-2.2.3.rst |
|
120 | 123 | release-notes-2.2.2.rst |
|
121 | 124 | release-notes-2.2.1.rst |
|
122 | 125 | release-notes-2.2.0.rst |
|
123 | 126 | release-notes-2.1.0.rst |
|
124 | 127 | release-notes-2.0.2.rst |
|
125 | 128 | release-notes-2.0.1.rst |
|
126 | 129 | release-notes-2.0.0.rst |
|
127 | 130 | |
|
128 | 131 | |RCE| 1.x Versions |
|
129 | 132 | ------------------ |
|
130 | 133 | |
|
131 | 134 | .. toctree:: |
|
132 | 135 | :maxdepth: 1 |
|
133 | 136 | |
|
134 | 137 | release-notes-1.7.2.rst |
|
135 | 138 | release-notes-1.7.1.rst |
|
136 | 139 | release-notes-1.7.0.rst |
|
137 | 140 | release-notes-1.6.0.rst |
@@ -1,554 +1,555 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | import inspect | |
|
22 | 21 | import itertools |
|
23 | 22 | import logging |
|
24 | 23 | import sys |
|
25 | 24 | import types |
|
26 | 25 | import fnmatch |
|
27 | 26 | |
|
28 | 27 | import decorator |
|
29 | 28 | import venusian |
|
30 | 29 | from collections import OrderedDict |
|
31 | 30 | |
|
32 | 31 | from pyramid.exceptions import ConfigurationError |
|
33 | 32 | from pyramid.renderers import render |
|
34 | 33 | from pyramid.response import Response |
|
35 | 34 | from pyramid.httpexceptions import HTTPNotFound |
|
36 | 35 | |
|
37 | 36 | from rhodecode.api.exc import ( |
|
38 | 37 | JSONRPCBaseError, JSONRPCError, JSONRPCForbidden, JSONRPCValidationError) |
|
39 | 38 | from rhodecode.apps._base import TemplateArgs |
|
40 | 39 | from rhodecode.lib.auth import AuthUser |
|
41 | 40 | from rhodecode.lib.base import get_ip_addr, attach_context_attributes |
|
42 | 41 | from rhodecode.lib.exc_tracking import store_exception |
|
43 | 42 | from rhodecode.lib.ext_json import json |
|
44 | 43 | from rhodecode.lib.utils2 import safe_str |
|
45 | 44 | from rhodecode.lib.plugins.utils import get_plugin_settings |
|
46 | 45 | from rhodecode.model.db import User, UserApiKeys |
|
47 | 46 | |
|
48 | 47 | log = logging.getLogger(__name__) |
|
49 | 48 | |
|
50 | 49 | DEFAULT_RENDERER = 'jsonrpc_renderer' |
|
51 | 50 | DEFAULT_URL = '/_admin/apiv2' |
|
52 | 51 | |
|
53 | 52 | |
|
54 | 53 | def find_methods(jsonrpc_methods, pattern): |
|
55 | 54 | matches = OrderedDict() |
|
56 | 55 | if not isinstance(pattern, (list, tuple)): |
|
57 | 56 | pattern = [pattern] |
|
58 | 57 | |
|
59 | 58 | for single_pattern in pattern: |
|
60 | 59 | for method_name, method in jsonrpc_methods.items(): |
|
61 | 60 | if fnmatch.fnmatch(method_name, single_pattern): |
|
62 | 61 | matches[method_name] = method |
|
63 | 62 | return matches |
|
64 | 63 | |
|
65 | 64 | |
|
66 | 65 | class ExtJsonRenderer(object): |
|
67 | 66 | """ |
|
68 | 67 | Custom renderer that mkaes use of our ext_json lib |
|
69 | 68 | |
|
70 | 69 | """ |
|
71 | 70 | |
|
72 | 71 | def __init__(self, serializer=json.dumps, **kw): |
|
73 | 72 | """ Any keyword arguments will be passed to the ``serializer`` |
|
74 | 73 | function.""" |
|
75 | 74 | self.serializer = serializer |
|
76 | 75 | self.kw = kw |
|
77 | 76 | |
|
78 | 77 | def __call__(self, info): |
|
79 | 78 | """ Returns a plain JSON-encoded string with content-type |
|
80 | 79 | ``application/json``. The content-type may be overridden by |
|
81 | 80 | setting ``request.response.content_type``.""" |
|
82 | 81 | |
|
83 | 82 | def _render(value, system): |
|
84 | 83 | request = system.get('request') |
|
85 | 84 | if request is not None: |
|
86 | 85 | response = request.response |
|
87 | 86 | ct = response.content_type |
|
88 | 87 | if ct == response.default_content_type: |
|
89 | 88 | response.content_type = 'application/json' |
|
90 | 89 | |
|
91 | 90 | return self.serializer(value, **self.kw) |
|
92 | 91 | |
|
93 | 92 | return _render |
|
94 | 93 | |
|
95 | 94 | |
|
96 | 95 | def jsonrpc_response(request, result): |
|
97 | 96 | rpc_id = getattr(request, 'rpc_id', None) |
|
98 | 97 | response = request.response |
|
99 | 98 | |
|
100 | 99 | # store content_type before render is called |
|
101 | 100 | ct = response.content_type |
|
102 | 101 | |
|
103 | 102 | ret_value = '' |
|
104 | 103 | if rpc_id: |
|
105 | 104 | ret_value = { |
|
106 | 105 | 'id': rpc_id, |
|
107 | 106 | 'result': result, |
|
108 | 107 | 'error': None, |
|
109 | 108 | } |
|
110 | 109 | |
|
111 | 110 | # fetch deprecation warnings, and store it inside results |
|
112 | 111 | deprecation = getattr(request, 'rpc_deprecation', None) |
|
113 | 112 | if deprecation: |
|
114 | 113 | ret_value['DEPRECATION_WARNING'] = deprecation |
|
115 | 114 | |
|
116 | 115 | raw_body = render(DEFAULT_RENDERER, ret_value, request=request) |
|
117 | 116 | response.body = safe_str(raw_body, response.charset) |
|
118 | 117 | |
|
119 | 118 | if ct == response.default_content_type: |
|
120 | 119 | response.content_type = 'application/json' |
|
121 | 120 | |
|
122 | 121 | return response |
|
123 | 122 | |
|
124 | 123 | |
|
125 | 124 | def jsonrpc_error(request, message, retid=None, code=None, headers=None): |
|
126 | 125 | """ |
|
127 | 126 | Generate a Response object with a JSON-RPC error body |
|
128 | 127 | |
|
129 | 128 | :param code: |
|
130 | 129 | :param retid: |
|
131 | 130 | :param message: |
|
132 | 131 | """ |
|
133 | 132 | err_dict = {'id': retid, 'result': None, 'error': message} |
|
134 | 133 | body = render(DEFAULT_RENDERER, err_dict, request=request).encode('utf-8') |
|
135 | 134 | |
|
136 | 135 | return Response( |
|
137 | 136 | body=body, |
|
138 | 137 | status=code, |
|
139 | 138 | content_type='application/json', |
|
140 | 139 | headerlist=headers |
|
141 | 140 | ) |
|
142 | 141 | |
|
143 | 142 | |
|
144 | 143 | def exception_view(exc, request): |
|
145 | 144 | rpc_id = getattr(request, 'rpc_id', None) |
|
146 | 145 | |
|
147 | 146 | if isinstance(exc, JSONRPCError): |
|
148 | 147 | fault_message = safe_str(exc.message) |
|
149 | 148 | log.debug('json-rpc error rpc_id:%s "%s"', rpc_id, fault_message) |
|
150 | 149 | elif isinstance(exc, JSONRPCValidationError): |
|
151 | 150 | colander_exc = exc.colander_exception |
|
152 | 151 | # TODO(marcink): think maybe of nicer way to serialize errors ? |
|
153 | 152 | fault_message = colander_exc.asdict() |
|
154 | 153 | log.debug('json-rpc colander error rpc_id:%s "%s"', rpc_id, fault_message) |
|
155 | 154 | elif isinstance(exc, JSONRPCForbidden): |
|
156 | 155 | fault_message = 'Access was denied to this resource.' |
|
157 | 156 | log.warning('json-rpc forbidden call rpc_id:%s "%s"', rpc_id, fault_message) |
|
158 | 157 | elif isinstance(exc, HTTPNotFound): |
|
159 | 158 | method = request.rpc_method |
|
160 | 159 | log.debug('json-rpc method `%s` not found in list of ' |
|
161 | 160 | 'api calls: %s, rpc_id:%s', |
|
162 | 161 | method, request.registry.jsonrpc_methods.keys(), rpc_id) |
|
163 | 162 | |
|
164 | 163 | similar = 'none' |
|
165 | 164 | try: |
|
166 | 165 | similar_paterns = ['*{}*'.format(x) for x in method.split('_')] |
|
167 | 166 | similar_found = find_methods( |
|
168 | 167 | request.registry.jsonrpc_methods, similar_paterns) |
|
169 | 168 | similar = ', '.join(similar_found.keys()) or similar |
|
170 | 169 | except Exception: |
|
171 | 170 | # make the whole above block safe |
|
172 | 171 | pass |
|
173 | 172 | |
|
174 | 173 | fault_message = "No such method: {}. Similar methods: {}".format( |
|
175 | 174 | method, similar) |
|
176 | 175 | else: |
|
177 | 176 | fault_message = 'undefined error' |
|
178 | 177 | exc_info = exc.exc_info() |
|
179 | 178 | store_exception(id(exc_info), exc_info, prefix='rhodecode-api') |
|
180 | 179 | |
|
181 | 180 | return jsonrpc_error(request, fault_message, rpc_id) |
|
182 | 181 | |
|
183 | 182 | |
|
184 | 183 | def request_view(request): |
|
185 | 184 | """ |
|
186 | 185 | Main request handling method. It handles all logic to call a specific |
|
187 | 186 | exposed method |
|
188 | 187 | """ |
|
188 | # cython compatible inspect | |
|
189 | from rhodecode.config.patches import inspect_getargspec | |
|
190 | inspect = inspect_getargspec() | |
|
189 | 191 | |
|
190 | 192 | # check if we can find this session using api_key, get_by_auth_token |
|
191 | 193 | # search not expired tokens only |
|
192 | ||
|
193 | 194 | try: |
|
194 | 195 | api_user = User.get_by_auth_token(request.rpc_api_key) |
|
195 | 196 | |
|
196 | 197 | if api_user is None: |
|
197 | 198 | return jsonrpc_error( |
|
198 | 199 | request, retid=request.rpc_id, message='Invalid API KEY') |
|
199 | 200 | |
|
200 | 201 | if not api_user.active: |
|
201 | 202 | return jsonrpc_error( |
|
202 | 203 | request, retid=request.rpc_id, |
|
203 | 204 | message='Request from this user not allowed') |
|
204 | 205 | |
|
205 | 206 | # check if we are allowed to use this IP |
|
206 | 207 | auth_u = AuthUser( |
|
207 | 208 | api_user.user_id, request.rpc_api_key, ip_addr=request.rpc_ip_addr) |
|
208 | 209 | if not auth_u.ip_allowed: |
|
209 | 210 | return jsonrpc_error( |
|
210 | 211 | request, retid=request.rpc_id, |
|
211 | 212 | message='Request from IP:%s not allowed' % ( |
|
212 | 213 | request.rpc_ip_addr,)) |
|
213 | 214 | else: |
|
214 | 215 | log.info('Access for IP:%s allowed', request.rpc_ip_addr) |
|
215 | 216 | |
|
216 | 217 | # register our auth-user |
|
217 | 218 | request.rpc_user = auth_u |
|
218 | 219 | request.environ['rc_auth_user_id'] = auth_u.user_id |
|
219 | 220 | |
|
220 | 221 | # now check if token is valid for API |
|
221 | 222 | auth_token = request.rpc_api_key |
|
222 | 223 | token_match = api_user.authenticate_by_token( |
|
223 | 224 | auth_token, roles=[UserApiKeys.ROLE_API]) |
|
224 | 225 | invalid_token = not token_match |
|
225 | 226 | |
|
226 | 227 | log.debug('Checking if API KEY is valid with proper role') |
|
227 | 228 | if invalid_token: |
|
228 | 229 | return jsonrpc_error( |
|
229 | 230 | request, retid=request.rpc_id, |
|
230 | 231 | message='API KEY invalid or, has bad role for an API call') |
|
231 | 232 | |
|
232 | 233 | except Exception: |
|
233 | 234 | log.exception('Error on API AUTH') |
|
234 | 235 | return jsonrpc_error( |
|
235 | 236 | request, retid=request.rpc_id, message='Invalid API KEY') |
|
236 | 237 | |
|
237 | 238 | method = request.rpc_method |
|
238 | 239 | func = request.registry.jsonrpc_methods[method] |
|
239 | 240 | |
|
240 | 241 | # now that we have a method, add request._req_params to |
|
241 | 242 | # self.kargs and dispatch control to WGIController |
|
242 | 243 | argspec = inspect.getargspec(func) |
|
243 | 244 | arglist = argspec[0] |
|
244 | 245 | defaults = map(type, argspec[3] or []) |
|
245 | 246 | default_empty = types.NotImplementedType |
|
246 | 247 | |
|
247 | 248 | # kw arguments required by this method |
|
248 | 249 | func_kwargs = dict(itertools.izip_longest( |
|
249 | 250 | reversed(arglist), reversed(defaults), fillvalue=default_empty)) |
|
250 | 251 | |
|
251 | 252 | # This attribute will need to be first param of a method that uses |
|
252 | 253 | # api_key, which is translated to instance of user at that name |
|
253 | 254 | user_var = 'apiuser' |
|
254 | 255 | request_var = 'request' |
|
255 | 256 | |
|
256 | 257 | for arg in [user_var, request_var]: |
|
257 | 258 | if arg not in arglist: |
|
258 | 259 | return jsonrpc_error( |
|
259 | 260 | request, |
|
260 | 261 | retid=request.rpc_id, |
|
261 | 262 | message='This method [%s] does not support ' |
|
262 | 263 | 'required parameter `%s`' % (func.__name__, arg)) |
|
263 | 264 | |
|
264 | 265 | # get our arglist and check if we provided them as args |
|
265 | 266 | for arg, default in func_kwargs.items(): |
|
266 | 267 | if arg in [user_var, request_var]: |
|
267 | 268 | # user_var and request_var are pre-hardcoded parameters and we |
|
268 | 269 | # don't need to do any translation |
|
269 | 270 | continue |
|
270 | 271 | |
|
271 | 272 | # skip the required param check if it's default value is |
|
272 | 273 | # NotImplementedType (default_empty) |
|
273 | 274 | if default == default_empty and arg not in request.rpc_params: |
|
274 | 275 | return jsonrpc_error( |
|
275 | 276 | request, |
|
276 | 277 | retid=request.rpc_id, |
|
277 | 278 | message=('Missing non optional `%s` arg in JSON DATA' % arg) |
|
278 | 279 | ) |
|
279 | 280 | |
|
280 | 281 | # sanitize extra passed arguments |
|
281 | 282 | for k in request.rpc_params.keys()[:]: |
|
282 | 283 | if k not in func_kwargs: |
|
283 | 284 | del request.rpc_params[k] |
|
284 | 285 | |
|
285 | 286 | call_params = request.rpc_params |
|
286 | 287 | call_params.update({ |
|
287 | 288 | 'request': request, |
|
288 | 289 | 'apiuser': auth_u |
|
289 | 290 | }) |
|
290 | 291 | |
|
291 | 292 | # register some common functions for usage |
|
292 | 293 | attach_context_attributes(TemplateArgs(), request, request.rpc_user.user_id) |
|
293 | 294 | |
|
294 | 295 | try: |
|
295 | 296 | ret_value = func(**call_params) |
|
296 | 297 | return jsonrpc_response(request, ret_value) |
|
297 | 298 | except JSONRPCBaseError: |
|
298 | 299 | raise |
|
299 | 300 | except Exception: |
|
300 | 301 | log.exception('Unhandled exception occurred on api call: %s', func) |
|
301 | 302 | exc_info = sys.exc_info() |
|
302 | 303 | exc_id, exc_type_name = store_exception( |
|
303 | 304 | id(exc_info), exc_info, prefix='rhodecode-api') |
|
304 | 305 | error_headers = [('RhodeCode-Exception-Id', str(exc_id)), |
|
305 | 306 | ('RhodeCode-Exception-Type', str(exc_type_name))] |
|
306 | 307 | return jsonrpc_error( |
|
307 | 308 | request, retid=request.rpc_id, message='Internal server error', |
|
308 | 309 | headers=error_headers) |
|
309 | 310 | |
|
310 | 311 | |
|
311 | 312 | def setup_request(request): |
|
312 | 313 | """ |
|
313 | 314 | Parse a JSON-RPC request body. It's used inside the predicates method |
|
314 | 315 | to validate and bootstrap requests for usage in rpc calls. |
|
315 | 316 | |
|
316 | 317 | We need to raise JSONRPCError here if we want to return some errors back to |
|
317 | 318 | user. |
|
318 | 319 | """ |
|
319 | 320 | |
|
320 | 321 | log.debug('Executing setup request: %r', request) |
|
321 | 322 | request.rpc_ip_addr = get_ip_addr(request.environ) |
|
322 | 323 | # TODO(marcink): deprecate GET at some point |
|
323 | 324 | if request.method not in ['POST', 'GET']: |
|
324 | 325 | log.debug('unsupported request method "%s"', request.method) |
|
325 | 326 | raise JSONRPCError( |
|
326 | 327 | 'unsupported request method "%s". Please use POST' % request.method) |
|
327 | 328 | |
|
328 | 329 | if 'CONTENT_LENGTH' not in request.environ: |
|
329 | 330 | log.debug("No Content-Length") |
|
330 | 331 | raise JSONRPCError("Empty body, No Content-Length in request") |
|
331 | 332 | |
|
332 | 333 | else: |
|
333 | 334 | length = request.environ['CONTENT_LENGTH'] |
|
334 | 335 | log.debug('Content-Length: %s', length) |
|
335 | 336 | |
|
336 | 337 | if length == 0: |
|
337 | 338 | log.debug("Content-Length is 0") |
|
338 | 339 | raise JSONRPCError("Content-Length is 0") |
|
339 | 340 | |
|
340 | 341 | raw_body = request.body |
|
341 | 342 | log.debug("Loading JSON body now") |
|
342 | 343 | try: |
|
343 | 344 | json_body = json.loads(raw_body) |
|
344 | 345 | except ValueError as e: |
|
345 | 346 | # catch JSON errors Here |
|
346 | 347 | raise JSONRPCError("JSON parse error ERR:%s RAW:%r" % (e, raw_body)) |
|
347 | 348 | |
|
348 | 349 | request.rpc_id = json_body.get('id') |
|
349 | 350 | request.rpc_method = json_body.get('method') |
|
350 | 351 | |
|
351 | 352 | # check required base parameters |
|
352 | 353 | try: |
|
353 | 354 | api_key = json_body.get('api_key') |
|
354 | 355 | if not api_key: |
|
355 | 356 | api_key = json_body.get('auth_token') |
|
356 | 357 | |
|
357 | 358 | if not api_key: |
|
358 | 359 | raise KeyError('api_key or auth_token') |
|
359 | 360 | |
|
360 | 361 | # TODO(marcink): support passing in token in request header |
|
361 | 362 | |
|
362 | 363 | request.rpc_api_key = api_key |
|
363 | 364 | request.rpc_id = json_body['id'] |
|
364 | 365 | request.rpc_method = json_body['method'] |
|
365 | 366 | request.rpc_params = json_body['args'] \ |
|
366 | 367 | if isinstance(json_body['args'], dict) else {} |
|
367 | 368 | |
|
368 | 369 | log.debug('method: %s, params: %.10240r', request.rpc_method, request.rpc_params) |
|
369 | 370 | except KeyError as e: |
|
370 | 371 | raise JSONRPCError('Incorrect JSON data. Missing %s' % e) |
|
371 | 372 | |
|
372 | 373 | log.debug('setup complete, now handling method:%s rpcid:%s', |
|
373 | 374 | request.rpc_method, request.rpc_id, ) |
|
374 | 375 | |
|
375 | 376 | |
|
376 | 377 | class RoutePredicate(object): |
|
377 | 378 | def __init__(self, val, config): |
|
378 | 379 | self.val = val |
|
379 | 380 | |
|
380 | 381 | def text(self): |
|
381 | 382 | return 'jsonrpc route = %s' % self.val |
|
382 | 383 | |
|
383 | 384 | phash = text |
|
384 | 385 | |
|
385 | 386 | def __call__(self, info, request): |
|
386 | 387 | if self.val: |
|
387 | 388 | # potentially setup and bootstrap our call |
|
388 | 389 | setup_request(request) |
|
389 | 390 | |
|
390 | 391 | # Always return True so that even if it isn't a valid RPC it |
|
391 | 392 | # will fall through to the underlaying handlers like notfound_view |
|
392 | 393 | return True |
|
393 | 394 | |
|
394 | 395 | |
|
395 | 396 | class NotFoundPredicate(object): |
|
396 | 397 | def __init__(self, val, config): |
|
397 | 398 | self.val = val |
|
398 | 399 | self.methods = config.registry.jsonrpc_methods |
|
399 | 400 | |
|
400 | 401 | def text(self): |
|
401 | 402 | return 'jsonrpc method not found = {}.'.format(self.val) |
|
402 | 403 | |
|
403 | 404 | phash = text |
|
404 | 405 | |
|
405 | 406 | def __call__(self, info, request): |
|
406 | 407 | return hasattr(request, 'rpc_method') |
|
407 | 408 | |
|
408 | 409 | |
|
409 | 410 | class MethodPredicate(object): |
|
410 | 411 | def __init__(self, val, config): |
|
411 | 412 | self.method = val |
|
412 | 413 | |
|
413 | 414 | def text(self): |
|
414 | 415 | return 'jsonrpc method = %s' % self.method |
|
415 | 416 | |
|
416 | 417 | phash = text |
|
417 | 418 | |
|
418 | 419 | def __call__(self, context, request): |
|
419 | 420 | # we need to explicitly return False here, so pyramid doesn't try to |
|
420 | 421 | # execute our view directly. We need our main handler to execute things |
|
421 | 422 | return getattr(request, 'rpc_method') == self.method |
|
422 | 423 | |
|
423 | 424 | |
|
424 | 425 | def add_jsonrpc_method(config, view, **kwargs): |
|
425 | 426 | # pop the method name |
|
426 | 427 | method = kwargs.pop('method', None) |
|
427 | 428 | |
|
428 | 429 | if method is None: |
|
429 | 430 | raise ConfigurationError( |
|
430 | 431 | 'Cannot register a JSON-RPC method without specifying the "method"') |
|
431 | 432 | |
|
432 | 433 | # we define custom predicate, to enable to detect conflicting methods, |
|
433 | 434 | # those predicates are kind of "translation" from the decorator variables |
|
434 | 435 | # to internal predicates names |
|
435 | 436 | |
|
436 | 437 | kwargs['jsonrpc_method'] = method |
|
437 | 438 | |
|
438 | 439 | # register our view into global view store for validation |
|
439 | 440 | config.registry.jsonrpc_methods[method] = view |
|
440 | 441 | |
|
441 | 442 | # we're using our main request_view handler, here, so each method |
|
442 | 443 | # has a unified handler for itself |
|
443 | 444 | config.add_view(request_view, route_name='apiv2', **kwargs) |
|
444 | 445 | |
|
445 | 446 | |
|
446 | 447 | class jsonrpc_method(object): |
|
447 | 448 | """ |
|
448 | 449 | decorator that works similar to @add_view_config decorator, |
|
449 | 450 | but tailored for our JSON RPC |
|
450 | 451 | """ |
|
451 | 452 | |
|
452 | 453 | venusian = venusian # for testing injection |
|
453 | 454 | |
|
454 | 455 | def __init__(self, method=None, **kwargs): |
|
455 | 456 | self.method = method |
|
456 | 457 | self.kwargs = kwargs |
|
457 | 458 | |
|
458 | 459 | def __call__(self, wrapped): |
|
459 | 460 | kwargs = self.kwargs.copy() |
|
460 | 461 | kwargs['method'] = self.method or wrapped.__name__ |
|
461 | 462 | depth = kwargs.pop('_depth', 0) |
|
462 | 463 | |
|
463 | 464 | def callback(context, name, ob): |
|
464 | 465 | config = context.config.with_package(info.module) |
|
465 | 466 | config.add_jsonrpc_method(view=ob, **kwargs) |
|
466 | 467 | |
|
467 | 468 | info = venusian.attach(wrapped, callback, category='pyramid', |
|
468 | 469 | depth=depth + 1) |
|
469 | 470 | if info.scope == 'class': |
|
470 | 471 | # ensure that attr is set if decorating a class method |
|
471 | 472 | kwargs.setdefault('attr', wrapped.__name__) |
|
472 | 473 | |
|
473 | 474 | kwargs['_info'] = info.codeinfo # fbo action_method |
|
474 | 475 | return wrapped |
|
475 | 476 | |
|
476 | 477 | |
|
477 | 478 | class jsonrpc_deprecated_method(object): |
|
478 | 479 | """ |
|
479 | 480 | Marks method as deprecated, adds log.warning, and inject special key to |
|
480 | 481 | the request variable to mark method as deprecated. |
|
481 | 482 | Also injects special docstring that extract_docs will catch to mark |
|
482 | 483 | method as deprecated. |
|
483 | 484 | |
|
484 | 485 | :param use_method: specify which method should be used instead of |
|
485 | 486 | the decorated one |
|
486 | 487 | |
|
487 | 488 | Use like:: |
|
488 | 489 | |
|
489 | 490 | @jsonrpc_method() |
|
490 | 491 | @jsonrpc_deprecated_method(use_method='new_func', deprecated_at_version='3.0.0') |
|
491 | 492 | def old_func(request, apiuser, arg1, arg2): |
|
492 | 493 | ... |
|
493 | 494 | """ |
|
494 | 495 | |
|
495 | 496 | def __init__(self, use_method, deprecated_at_version): |
|
496 | 497 | self.use_method = use_method |
|
497 | 498 | self.deprecated_at_version = deprecated_at_version |
|
498 | 499 | self.deprecated_msg = '' |
|
499 | 500 | |
|
500 | 501 | def __call__(self, func): |
|
501 | 502 | self.deprecated_msg = 'Please use method `{method}` instead.'.format( |
|
502 | 503 | method=self.use_method) |
|
503 | 504 | |
|
504 | 505 | docstring = """\n |
|
505 | 506 | .. deprecated:: {version} |
|
506 | 507 | |
|
507 | 508 | {deprecation_message} |
|
508 | 509 | |
|
509 | 510 | {original_docstring} |
|
510 | 511 | """ |
|
511 | 512 | func.__doc__ = docstring.format( |
|
512 | 513 | version=self.deprecated_at_version, |
|
513 | 514 | deprecation_message=self.deprecated_msg, |
|
514 | 515 | original_docstring=func.__doc__) |
|
515 | 516 | return decorator.decorator(self.__wrapper, func) |
|
516 | 517 | |
|
517 | 518 | def __wrapper(self, func, *fargs, **fkwargs): |
|
518 | 519 | log.warning('DEPRECATED API CALL on function %s, please ' |
|
519 | 520 | 'use `%s` instead', func, self.use_method) |
|
520 | 521 | # alter function docstring to mark as deprecated, this is picked up |
|
521 | 522 | # via fabric file that generates API DOC. |
|
522 | 523 | result = func(*fargs, **fkwargs) |
|
523 | 524 | |
|
524 | 525 | request = fargs[0] |
|
525 | 526 | request.rpc_deprecation = 'DEPRECATED METHOD ' + self.deprecated_msg |
|
526 | 527 | return result |
|
527 | 528 | |
|
528 | 529 | |
|
529 | 530 | def includeme(config): |
|
530 | 531 | plugin_module = 'rhodecode.api' |
|
531 | 532 | plugin_settings = get_plugin_settings( |
|
532 | 533 | plugin_module, config.registry.settings) |
|
533 | 534 | |
|
534 | 535 | if not hasattr(config.registry, 'jsonrpc_methods'): |
|
535 | 536 | config.registry.jsonrpc_methods = OrderedDict() |
|
536 | 537 | |
|
537 | 538 | # match filter by given method only |
|
538 | 539 | config.add_view_predicate('jsonrpc_method', MethodPredicate) |
|
539 | 540 | config.add_view_predicate('jsonrpc_method_not_found', NotFoundPredicate) |
|
540 | 541 | |
|
541 | 542 | config.add_renderer(DEFAULT_RENDERER, ExtJsonRenderer( |
|
542 | 543 | serializer=json.dumps, indent=4)) |
|
543 | 544 | config.add_directive('add_jsonrpc_method', add_jsonrpc_method) |
|
544 | 545 | |
|
545 | 546 | config.add_route_predicate( |
|
546 | 547 | 'jsonrpc_call', RoutePredicate) |
|
547 | 548 | |
|
548 | 549 | config.add_route( |
|
549 | 550 | 'apiv2', plugin_settings.get('url', DEFAULT_URL), jsonrpc_call=True) |
|
550 | 551 | |
|
551 | 552 | config.scan(plugin_module, ignore='rhodecode.api.tests') |
|
552 | 553 | # register some exception handling view |
|
553 | 554 | config.add_view(exception_view, context=JSONRPCBaseError) |
|
554 | 555 | config.add_notfound_view(exception_view, jsonrpc_method_not_found=True) |
@@ -1,60 +1,61 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.api.tests.utils import build_data, api_call, assert_ok |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | @pytest.mark.usefixtures("testuser_api", "app") |
|
28 | 28 | class TestGetMethod(object): |
|
29 | 29 | def test_get_methods_no_matches(self): |
|
30 | 30 | id_, params = build_data(self.apikey, 'get_method', pattern='hello') |
|
31 | 31 | response = api_call(self.app, params) |
|
32 | 32 | |
|
33 | 33 | expected = [] |
|
34 | 34 | assert_ok(id_, expected, given=response.body) |
|
35 | 35 | |
|
36 | 36 | def test_get_methods(self): |
|
37 | 37 | id_, params = build_data(self.apikey, 'get_method', pattern='*comment*') |
|
38 | 38 | response = api_call(self.app, params) |
|
39 | 39 | |
|
40 | 40 | expected = ['changeset_comment', 'comment_pull_request', |
|
41 | 41 | 'get_pull_request_comments', 'comment_commit', 'get_repo_comments'] |
|
42 | 42 | assert_ok(id_, expected, given=response.body) |
|
43 | 43 | |
|
44 | 44 | def test_get_methods_on_single_match(self): |
|
45 | 45 | id_, params = build_data(self.apikey, 'get_method', |
|
46 | 46 | pattern='*comment_commit*') |
|
47 | 47 | response = api_call(self.app, params) |
|
48 | 48 | |
|
49 | 49 | expected = ['comment_commit', |
|
50 | 50 | {'apiuser': '<RequiredType>', |
|
51 | 51 | 'comment_type': "<Optional:u'note'>", |
|
52 | 52 | 'commit_id': '<RequiredType>', |
|
53 | 53 | 'extra_recipients': '<Optional:[]>', |
|
54 | 54 | 'message': '<RequiredType>', |
|
55 | 55 | 'repoid': '<RequiredType>', |
|
56 | 56 | 'request': '<RequiredType>', |
|
57 | 57 | 'resolves_comment_id': '<Optional:None>', |
|
58 | 58 | 'status': '<Optional:None>', |
|
59 |
'userid': '<Optional:<OptionalAttr:apiuser>>' |
|
|
59 | 'userid': '<Optional:<OptionalAttr:apiuser>>', | |
|
60 | 'send_email': '<Optional:True>'}] | |
|
60 | 61 | assert_ok(id_, expected, given=response.body) |
@@ -1,90 +1,90 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.model.repo import RepoModel |
|
25 | 25 | from rhodecode.api.tests.utils import ( |
|
26 | 26 | build_data, api_call, assert_error, assert_ok, crash) |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | @pytest.mark.usefixtures("testuser_api", "app") |
|
30 | 30 | class TestGrantUserGroupPermission(object): |
|
31 | 31 | @pytest.mark.parametrize("name, perm", [ |
|
32 | 32 | ('none', 'repository.none'), |
|
33 | 33 | ('read', 'repository.read'), |
|
34 | 34 | ('write', 'repository.write'), |
|
35 | 35 | ('admin', 'repository.admin') |
|
36 | 36 | ]) |
|
37 | 37 | def test_api_grant_user_group_permission( |
|
38 | 38 | self, name, perm, backend, user_util): |
|
39 | 39 | user_group = user_util.create_user_group() |
|
40 | 40 | id_, params = build_data( |
|
41 | 41 | self.apikey, |
|
42 | 42 | 'grant_user_group_permission', |
|
43 | 43 | repoid=backend.repo_name, |
|
44 | 44 | usergroupid=user_group.users_group_name, |
|
45 | 45 | perm=perm) |
|
46 | 46 | response = api_call(self.app, params) |
|
47 | 47 | |
|
48 | 48 | ret = { |
|
49 | 49 | 'msg': 'Granted perm: `%s` for user group: `%s` in repo: `%s`' % ( |
|
50 | 50 | perm, user_group.users_group_name, backend.repo_name |
|
51 | 51 | ), |
|
52 | 52 | 'success': True |
|
53 | 53 | } |
|
54 | 54 | expected = ret |
|
55 | 55 | assert_ok(id_, expected, given=response.body) |
|
56 | 56 | |
|
57 | 57 | def test_api_grant_user_group_permission_wrong_permission( |
|
58 | 58 | self, backend, user_util): |
|
59 | 59 | perm = 'haha.no.permission' |
|
60 | 60 | user_group = user_util.create_user_group() |
|
61 | 61 | id_, params = build_data( |
|
62 | 62 | self.apikey, |
|
63 | 63 | 'grant_user_group_permission', |
|
64 | 64 | repoid=backend.repo_name, |
|
65 | 65 | usergroupid=user_group.users_group_name, |
|
66 | 66 | perm=perm) |
|
67 | 67 | response = api_call(self.app, params) |
|
68 | 68 | |
|
69 | expected = 'permission `%s` does not exist' % (perm,) | |
|
69 | expected = 'permission `%s` does not exist.' % (perm,) | |
|
70 | 70 | assert_error(id_, expected, given=response.body) |
|
71 | 71 | |
|
72 | 72 | @mock.patch.object(RepoModel, 'grant_user_group_permission', crash) |
|
73 | 73 | def test_api_grant_user_group_permission_exception_when_adding( |
|
74 | 74 | self, backend, user_util): |
|
75 | 75 | perm = 'repository.read' |
|
76 | 76 | user_group = user_util.create_user_group() |
|
77 | 77 | id_, params = build_data( |
|
78 | 78 | self.apikey, |
|
79 | 79 | 'grant_user_group_permission', |
|
80 | 80 | repoid=backend.repo_name, |
|
81 | 81 | usergroupid=user_group.users_group_name, |
|
82 | 82 | perm=perm) |
|
83 | 83 | response = api_call(self.app, params) |
|
84 | 84 | |
|
85 | 85 | expected = ( |
|
86 | 86 | 'failed to edit permission for user group: `%s` in repo: `%s`' % ( |
|
87 | 87 | user_group.users_group_name, backend.repo_name |
|
88 | 88 | ) |
|
89 | 89 | ) |
|
90 | 90 | assert_error(id_, expected, given=response.body) |
@@ -1,174 +1,173 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.model.user import UserModel |
|
25 | 25 | from rhodecode.model.repo_group import RepoGroupModel |
|
26 | 26 | from rhodecode.api.tests.utils import ( |
|
27 | 27 | build_data, api_call, assert_error, assert_ok, crash) |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | @pytest.mark.usefixtures("testuser_api", "app") |
|
31 | 31 | class TestGrantUserGroupPermissionFromRepoGroup(object): |
|
32 | 32 | @pytest.mark.parametrize("name, perm, apply_to_children", [ |
|
33 | 33 | ('none', 'group.none', 'none'), |
|
34 | 34 | ('read', 'group.read', 'none'), |
|
35 | 35 | ('write', 'group.write', 'none'), |
|
36 | 36 | ('admin', 'group.admin', 'none'), |
|
37 | 37 | |
|
38 | 38 | ('none', 'group.none', 'all'), |
|
39 | 39 | ('read', 'group.read', 'all'), |
|
40 | 40 | ('write', 'group.write', 'all'), |
|
41 | 41 | ('admin', 'group.admin', 'all'), |
|
42 | 42 | |
|
43 | 43 | ('none', 'group.none', 'repos'), |
|
44 | 44 | ('read', 'group.read', 'repos'), |
|
45 | 45 | ('write', 'group.write', 'repos'), |
|
46 | 46 | ('admin', 'group.admin', 'repos'), |
|
47 | 47 | |
|
48 | 48 | ('none', 'group.none', 'groups'), |
|
49 | 49 | ('read', 'group.read', 'groups'), |
|
50 | 50 | ('write', 'group.write', 'groups'), |
|
51 | 51 | ('admin', 'group.admin', 'groups'), |
|
52 | 52 | ]) |
|
53 | 53 | def test_api_grant_user_group_permission_to_repo_group( |
|
54 | 54 | self, name, perm, apply_to_children, user_util): |
|
55 | 55 | user_group = user_util.create_user_group() |
|
56 | 56 | repo_group = user_util.create_repo_group() |
|
57 | 57 | user_util.create_repo(parent=repo_group) |
|
58 | 58 | |
|
59 | 59 | id_, params = build_data( |
|
60 | 60 | self.apikey, |
|
61 | 61 | 'grant_user_group_permission_to_repo_group', |
|
62 | 62 | repogroupid=repo_group.name, |
|
63 | 63 | usergroupid=user_group.users_group_name, |
|
64 | 64 | perm=perm, |
|
65 | 65 | apply_to_children=apply_to_children,) |
|
66 | 66 | response = api_call(self.app, params) |
|
67 | 67 | |
|
68 | 68 | ret = { |
|
69 | 69 | 'msg': ( |
|
70 | 70 | 'Granted perm: `%s` (recursive:%s) for user group: `%s`' |
|
71 | 71 | ' in repo group: `%s`' % ( |
|
72 | 72 | perm, apply_to_children, user_group.users_group_name, |
|
73 | 73 | repo_group.name |
|
74 | 74 | ) |
|
75 | 75 | ), |
|
76 | 76 | 'success': True |
|
77 | 77 | } |
|
78 | 78 | expected = ret |
|
79 | 79 | try: |
|
80 | 80 | assert_ok(id_, expected, given=response.body) |
|
81 | 81 | finally: |
|
82 | 82 | RepoGroupModel().revoke_user_group_permission( |
|
83 | 83 | repo_group.group_id, user_group.users_group_id) |
|
84 | 84 | |
|
85 | 85 | @pytest.mark.parametrize( |
|
86 | 86 | "name, perm, apply_to_children, grant_admin, access_ok", [ |
|
87 | 87 | ('none_fails', 'group.none', 'none', False, False), |
|
88 | 88 | ('read_fails', 'group.read', 'none', False, False), |
|
89 | 89 | ('write_fails', 'group.write', 'none', False, False), |
|
90 | 90 | ('admin_fails', 'group.admin', 'none', False, False), |
|
91 | 91 | |
|
92 | 92 | # with granted perms |
|
93 | 93 | ('none_ok', 'group.none', 'none', True, True), |
|
94 | 94 | ('read_ok', 'group.read', 'none', True, True), |
|
95 | 95 | ('write_ok', 'group.write', 'none', True, True), |
|
96 | 96 | ('admin_ok', 'group.admin', 'none', True, True), |
|
97 | 97 | ] |
|
98 | 98 | ) |
|
99 | 99 | def test_api_grant_user_group_permission_to_repo_group_by_regular_user( |
|
100 | 100 | self, name, perm, apply_to_children, grant_admin, access_ok, |
|
101 | 101 | user_util): |
|
102 | 102 | user = UserModel().get_by_username(self.TEST_USER_LOGIN) |
|
103 | 103 | user_group = user_util.create_user_group() |
|
104 | 104 | repo_group = user_util.create_repo_group() |
|
105 | 105 | if grant_admin: |
|
106 | 106 | user_util.grant_user_permission_to_repo_group( |
|
107 | 107 | repo_group, user, 'group.admin') |
|
108 | 108 | |
|
109 | 109 | id_, params = build_data( |
|
110 | 110 | self.apikey_regular, |
|
111 | 111 | 'grant_user_group_permission_to_repo_group', |
|
112 | 112 | repogroupid=repo_group.name, |
|
113 | 113 | usergroupid=user_group.users_group_name, |
|
114 | 114 | perm=perm, |
|
115 | 115 | apply_to_children=apply_to_children,) |
|
116 | 116 | response = api_call(self.app, params) |
|
117 | 117 | if access_ok: |
|
118 | 118 | ret = { |
|
119 | 119 | 'msg': ( |
|
120 | 120 | 'Granted perm: `%s` (recursive:%s) for user group: `%s`' |
|
121 | 121 | ' in repo group: `%s`' % ( |
|
122 | 122 | perm, apply_to_children, user_group.users_group_name, |
|
123 | 123 | repo_group.name |
|
124 | 124 | ) |
|
125 | 125 | ), |
|
126 | 126 | 'success': True |
|
127 | 127 | } |
|
128 | 128 | expected = ret |
|
129 | 129 | try: |
|
130 | 130 | assert_ok(id_, expected, given=response.body) |
|
131 | 131 | finally: |
|
132 | 132 | RepoGroupModel().revoke_user_group_permission( |
|
133 | 133 | repo_group.group_id, user_group.users_group_id) |
|
134 | 134 | else: |
|
135 | expected = 'repository group `%s` does not exist' % ( | |
|
136 | repo_group.name,) | |
|
135 | expected = 'repository group `%s` does not exist' % (repo_group.name,) | |
|
137 | 136 | assert_error(id_, expected, given=response.body) |
|
138 | 137 | |
|
139 | 138 | def test_api_grant_user_group_permission_to_repo_group_wrong_permission( |
|
140 | 139 | self, user_util): |
|
141 | 140 | user_group = user_util.create_user_group() |
|
142 | 141 | repo_group = user_util.create_repo_group() |
|
143 | 142 | perm = 'haha.no.permission' |
|
144 | 143 | id_, params = build_data( |
|
145 | 144 | self.apikey, |
|
146 | 145 | 'grant_user_group_permission_to_repo_group', |
|
147 | 146 | repogroupid=repo_group.name, |
|
148 | 147 | usergroupid=user_group.users_group_name, |
|
149 | 148 | perm=perm) |
|
150 | 149 | response = api_call(self.app, params) |
|
151 | 150 | |
|
152 | expected = 'permission `%s` does not exist' % (perm,) | |
|
151 | expected = 'permission `%s` does not exist. Permission should start with prefix: `group.`' % (perm,) | |
|
153 | 152 | assert_error(id_, expected, given=response.body) |
|
154 | 153 | |
|
155 | 154 | @mock.patch.object(RepoGroupModel, 'grant_user_group_permission', crash) |
|
156 | 155 | def test_api_grant_user_group_permission_exception_when_adding_2( |
|
157 | 156 | self, user_util): |
|
158 | 157 | user_group = user_util.create_user_group() |
|
159 | 158 | repo_group = user_util.create_repo_group() |
|
160 | 159 | perm = 'group.read' |
|
161 | 160 | id_, params = build_data( |
|
162 | 161 | self.apikey, |
|
163 | 162 | 'grant_user_group_permission_to_repo_group', |
|
164 | 163 | repogroupid=repo_group.name, |
|
165 | 164 | usergroupid=user_group.users_group_name, |
|
166 | 165 | perm=perm) |
|
167 | 166 | response = api_call(self.app, params) |
|
168 | 167 | |
|
169 | 168 | expected = ( |
|
170 | 169 | 'failed to edit permission for user group: `%s`' |
|
171 | 170 | ' in repo group: `%s`' % ( |
|
172 | 171 | user_group.users_group_name, repo_group.name) |
|
173 | 172 | ) |
|
174 | 173 | assert_error(id_, expected, given=response.body) |
@@ -1,87 +1,87 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.model.repo import RepoModel |
|
25 | 25 | from rhodecode.api.tests.utils import ( |
|
26 | 26 | build_data, api_call, assert_error, assert_ok, crash) |
|
27 | 27 | |
|
28 | 28 | |
|
29 | 29 | @pytest.mark.usefixtures("testuser_api", "app") |
|
30 | 30 | class TestGrantUserPermission(object): |
|
31 | 31 | @pytest.mark.parametrize("name, perm", [ |
|
32 | 32 | ('none', 'repository.none'), |
|
33 | 33 | ('read', 'repository.read'), |
|
34 | 34 | ('write', 'repository.write'), |
|
35 | 35 | ('admin', 'repository.admin') |
|
36 | 36 | ]) |
|
37 | 37 | def test_api_grant_user_permission(self, name, perm, backend, user_util): |
|
38 | 38 | user = user_util.create_user() |
|
39 | 39 | id_, params = build_data( |
|
40 | 40 | self.apikey, |
|
41 | 41 | 'grant_user_permission', |
|
42 | 42 | repoid=backend.repo_name, |
|
43 | 43 | userid=user.username, |
|
44 | 44 | perm=perm) |
|
45 | 45 | response = api_call(self.app, params) |
|
46 | 46 | |
|
47 | 47 | ret = { |
|
48 | 48 | 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % ( |
|
49 | 49 | perm, user.username, backend.repo_name |
|
50 | 50 | ), |
|
51 | 51 | 'success': True |
|
52 | 52 | } |
|
53 | 53 | expected = ret |
|
54 | 54 | assert_ok(id_, expected, given=response.body) |
|
55 | 55 | |
|
56 | 56 | def test_api_grant_user_permission_wrong_permission( |
|
57 | 57 | self, backend, user_util): |
|
58 | 58 | user = user_util.create_user() |
|
59 | 59 | perm = 'haha.no.permission' |
|
60 | 60 | id_, params = build_data( |
|
61 | 61 | self.apikey, |
|
62 | 62 | 'grant_user_permission', |
|
63 | 63 | repoid=backend.repo_name, |
|
64 | 64 | userid=user.username, |
|
65 | 65 | perm=perm) |
|
66 | 66 | response = api_call(self.app, params) |
|
67 | 67 | |
|
68 | expected = 'permission `%s` does not exist' % (perm,) | |
|
68 | expected = 'permission `%s` does not exist.' % (perm,) | |
|
69 | 69 | assert_error(id_, expected, given=response.body) |
|
70 | 70 | |
|
71 | 71 | @mock.patch.object(RepoModel, 'grant_user_permission', crash) |
|
72 | 72 | def test_api_grant_user_permission_exception_when_adding( |
|
73 | 73 | self, backend, user_util): |
|
74 | 74 | user = user_util.create_user() |
|
75 | 75 | perm = 'repository.read' |
|
76 | 76 | id_, params = build_data( |
|
77 | 77 | self.apikey, |
|
78 | 78 | 'grant_user_permission', |
|
79 | 79 | repoid=backend.repo_name, |
|
80 | 80 | userid=user.username, |
|
81 | 81 | perm=perm) |
|
82 | 82 | response = api_call(self.app, params) |
|
83 | 83 | |
|
84 | 84 | expected = 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
|
85 | 85 | user.username, backend.repo_name |
|
86 | 86 | ) |
|
87 | 87 | assert_error(id_, expected, given=response.body) |
@@ -1,157 +1,157 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.model.user import UserModel |
|
25 | 25 | from rhodecode.model.repo_group import RepoGroupModel |
|
26 | 26 | from rhodecode.api.tests.utils import ( |
|
27 | 27 | build_data, api_call, assert_error, assert_ok, crash) |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | @pytest.mark.usefixtures("testuser_api", "app") |
|
31 | 31 | class TestGrantUserPermissionFromRepoGroup(object): |
|
32 | 32 | @pytest.mark.parametrize("name, perm, apply_to_children", [ |
|
33 | 33 | ('none', 'group.none', 'none'), |
|
34 | 34 | ('read', 'group.read', 'none'), |
|
35 | 35 | ('write', 'group.write', 'none'), |
|
36 | 36 | ('admin', 'group.admin', 'none'), |
|
37 | 37 | |
|
38 | 38 | ('none', 'group.none', 'all'), |
|
39 | 39 | ('read', 'group.read', 'all'), |
|
40 | 40 | ('write', 'group.write', 'all'), |
|
41 | 41 | ('admin', 'group.admin', 'all'), |
|
42 | 42 | |
|
43 | 43 | ('none', 'group.none', 'repos'), |
|
44 | 44 | ('read', 'group.read', 'repos'), |
|
45 | 45 | ('write', 'group.write', 'repos'), |
|
46 | 46 | ('admin', 'group.admin', 'repos'), |
|
47 | 47 | |
|
48 | 48 | ('none', 'group.none', 'groups'), |
|
49 | 49 | ('read', 'group.read', 'groups'), |
|
50 | 50 | ('write', 'group.write', 'groups'), |
|
51 | 51 | ('admin', 'group.admin', 'groups'), |
|
52 | 52 | ]) |
|
53 | 53 | def test_api_grant_user_permission_to_repo_group( |
|
54 | 54 | self, name, perm, apply_to_children, user_util): |
|
55 | 55 | user = user_util.create_user() |
|
56 | 56 | repo_group = user_util.create_repo_group() |
|
57 | 57 | id_, params = build_data( |
|
58 | 58 | self.apikey, 'grant_user_permission_to_repo_group', |
|
59 | 59 | repogroupid=repo_group.name, userid=user.username, |
|
60 | 60 | perm=perm, apply_to_children=apply_to_children) |
|
61 | 61 | response = api_call(self.app, params) |
|
62 | 62 | |
|
63 | 63 | ret = { |
|
64 | 64 | 'msg': ( |
|
65 | 65 | 'Granted perm: `%s` (recursive:%s) for user: `%s`' |
|
66 | 66 | ' in repo group: `%s`' % ( |
|
67 | 67 | perm, apply_to_children, user.username, repo_group.name |
|
68 | 68 | ) |
|
69 | 69 | ), |
|
70 | 70 | 'success': True |
|
71 | 71 | } |
|
72 | 72 | expected = ret |
|
73 | 73 | assert_ok(id_, expected, given=response.body) |
|
74 | 74 | |
|
75 | 75 | @pytest.mark.parametrize( |
|
76 | 76 | "name, perm, apply_to_children, grant_admin, access_ok", [ |
|
77 | 77 | ('none_fails', 'group.none', 'none', False, False), |
|
78 | 78 | ('read_fails', 'group.read', 'none', False, False), |
|
79 | 79 | ('write_fails', 'group.write', 'none', False, False), |
|
80 | 80 | ('admin_fails', 'group.admin', 'none', False, False), |
|
81 | 81 | |
|
82 | 82 | # with granted perms |
|
83 | 83 | ('none_ok', 'group.none', 'none', True, True), |
|
84 | 84 | ('read_ok', 'group.read', 'none', True, True), |
|
85 | 85 | ('write_ok', 'group.write', 'none', True, True), |
|
86 | 86 | ('admin_ok', 'group.admin', 'none', True, True), |
|
87 | 87 | ] |
|
88 | 88 | ) |
|
89 | 89 | def test_api_grant_user_permission_to_repo_group_by_regular_user( |
|
90 | 90 | self, name, perm, apply_to_children, grant_admin, access_ok, |
|
91 | 91 | user_util): |
|
92 | 92 | user = user_util.create_user() |
|
93 | 93 | repo_group = user_util.create_repo_group() |
|
94 | 94 | |
|
95 | 95 | if grant_admin: |
|
96 | 96 | test_user = UserModel().get_by_username(self.TEST_USER_LOGIN) |
|
97 | 97 | user_util.grant_user_permission_to_repo_group( |
|
98 | 98 | repo_group, test_user, 'group.admin') |
|
99 | 99 | |
|
100 | 100 | id_, params = build_data( |
|
101 | 101 | self.apikey_regular, 'grant_user_permission_to_repo_group', |
|
102 | 102 | repogroupid=repo_group.name, userid=user.username, |
|
103 | 103 | perm=perm, apply_to_children=apply_to_children) |
|
104 | 104 | response = api_call(self.app, params) |
|
105 | 105 | if access_ok: |
|
106 | 106 | ret = { |
|
107 | 107 | 'msg': ( |
|
108 | 108 | 'Granted perm: `%s` (recursive:%s) for user: `%s`' |
|
109 | 109 | ' in repo group: `%s`' % ( |
|
110 | 110 | perm, apply_to_children, user.username, repo_group.name |
|
111 | 111 | ) |
|
112 | 112 | ), |
|
113 | 113 | 'success': True |
|
114 | 114 | } |
|
115 | 115 | expected = ret |
|
116 | 116 | assert_ok(id_, expected, given=response.body) |
|
117 | 117 | else: |
|
118 | 118 | expected = 'repository group `%s` does not exist' % ( |
|
119 | 119 | repo_group.name, ) |
|
120 | 120 | assert_error(id_, expected, given=response.body) |
|
121 | 121 | |
|
122 | 122 | def test_api_grant_user_permission_to_repo_group_wrong_permission( |
|
123 | 123 | self, user_util): |
|
124 | 124 | user = user_util.create_user() |
|
125 | 125 | repo_group = user_util.create_repo_group() |
|
126 | 126 | perm = 'haha.no.permission' |
|
127 | 127 | id_, params = build_data( |
|
128 | 128 | self.apikey, |
|
129 | 129 | 'grant_user_permission_to_repo_group', |
|
130 | 130 | repogroupid=repo_group.name, |
|
131 | 131 | userid=user.username, |
|
132 | 132 | perm=perm) |
|
133 | 133 | response = api_call(self.app, params) |
|
134 | 134 | |
|
135 | expected = 'permission `%s` does not exist' % (perm,) | |
|
135 | expected = 'permission `%s` does not exist. Permission should start with prefix: `group.`' % (perm,) | |
|
136 | 136 | assert_error(id_, expected, given=response.body) |
|
137 | 137 | |
|
138 | 138 | @mock.patch.object(RepoGroupModel, 'grant_user_permission', crash) |
|
139 | 139 | def test_api_grant_user_permission_to_repo_group_exception_when_adding( |
|
140 | 140 | self, user_util): |
|
141 | 141 | user = user_util.create_user() |
|
142 | 142 | repo_group = user_util.create_repo_group() |
|
143 | 143 | perm = 'group.read' |
|
144 | 144 | id_, params = build_data( |
|
145 | 145 | self.apikey, |
|
146 | 146 | 'grant_user_permission_to_repo_group', |
|
147 | 147 | repogroupid=repo_group.name, |
|
148 | 148 | userid=user.username, |
|
149 | 149 | perm=perm) |
|
150 | 150 | response = api_call(self.app, params) |
|
151 | 151 | |
|
152 | 152 | expected = ( |
|
153 | 153 | 'failed to edit permission for user: `%s` in repo group: `%s`' % ( |
|
154 | 154 | user.username, repo_group.name |
|
155 | 155 | ) |
|
156 | 156 | ) |
|
157 | 157 | assert_error(id_, expected, given=response.body) |
@@ -1,156 +1,156 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.model.user import UserModel |
|
25 | 25 | from rhodecode.model.user_group import UserGroupModel |
|
26 | 26 | from rhodecode.api.tests.utils import ( |
|
27 | 27 | build_data, api_call, assert_error, assert_ok, crash) |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | @pytest.mark.usefixtures("testuser_api", "app") |
|
31 | 31 | class TestGrantUserPermissionFromUserGroup(object): |
|
32 | 32 | @pytest.mark.parametrize("name, perm", [ |
|
33 | 33 | ('none', 'usergroup.none'), |
|
34 | 34 | ('read', 'usergroup.read'), |
|
35 | 35 | ('write', 'usergroup.write'), |
|
36 | 36 | ('admin', 'usergroup.admin'), |
|
37 | 37 | |
|
38 | 38 | ('none', 'usergroup.none'), |
|
39 | 39 | ('read', 'usergroup.read'), |
|
40 | 40 | ('write', 'usergroup.write'), |
|
41 | 41 | ('admin', 'usergroup.admin'), |
|
42 | 42 | |
|
43 | 43 | ('none', 'usergroup.none'), |
|
44 | 44 | ('read', 'usergroup.read'), |
|
45 | 45 | ('write', 'usergroup.write'), |
|
46 | 46 | ('admin', 'usergroup.admin'), |
|
47 | 47 | |
|
48 | 48 | ('none', 'usergroup.none'), |
|
49 | 49 | ('read', 'usergroup.read'), |
|
50 | 50 | ('write', 'usergroup.write'), |
|
51 | 51 | ('admin', 'usergroup.admin'), |
|
52 | 52 | ]) |
|
53 | 53 | def test_api_grant_user_permission_to_user_group( |
|
54 | 54 | self, name, perm, user_util): |
|
55 | 55 | user = user_util.create_user() |
|
56 | 56 | group = user_util.create_user_group() |
|
57 | 57 | id_, params = build_data( |
|
58 | 58 | self.apikey, |
|
59 | 59 | 'grant_user_permission_to_user_group', |
|
60 | 60 | usergroupid=group.users_group_name, |
|
61 | 61 | userid=user.username, |
|
62 | 62 | perm=perm) |
|
63 | 63 | response = api_call(self.app, params) |
|
64 | 64 | |
|
65 | 65 | ret = { |
|
66 | 66 | 'msg': 'Granted perm: `%s` for user: `%s` in user group: `%s`' % ( |
|
67 | 67 | perm, user.username, group.users_group_name |
|
68 | 68 | ), |
|
69 | 69 | 'success': True |
|
70 | 70 | } |
|
71 | 71 | expected = ret |
|
72 | 72 | assert_ok(id_, expected, given=response.body) |
|
73 | 73 | |
|
74 | 74 | @pytest.mark.parametrize("name, perm, grant_admin, access_ok", [ |
|
75 | 75 | ('none_fails', 'usergroup.none', False, False), |
|
76 | 76 | ('read_fails', 'usergroup.read', False, False), |
|
77 | 77 | ('write_fails', 'usergroup.write', False, False), |
|
78 | 78 | ('admin_fails', 'usergroup.admin', False, False), |
|
79 | 79 | |
|
80 | 80 | # with granted perms |
|
81 | 81 | ('none_ok', 'usergroup.none', True, True), |
|
82 | 82 | ('read_ok', 'usergroup.read', True, True), |
|
83 | 83 | ('write_ok', 'usergroup.write', True, True), |
|
84 | 84 | ('admin_ok', 'usergroup.admin', True, True), |
|
85 | 85 | ]) |
|
86 | 86 | def test_api_grant_user_permission_to_user_group_by_regular_user( |
|
87 | 87 | self, name, perm, grant_admin, access_ok, user_util): |
|
88 | 88 | api_user = UserModel().get_by_username(self.TEST_USER_LOGIN) |
|
89 | 89 | user = user_util.create_user() |
|
90 | 90 | group = user_util.create_user_group() |
|
91 | 91 | # grant the user ability to at least read the group |
|
92 | 92 | permission = 'usergroup.admin' if grant_admin else 'usergroup.read' |
|
93 | 93 | user_util.grant_user_permission_to_user_group( |
|
94 | 94 | group, api_user, permission) |
|
95 | 95 | |
|
96 | 96 | id_, params = build_data( |
|
97 | 97 | self.apikey_regular, |
|
98 | 98 | 'grant_user_permission_to_user_group', |
|
99 | 99 | usergroupid=group.users_group_name, |
|
100 | 100 | userid=user.username, |
|
101 | 101 | perm=perm) |
|
102 | 102 | response = api_call(self.app, params) |
|
103 | 103 | |
|
104 | 104 | if access_ok: |
|
105 | 105 | ret = { |
|
106 | 106 | 'msg': ( |
|
107 | 107 | 'Granted perm: `%s` for user: `%s` in user group: `%s`' % ( |
|
108 | 108 | perm, user.username, group.users_group_name |
|
109 | 109 | ) |
|
110 | 110 | ), |
|
111 | 111 | 'success': True |
|
112 | 112 | } |
|
113 | 113 | expected = ret |
|
114 | 114 | assert_ok(id_, expected, given=response.body) |
|
115 | 115 | else: |
|
116 | 116 | expected = 'user group `%s` does not exist' % ( |
|
117 | 117 | group.users_group_name) |
|
118 | 118 | assert_error(id_, expected, given=response.body) |
|
119 | 119 | |
|
120 | 120 | def test_api_grant_user_permission_to_user_group_wrong_permission( |
|
121 | 121 | self, user_util): |
|
122 | 122 | user = user_util.create_user() |
|
123 | 123 | group = user_util.create_user_group() |
|
124 | 124 | perm = 'haha.no.permission' |
|
125 | 125 | id_, params = build_data( |
|
126 | 126 | self.apikey, |
|
127 | 127 | 'grant_user_permission_to_user_group', |
|
128 | 128 | usergroupid=group.users_group_name, |
|
129 | 129 | userid=user.username, |
|
130 | 130 | perm=perm) |
|
131 | 131 | response = api_call(self.app, params) |
|
132 | 132 | |
|
133 | expected = 'permission `%s` does not exist' % perm | |
|
133 | expected = 'permission `%s` does not exist. Permission should start with prefix: `usergroup.`' % perm | |
|
134 | 134 | assert_error(id_, expected, given=response.body) |
|
135 | 135 | |
|
136 | 136 | def test_api_grant_user_permission_to_user_group_exception_when_adding( |
|
137 | 137 | self, user_util): |
|
138 | 138 | user = user_util.create_user() |
|
139 | 139 | group = user_util.create_user_group() |
|
140 | 140 | |
|
141 | 141 | perm = 'usergroup.read' |
|
142 | 142 | id_, params = build_data( |
|
143 | 143 | self.apikey, |
|
144 | 144 | 'grant_user_permission_to_user_group', |
|
145 | 145 | usergroupid=group.users_group_name, |
|
146 | 146 | userid=user.username, |
|
147 | 147 | perm=perm) |
|
148 | 148 | with mock.patch.object(UserGroupModel, 'grant_user_permission', crash): |
|
149 | 149 | response = api_call(self.app, params) |
|
150 | 150 | |
|
151 | 151 | expected = ( |
|
152 | 152 | 'failed to edit permission for user: `%s` in user group: `%s`' % ( |
|
153 | 153 | user.username, group.users_group_name |
|
154 | 154 | ) |
|
155 | 155 | ) |
|
156 | 156 | assert_error(id_, expected, given=response.body) |
@@ -1,449 +1,453 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2014-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | """ |
|
22 | 22 | JSON RPC utils |
|
23 | 23 | """ |
|
24 | 24 | |
|
25 | 25 | import collections |
|
26 | 26 | import logging |
|
27 | 27 | |
|
28 | 28 | from rhodecode.api.exc import JSONRPCError |
|
29 | 29 | from rhodecode.lib.auth import ( |
|
30 | 30 | HasPermissionAnyApi, HasRepoPermissionAnyApi, HasRepoGroupPermissionAnyApi) |
|
31 | 31 | from rhodecode.lib.utils import safe_unicode |
|
32 | 32 | from rhodecode.lib.vcs.exceptions import RepositoryError |
|
33 | 33 | from rhodecode.lib.view_utils import get_commit_from_ref_name |
|
34 | 34 | from rhodecode.lib.utils2 import str2bool |
|
35 | 35 | |
|
36 | 36 | log = logging.getLogger(__name__) |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | class OAttr(object): |
|
40 | 40 | """ |
|
41 | 41 | Special Option that defines other attribute, and can default to them |
|
42 | 42 | |
|
43 | 43 | Example:: |
|
44 | 44 | |
|
45 | 45 | def test(apiuser, userid=Optional(OAttr('apiuser')): |
|
46 | 46 | user = Optional.extract(userid, evaluate_locals=local()) |
|
47 | 47 | #if we pass in userid, we get it, else it will default to apiuser |
|
48 | 48 | #attribute |
|
49 | 49 | """ |
|
50 | 50 | |
|
51 | 51 | def __init__(self, attr_name): |
|
52 | 52 | self.attr_name = attr_name |
|
53 | 53 | |
|
54 | 54 | def __repr__(self): |
|
55 | 55 | return '<OptionalAttr:%s>' % self.attr_name |
|
56 | 56 | |
|
57 | 57 | def __call__(self): |
|
58 | 58 | return self |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | class Optional(object): |
|
62 | 62 | """ |
|
63 | 63 | Defines an optional parameter:: |
|
64 | 64 | |
|
65 | 65 | param = param.getval() if isinstance(param, Optional) else param |
|
66 | 66 | param = param() if isinstance(param, Optional) else param |
|
67 | 67 | |
|
68 | 68 | is equivalent of:: |
|
69 | 69 | |
|
70 | 70 | param = Optional.extract(param) |
|
71 | 71 | |
|
72 | 72 | """ |
|
73 | 73 | |
|
74 | 74 | def __init__(self, type_): |
|
75 | 75 | self.type_ = type_ |
|
76 | 76 | |
|
77 | 77 | def __repr__(self): |
|
78 | 78 | return '<Optional:%s>' % self.type_.__repr__() |
|
79 | 79 | |
|
80 | 80 | def __call__(self): |
|
81 | 81 | return self.getval() |
|
82 | 82 | |
|
83 | 83 | def getval(self, evaluate_locals=None): |
|
84 | 84 | """ |
|
85 | 85 | returns value from this Optional instance |
|
86 | 86 | """ |
|
87 | 87 | if isinstance(self.type_, OAttr): |
|
88 | 88 | param_name = self.type_.attr_name |
|
89 | 89 | if evaluate_locals: |
|
90 | 90 | return evaluate_locals[param_name] |
|
91 | 91 | # use params name |
|
92 | 92 | return param_name |
|
93 | 93 | return self.type_ |
|
94 | 94 | |
|
95 | 95 | @classmethod |
|
96 | 96 | def extract(cls, val, evaluate_locals=None, binary=None): |
|
97 | 97 | """ |
|
98 | 98 | Extracts value from Optional() instance |
|
99 | 99 | |
|
100 | 100 | :param val: |
|
101 | 101 | :return: original value if it's not Optional instance else |
|
102 | 102 | value of instance |
|
103 | 103 | """ |
|
104 | 104 | if isinstance(val, cls): |
|
105 | 105 | val = val.getval(evaluate_locals) |
|
106 | 106 | |
|
107 | 107 | if binary: |
|
108 | 108 | val = str2bool(val) |
|
109 | 109 | |
|
110 | 110 | return val |
|
111 | 111 | |
|
112 | 112 | |
|
113 | 113 | def parse_args(cli_args, key_prefix=''): |
|
114 | 114 | from rhodecode.lib.utils2 import (escape_split) |
|
115 | 115 | kwargs = collections.defaultdict(dict) |
|
116 | 116 | for el in escape_split(cli_args, ','): |
|
117 | 117 | kv = escape_split(el, '=', 1) |
|
118 | 118 | if len(kv) == 2: |
|
119 | 119 | k, v = kv |
|
120 | 120 | kwargs[key_prefix + k] = v |
|
121 | 121 | return kwargs |
|
122 | 122 | |
|
123 | 123 | |
|
124 | 124 | def get_origin(obj): |
|
125 | 125 | """ |
|
126 | 126 | Get origin of permission from object. |
|
127 | 127 | |
|
128 | 128 | :param obj: |
|
129 | 129 | """ |
|
130 | 130 | origin = 'permission' |
|
131 | 131 | |
|
132 | 132 | if getattr(obj, 'owner_row', '') and getattr(obj, 'admin_row', ''): |
|
133 | 133 | # admin and owner case, maybe we should use dual string ? |
|
134 | 134 | origin = 'owner' |
|
135 | 135 | elif getattr(obj, 'owner_row', ''): |
|
136 | 136 | origin = 'owner' |
|
137 | 137 | elif getattr(obj, 'admin_row', ''): |
|
138 | 138 | origin = 'super-admin' |
|
139 | 139 | return origin |
|
140 | 140 | |
|
141 | 141 | |
|
142 | 142 | def store_update(updates, attr, name): |
|
143 | 143 | """ |
|
144 | 144 | Stores param in updates dict if it's not instance of Optional |
|
145 | 145 | allows easy updates of passed in params |
|
146 | 146 | """ |
|
147 | 147 | if not isinstance(attr, Optional): |
|
148 | 148 | updates[name] = attr |
|
149 | 149 | |
|
150 | 150 | |
|
151 | 151 | def has_superadmin_permission(apiuser): |
|
152 | 152 | """ |
|
153 | 153 | Return True if apiuser is admin or return False |
|
154 | 154 | |
|
155 | 155 | :param apiuser: |
|
156 | 156 | """ |
|
157 | 157 | if HasPermissionAnyApi('hg.admin')(user=apiuser): |
|
158 | 158 | return True |
|
159 | 159 | return False |
|
160 | 160 | |
|
161 | 161 | |
|
162 | 162 | def validate_repo_permissions(apiuser, repoid, repo, perms): |
|
163 | 163 | """ |
|
164 | 164 | Raise JsonRPCError if apiuser is not authorized or return True |
|
165 | 165 | |
|
166 | 166 | :param apiuser: |
|
167 | 167 | :param repoid: |
|
168 | 168 | :param repo: |
|
169 | 169 | :param perms: |
|
170 | 170 | """ |
|
171 | 171 | if not HasRepoPermissionAnyApi(*perms)( |
|
172 | 172 | user=apiuser, repo_name=repo.repo_name): |
|
173 | 173 | raise JSONRPCError( |
|
174 | 174 | 'repository `%s` does not exist' % repoid) |
|
175 | 175 | |
|
176 | 176 | return True |
|
177 | 177 | |
|
178 | 178 | |
|
179 | 179 | def validate_repo_group_permissions(apiuser, repogroupid, repo_group, perms): |
|
180 | 180 | """ |
|
181 | 181 | Raise JsonRPCError if apiuser is not authorized or return True |
|
182 | 182 | |
|
183 | 183 | :param apiuser: |
|
184 | 184 | :param repogroupid: just the id of repository group |
|
185 | 185 | :param repo_group: instance of repo_group |
|
186 | 186 | :param perms: |
|
187 | 187 | """ |
|
188 | 188 | if not HasRepoGroupPermissionAnyApi(*perms)( |
|
189 | 189 | user=apiuser, group_name=repo_group.group_name): |
|
190 | 190 | raise JSONRPCError( |
|
191 | 191 | 'repository group `%s` does not exist' % repogroupid) |
|
192 | 192 | |
|
193 | 193 | return True |
|
194 | 194 | |
|
195 | 195 | |
|
196 | 196 | def validate_set_owner_permissions(apiuser, owner): |
|
197 | 197 | if isinstance(owner, Optional): |
|
198 | 198 | owner = get_user_or_error(apiuser.user_id) |
|
199 | 199 | else: |
|
200 | 200 | if has_superadmin_permission(apiuser): |
|
201 | 201 | owner = get_user_or_error(owner) |
|
202 | 202 | else: |
|
203 | 203 | # forbid setting owner for non-admins |
|
204 | 204 | raise JSONRPCError( |
|
205 | 205 | 'Only RhodeCode super-admin can specify `owner` param') |
|
206 | 206 | return owner |
|
207 | 207 | |
|
208 | 208 | |
|
209 | 209 | def get_user_or_error(userid): |
|
210 | 210 | """ |
|
211 | 211 | Get user by id or name or return JsonRPCError if not found |
|
212 | 212 | |
|
213 | 213 | :param userid: |
|
214 | 214 | """ |
|
215 | 215 | from rhodecode.model.user import UserModel |
|
216 | 216 | user_model = UserModel() |
|
217 | 217 | |
|
218 | 218 | if isinstance(userid, (int, long)): |
|
219 | 219 | try: |
|
220 | 220 | user = user_model.get_user(userid) |
|
221 | 221 | except ValueError: |
|
222 | 222 | user = None |
|
223 | 223 | else: |
|
224 | 224 | user = user_model.get_by_username(userid) |
|
225 | 225 | |
|
226 | 226 | if user is None: |
|
227 | 227 | raise JSONRPCError( |
|
228 | 228 | 'user `%s` does not exist' % (userid,)) |
|
229 | 229 | return user |
|
230 | 230 | |
|
231 | 231 | |
|
232 | 232 | def get_repo_or_error(repoid): |
|
233 | 233 | """ |
|
234 | 234 | Get repo by id or name or return JsonRPCError if not found |
|
235 | 235 | |
|
236 | 236 | :param repoid: |
|
237 | 237 | """ |
|
238 | 238 | from rhodecode.model.repo import RepoModel |
|
239 | 239 | repo_model = RepoModel() |
|
240 | 240 | |
|
241 | 241 | if isinstance(repoid, (int, long)): |
|
242 | 242 | try: |
|
243 | 243 | repo = repo_model.get_repo(repoid) |
|
244 | 244 | except ValueError: |
|
245 | 245 | repo = None |
|
246 | 246 | else: |
|
247 | 247 | repo = repo_model.get_by_repo_name(repoid) |
|
248 | 248 | |
|
249 | 249 | if repo is None: |
|
250 | 250 | raise JSONRPCError( |
|
251 | 251 | 'repository `%s` does not exist' % (repoid,)) |
|
252 | 252 | return repo |
|
253 | 253 | |
|
254 | 254 | |
|
255 | 255 | def get_repo_group_or_error(repogroupid): |
|
256 | 256 | """ |
|
257 | 257 | Get repo group by id or name or return JsonRPCError if not found |
|
258 | 258 | |
|
259 | 259 | :param repogroupid: |
|
260 | 260 | """ |
|
261 | 261 | from rhodecode.model.repo_group import RepoGroupModel |
|
262 | 262 | repo_group_model = RepoGroupModel() |
|
263 | 263 | |
|
264 | 264 | if isinstance(repogroupid, (int, long)): |
|
265 | 265 | try: |
|
266 | 266 | repo_group = repo_group_model._get_repo_group(repogroupid) |
|
267 | 267 | except ValueError: |
|
268 | 268 | repo_group = None |
|
269 | 269 | else: |
|
270 | 270 | repo_group = repo_group_model.get_by_group_name(repogroupid) |
|
271 | 271 | |
|
272 | 272 | if repo_group is None: |
|
273 | 273 | raise JSONRPCError( |
|
274 | 274 | 'repository group `%s` does not exist' % (repogroupid,)) |
|
275 | 275 | return repo_group |
|
276 | 276 | |
|
277 | 277 | |
|
278 | 278 | def get_user_group_or_error(usergroupid): |
|
279 | 279 | """ |
|
280 | 280 | Get user group by id or name or return JsonRPCError if not found |
|
281 | 281 | |
|
282 | 282 | :param usergroupid: |
|
283 | 283 | """ |
|
284 | 284 | from rhodecode.model.user_group import UserGroupModel |
|
285 | 285 | user_group_model = UserGroupModel() |
|
286 | 286 | |
|
287 | 287 | if isinstance(usergroupid, (int, long)): |
|
288 | 288 | try: |
|
289 | 289 | user_group = user_group_model.get_group(usergroupid) |
|
290 | 290 | except ValueError: |
|
291 | 291 | user_group = None |
|
292 | 292 | else: |
|
293 | 293 | user_group = user_group_model.get_by_name(usergroupid) |
|
294 | 294 | |
|
295 | 295 | if user_group is None: |
|
296 | 296 | raise JSONRPCError( |
|
297 | 297 | 'user group `%s` does not exist' % (usergroupid,)) |
|
298 | 298 | return user_group |
|
299 | 299 | |
|
300 | 300 | |
|
301 | 301 | def get_perm_or_error(permid, prefix=None): |
|
302 | 302 | """ |
|
303 | 303 | Get permission by id or name or return JsonRPCError if not found |
|
304 | 304 | |
|
305 | 305 | :param permid: |
|
306 | 306 | """ |
|
307 | 307 | from rhodecode.model.permission import PermissionModel |
|
308 | 308 | |
|
309 | 309 | perm = PermissionModel.cls.get_by_key(permid) |
|
310 | 310 | if perm is None: |
|
311 |
|
|
|
311 | msg = 'permission `{}` does not exist.'.format(permid) | |
|
312 | if prefix: | |
|
313 | msg += ' Permission should start with prefix: `{}`'.format(prefix) | |
|
314 | raise JSONRPCError(msg) | |
|
315 | ||
|
312 | 316 | if prefix: |
|
313 | 317 | if not perm.permission_name.startswith(prefix): |
|
314 | 318 | raise JSONRPCError('permission `%s` is invalid, ' |
|
315 | 319 | 'should start with %s' % (permid, prefix)) |
|
316 | 320 | return perm |
|
317 | 321 | |
|
318 | 322 | |
|
319 | 323 | def get_gist_or_error(gistid): |
|
320 | 324 | """ |
|
321 | 325 | Get gist by id or gist_access_id or return JsonRPCError if not found |
|
322 | 326 | |
|
323 | 327 | :param gistid: |
|
324 | 328 | """ |
|
325 | 329 | from rhodecode.model.gist import GistModel |
|
326 | 330 | |
|
327 | 331 | gist = GistModel.cls.get_by_access_id(gistid) |
|
328 | 332 | if gist is None: |
|
329 | 333 | raise JSONRPCError('gist `%s` does not exist' % (gistid,)) |
|
330 | 334 | return gist |
|
331 | 335 | |
|
332 | 336 | |
|
333 | 337 | def get_pull_request_or_error(pullrequestid): |
|
334 | 338 | """ |
|
335 | 339 | Get pull request by id or return JsonRPCError if not found |
|
336 | 340 | |
|
337 | 341 | :param pullrequestid: |
|
338 | 342 | """ |
|
339 | 343 | from rhodecode.model.pull_request import PullRequestModel |
|
340 | 344 | |
|
341 | 345 | try: |
|
342 | 346 | pull_request = PullRequestModel().get(int(pullrequestid)) |
|
343 | 347 | except ValueError: |
|
344 | 348 | raise JSONRPCError('pullrequestid must be an integer') |
|
345 | 349 | if not pull_request: |
|
346 | 350 | raise JSONRPCError('pull request `%s` does not exist' % ( |
|
347 | 351 | pullrequestid,)) |
|
348 | 352 | return pull_request |
|
349 | 353 | |
|
350 | 354 | |
|
351 | 355 | def build_commit_data(commit, detail_level): |
|
352 | 356 | parsed_diff = [] |
|
353 | 357 | if detail_level == 'extended': |
|
354 | for f in commit.added: | |
|
355 |
parsed_diff.append(_get_commit_dict(filename=f |
|
|
356 | for f in commit.changed: | |
|
357 |
parsed_diff.append(_get_commit_dict(filename=f |
|
|
358 | for f in commit.removed: | |
|
359 |
parsed_diff.append(_get_commit_dict(filename=f |
|
|
358 | for f_path in commit.added_paths: | |
|
359 | parsed_diff.append(_get_commit_dict(filename=f_path, op='A')) | |
|
360 | for f_path in commit.changed_paths: | |
|
361 | parsed_diff.append(_get_commit_dict(filename=f_path, op='M')) | |
|
362 | for f_path in commit.removed_paths: | |
|
363 | parsed_diff.append(_get_commit_dict(filename=f_path, op='D')) | |
|
360 | 364 | |
|
361 | 365 | elif detail_level == 'full': |
|
362 | 366 | from rhodecode.lib.diffs import DiffProcessor |
|
363 | 367 | diff_processor = DiffProcessor(commit.diff()) |
|
364 | 368 | for dp in diff_processor.prepare(): |
|
365 | 369 | del dp['stats']['ops'] |
|
366 | 370 | _stats = dp['stats'] |
|
367 | 371 | parsed_diff.append(_get_commit_dict( |
|
368 | 372 | filename=dp['filename'], op=dp['operation'], |
|
369 | 373 | new_revision=dp['new_revision'], |
|
370 | 374 | old_revision=dp['old_revision'], |
|
371 | 375 | raw_diff=dp['raw_diff'], stats=_stats)) |
|
372 | 376 | |
|
373 | 377 | return parsed_diff |
|
374 | 378 | |
|
375 | 379 | |
|
376 | 380 | def get_commit_or_error(ref, repo): |
|
377 | 381 | try: |
|
378 | 382 | ref_type, _, ref_hash = ref.split(':') |
|
379 | 383 | except ValueError: |
|
380 | 384 | raise JSONRPCError( |
|
381 | 385 | 'Ref `{ref}` given in a wrong format. Please check the API' |
|
382 | 386 | ' documentation for more details'.format(ref=ref)) |
|
383 | 387 | try: |
|
384 | 388 | # TODO: dan: refactor this to use repo.scm_instance().get_commit() |
|
385 | 389 | # once get_commit supports ref_types |
|
386 | 390 | return get_commit_from_ref_name(repo, ref_hash) |
|
387 | 391 | except RepositoryError: |
|
388 | 392 | raise JSONRPCError('Ref `{ref}` does not exist'.format(ref=ref)) |
|
389 | 393 | |
|
390 | 394 | |
|
391 | 395 | def _get_ref_hash(repo, type_, name): |
|
392 | 396 | vcs_repo = repo.scm_instance() |
|
393 | 397 | if type_ in ['branch'] and vcs_repo.alias in ('hg', 'git'): |
|
394 | 398 | return vcs_repo.branches[name] |
|
395 | 399 | elif type_ in ['bookmark', 'book'] and vcs_repo.alias == 'hg': |
|
396 | 400 | return vcs_repo.bookmarks[name] |
|
397 | 401 | else: |
|
398 | 402 | raise ValueError() |
|
399 | 403 | |
|
400 | 404 | |
|
401 | 405 | def resolve_ref_or_error(ref, repo, allowed_ref_types=None): |
|
402 | 406 | allowed_ref_types = allowed_ref_types or ['bookmark', 'book', 'tag', 'branch'] |
|
403 | 407 | |
|
404 | 408 | def _parse_ref(type_, name, hash_=None): |
|
405 | 409 | return type_, name, hash_ |
|
406 | 410 | |
|
407 | 411 | try: |
|
408 | 412 | ref_type, ref_name, ref_hash = _parse_ref(*ref.split(':')) |
|
409 | 413 | except TypeError: |
|
410 | 414 | raise JSONRPCError( |
|
411 | 415 | 'Ref `{ref}` given in a wrong format. Please check the API' |
|
412 | 416 | ' documentation for more details'.format(ref=ref)) |
|
413 | 417 | |
|
414 | 418 | if ref_type not in allowed_ref_types: |
|
415 | 419 | raise JSONRPCError( |
|
416 | 420 | 'Ref `{ref}` type is not allowed. ' |
|
417 | 421 | 'Only:{allowed_refs} are possible.'.format( |
|
418 | 422 | ref=ref, allowed_refs=allowed_ref_types)) |
|
419 | 423 | |
|
420 | 424 | try: |
|
421 | 425 | ref_hash = ref_hash or _get_ref_hash(repo, ref_type, ref_name) |
|
422 | 426 | except (KeyError, ValueError): |
|
423 | 427 | raise JSONRPCError( |
|
424 | 428 | 'The specified value:{type}:`{name}` does not exist, or is not allowed.'.format( |
|
425 | 429 | type=ref_type, name=ref_name)) |
|
426 | 430 | |
|
427 | 431 | return ':'.join([ref_type, ref_name, ref_hash]) |
|
428 | 432 | |
|
429 | 433 | |
|
430 | 434 | def _get_commit_dict( |
|
431 | 435 | filename, op, new_revision=None, old_revision=None, |
|
432 | 436 | raw_diff=None, stats=None): |
|
433 | 437 | if stats is None: |
|
434 | 438 | stats = { |
|
435 | 439 | "added": None, |
|
436 | 440 | "binary": None, |
|
437 | 441 | "deleted": None |
|
438 | 442 | } |
|
439 | 443 | return { |
|
440 | 444 | "filename": safe_unicode(filename), |
|
441 | 445 | "op": op, |
|
442 | 446 | |
|
443 | 447 | # extra details |
|
444 | 448 | "new_revision": new_revision, |
|
445 | 449 | "old_revision": old_revision, |
|
446 | 450 | |
|
447 | 451 | "raw_diff": raw_diff, |
|
448 | 452 | "stats": stats |
|
449 | 453 | } |
@@ -1,1011 +1,1016 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | import logging |
|
23 | 23 | |
|
24 | 24 | from rhodecode import events |
|
25 | 25 | from rhodecode.api import jsonrpc_method, JSONRPCError, JSONRPCValidationError |
|
26 | 26 | from rhodecode.api.utils import ( |
|
27 | 27 | has_superadmin_permission, Optional, OAttr, get_repo_or_error, |
|
28 | 28 | get_pull_request_or_error, get_commit_or_error, get_user_or_error, |
|
29 | 29 | validate_repo_permissions, resolve_ref_or_error, validate_set_owner_permissions) |
|
30 | 30 | from rhodecode.lib.auth import (HasRepoPermissionAnyApi) |
|
31 | 31 | from rhodecode.lib.base import vcs_operation_context |
|
32 | 32 | from rhodecode.lib.utils2 import str2bool |
|
33 | 33 | from rhodecode.model.changeset_status import ChangesetStatusModel |
|
34 | 34 | from rhodecode.model.comment import CommentsModel |
|
35 | 35 | from rhodecode.model.db import Session, ChangesetStatus, ChangesetComment, PullRequest |
|
36 | 36 | from rhodecode.model.pull_request import PullRequestModel, MergeCheck |
|
37 | 37 | from rhodecode.model.settings import SettingsModel |
|
38 | 38 | from rhodecode.model.validation_schema import Invalid |
|
39 | 39 | from rhodecode.model.validation_schema.schemas.reviewer_schema import( |
|
40 | 40 | ReviewerListSchema) |
|
41 | 41 | |
|
42 | 42 | log = logging.getLogger(__name__) |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | @jsonrpc_method() |
|
46 | 46 | def get_pull_request(request, apiuser, pullrequestid, repoid=Optional(None), |
|
47 | 47 | merge_state=Optional(False)): |
|
48 | 48 | """ |
|
49 | 49 | Get a pull request based on the given ID. |
|
50 | 50 | |
|
51 | 51 | :param apiuser: This is filled automatically from the |authtoken|. |
|
52 | 52 | :type apiuser: AuthUser |
|
53 | 53 | :param repoid: Optional, repository name or repository ID from where |
|
54 | 54 | the pull request was opened. |
|
55 | 55 | :type repoid: str or int |
|
56 | 56 | :param pullrequestid: ID of the requested pull request. |
|
57 | 57 | :type pullrequestid: int |
|
58 | 58 | :param merge_state: Optional calculate merge state for each repository. |
|
59 | 59 | This could result in longer time to fetch the data |
|
60 | 60 | :type merge_state: bool |
|
61 | 61 | |
|
62 | 62 | Example output: |
|
63 | 63 | |
|
64 | 64 | .. code-block:: bash |
|
65 | 65 | |
|
66 | 66 | "id": <id_given_in_input>, |
|
67 | 67 | "result": |
|
68 | 68 | { |
|
69 | 69 | "pull_request_id": "<pull_request_id>", |
|
70 | 70 | "url": "<url>", |
|
71 | 71 | "title": "<title>", |
|
72 | 72 | "description": "<description>", |
|
73 | 73 | "status" : "<status>", |
|
74 | 74 | "created_on": "<date_time_created>", |
|
75 | 75 | "updated_on": "<date_time_updated>", |
|
76 | "versions": "<number_or_versions_of_pr>", | |
|
76 | 77 | "commit_ids": [ |
|
77 | 78 | ... |
|
78 | 79 | "<commit_id>", |
|
79 | 80 | "<commit_id>", |
|
80 | 81 | ... |
|
81 | 82 | ], |
|
82 | 83 | "review_status": "<review_status>", |
|
83 | 84 | "mergeable": { |
|
84 | 85 | "status": "<bool>", |
|
85 | 86 | "message": "<message>", |
|
86 | 87 | }, |
|
87 | 88 | "source": { |
|
88 | 89 | "clone_url": "<clone_url>", |
|
89 | 90 | "repository": "<repository_name>", |
|
90 | 91 | "reference": |
|
91 | 92 | { |
|
92 | 93 | "name": "<name>", |
|
93 | 94 | "type": "<type>", |
|
94 | 95 | "commit_id": "<commit_id>", |
|
95 | 96 | } |
|
96 | 97 | }, |
|
97 | 98 | "target": { |
|
98 | 99 | "clone_url": "<clone_url>", |
|
99 | 100 | "repository": "<repository_name>", |
|
100 | 101 | "reference": |
|
101 | 102 | { |
|
102 | 103 | "name": "<name>", |
|
103 | 104 | "type": "<type>", |
|
104 | 105 | "commit_id": "<commit_id>", |
|
105 | 106 | } |
|
106 | 107 | }, |
|
107 | 108 | "merge": { |
|
108 | 109 | "clone_url": "<clone_url>", |
|
109 | 110 | "reference": |
|
110 | 111 | { |
|
111 | 112 | "name": "<name>", |
|
112 | 113 | "type": "<type>", |
|
113 | 114 | "commit_id": "<commit_id>", |
|
114 | 115 | } |
|
115 | 116 | }, |
|
116 | 117 | "author": <user_obj>, |
|
117 | 118 | "reviewers": [ |
|
118 | 119 | ... |
|
119 | 120 | { |
|
120 | 121 | "user": "<user_obj>", |
|
121 | 122 | "review_status": "<review_status>", |
|
122 | 123 | } |
|
123 | 124 | ... |
|
124 | 125 | ] |
|
125 | 126 | }, |
|
126 | 127 | "error": null |
|
127 | 128 | """ |
|
128 | 129 | |
|
129 | 130 | pull_request = get_pull_request_or_error(pullrequestid) |
|
130 | 131 | if Optional.extract(repoid): |
|
131 | 132 | repo = get_repo_or_error(repoid) |
|
132 | 133 | else: |
|
133 | 134 | repo = pull_request.target_repo |
|
134 | 135 | |
|
135 | 136 | if not PullRequestModel().check_user_read(pull_request, apiuser, api=True): |
|
136 | 137 | raise JSONRPCError('repository `%s` or pull request `%s` ' |
|
137 | 138 | 'does not exist' % (repoid, pullrequestid)) |
|
138 | 139 | |
|
139 | 140 | # NOTE(marcink): only calculate and return merge state if the pr state is 'created' |
|
140 | 141 | # otherwise we can lock the repo on calculation of merge state while update/merge |
|
141 | 142 | # is happening. |
|
142 | 143 | pr_created = pull_request.pull_request_state == pull_request.STATE_CREATED |
|
143 | 144 | merge_state = Optional.extract(merge_state, binary=True) and pr_created |
|
144 | 145 | data = pull_request.get_api_data(with_merge_state=merge_state) |
|
145 | 146 | return data |
|
146 | 147 | |
|
147 | 148 | |
|
148 | 149 | @jsonrpc_method() |
|
149 | 150 | def get_pull_requests(request, apiuser, repoid, status=Optional('new'), |
|
150 | 151 | merge_state=Optional(False)): |
|
151 | 152 | """ |
|
152 | 153 | Get all pull requests from the repository specified in `repoid`. |
|
153 | 154 | |
|
154 | 155 | :param apiuser: This is filled automatically from the |authtoken|. |
|
155 | 156 | :type apiuser: AuthUser |
|
156 | 157 | :param repoid: Optional repository name or repository ID. |
|
157 | 158 | :type repoid: str or int |
|
158 | 159 | :param status: Only return pull requests with the specified status. |
|
159 | 160 | Valid options are. |
|
160 | 161 | * ``new`` (default) |
|
161 | 162 | * ``open`` |
|
162 | 163 | * ``closed`` |
|
163 | 164 | :type status: str |
|
164 | 165 | :param merge_state: Optional calculate merge state for each repository. |
|
165 | 166 | This could result in longer time to fetch the data |
|
166 | 167 | :type merge_state: bool |
|
167 | 168 | |
|
168 | 169 | Example output: |
|
169 | 170 | |
|
170 | 171 | .. code-block:: bash |
|
171 | 172 | |
|
172 | 173 | "id": <id_given_in_input>, |
|
173 | 174 | "result": |
|
174 | 175 | [ |
|
175 | 176 | ... |
|
176 | 177 | { |
|
177 | 178 | "pull_request_id": "<pull_request_id>", |
|
178 | 179 | "url": "<url>", |
|
179 | 180 | "title" : "<title>", |
|
180 | 181 | "description": "<description>", |
|
181 | 182 | "status": "<status>", |
|
182 | 183 | "created_on": "<date_time_created>", |
|
183 | 184 | "updated_on": "<date_time_updated>", |
|
184 | 185 | "commit_ids": [ |
|
185 | 186 | ... |
|
186 | 187 | "<commit_id>", |
|
187 | 188 | "<commit_id>", |
|
188 | 189 | ... |
|
189 | 190 | ], |
|
190 | 191 | "review_status": "<review_status>", |
|
191 | 192 | "mergeable": { |
|
192 | 193 | "status": "<bool>", |
|
193 | 194 | "message: "<message>", |
|
194 | 195 | }, |
|
195 | 196 | "source": { |
|
196 | 197 | "clone_url": "<clone_url>", |
|
197 | 198 | "reference": |
|
198 | 199 | { |
|
199 | 200 | "name": "<name>", |
|
200 | 201 | "type": "<type>", |
|
201 | 202 | "commit_id": "<commit_id>", |
|
202 | 203 | } |
|
203 | 204 | }, |
|
204 | 205 | "target": { |
|
205 | 206 | "clone_url": "<clone_url>", |
|
206 | 207 | "reference": |
|
207 | 208 | { |
|
208 | 209 | "name": "<name>", |
|
209 | 210 | "type": "<type>", |
|
210 | 211 | "commit_id": "<commit_id>", |
|
211 | 212 | } |
|
212 | 213 | }, |
|
213 | 214 | "merge": { |
|
214 | 215 | "clone_url": "<clone_url>", |
|
215 | 216 | "reference": |
|
216 | 217 | { |
|
217 | 218 | "name": "<name>", |
|
218 | 219 | "type": "<type>", |
|
219 | 220 | "commit_id": "<commit_id>", |
|
220 | 221 | } |
|
221 | 222 | }, |
|
222 | 223 | "author": <user_obj>, |
|
223 | 224 | "reviewers": [ |
|
224 | 225 | ... |
|
225 | 226 | { |
|
226 | 227 | "user": "<user_obj>", |
|
227 | 228 | "review_status": "<review_status>", |
|
228 | 229 | } |
|
229 | 230 | ... |
|
230 | 231 | ] |
|
231 | 232 | } |
|
232 | 233 | ... |
|
233 | 234 | ], |
|
234 | 235 | "error": null |
|
235 | 236 | |
|
236 | 237 | """ |
|
237 | 238 | repo = get_repo_or_error(repoid) |
|
238 | 239 | if not has_superadmin_permission(apiuser): |
|
239 | 240 | _perms = ( |
|
240 | 241 | 'repository.admin', 'repository.write', 'repository.read',) |
|
241 | 242 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
242 | 243 | |
|
243 | 244 | status = Optional.extract(status) |
|
244 | 245 | merge_state = Optional.extract(merge_state, binary=True) |
|
245 | 246 | pull_requests = PullRequestModel().get_all(repo, statuses=[status], |
|
246 | 247 | order_by='id', order_dir='desc') |
|
247 | 248 | data = [pr.get_api_data(with_merge_state=merge_state) for pr in pull_requests] |
|
248 | 249 | return data |
|
249 | 250 | |
|
250 | 251 | |
|
251 | 252 | @jsonrpc_method() |
|
252 | 253 | def merge_pull_request( |
|
253 | 254 | request, apiuser, pullrequestid, repoid=Optional(None), |
|
254 | 255 | userid=Optional(OAttr('apiuser'))): |
|
255 | 256 | """ |
|
256 | 257 | Merge the pull request specified by `pullrequestid` into its target |
|
257 | 258 | repository. |
|
258 | 259 | |
|
259 | 260 | :param apiuser: This is filled automatically from the |authtoken|. |
|
260 | 261 | :type apiuser: AuthUser |
|
261 | 262 | :param repoid: Optional, repository name or repository ID of the |
|
262 | 263 | target repository to which the |pr| is to be merged. |
|
263 | 264 | :type repoid: str or int |
|
264 | 265 | :param pullrequestid: ID of the pull request which shall be merged. |
|
265 | 266 | :type pullrequestid: int |
|
266 | 267 | :param userid: Merge the pull request as this user. |
|
267 | 268 | :type userid: Optional(str or int) |
|
268 | 269 | |
|
269 | 270 | Example output: |
|
270 | 271 | |
|
271 | 272 | .. code-block:: bash |
|
272 | 273 | |
|
273 | 274 | "id": <id_given_in_input>, |
|
274 | 275 | "result": { |
|
275 | 276 | "executed": "<bool>", |
|
276 | 277 | "failure_reason": "<int>", |
|
277 | 278 | "merge_status_message": "<str>", |
|
278 | 279 | "merge_commit_id": "<merge_commit_id>", |
|
279 | 280 | "possible": "<bool>", |
|
280 | 281 | "merge_ref": { |
|
281 | 282 | "commit_id": "<commit_id>", |
|
282 | 283 | "type": "<type>", |
|
283 | 284 | "name": "<name>" |
|
284 | 285 | } |
|
285 | 286 | }, |
|
286 | 287 | "error": null |
|
287 | 288 | """ |
|
288 | 289 | pull_request = get_pull_request_or_error(pullrequestid) |
|
289 | 290 | if Optional.extract(repoid): |
|
290 | 291 | repo = get_repo_or_error(repoid) |
|
291 | 292 | else: |
|
292 | 293 | repo = pull_request.target_repo |
|
293 | 294 | auth_user = apiuser |
|
294 | 295 | if not isinstance(userid, Optional): |
|
295 | 296 | if (has_superadmin_permission(apiuser) or |
|
296 | 297 | HasRepoPermissionAnyApi('repository.admin')( |
|
297 | 298 | user=apiuser, repo_name=repo.repo_name)): |
|
298 | 299 | apiuser = get_user_or_error(userid) |
|
299 | 300 | auth_user = apiuser.AuthUser() |
|
300 | 301 | else: |
|
301 | 302 | raise JSONRPCError('userid is not the same as your user') |
|
302 | 303 | |
|
303 | 304 | if pull_request.pull_request_state != PullRequest.STATE_CREATED: |
|
304 | 305 | raise JSONRPCError( |
|
305 | 306 | 'Operation forbidden because pull request is in state {}, ' |
|
306 | 307 | 'only state {} is allowed.'.format( |
|
307 | 308 | pull_request.pull_request_state, PullRequest.STATE_CREATED)) |
|
308 | 309 | |
|
309 | 310 | with pull_request.set_state(PullRequest.STATE_UPDATING): |
|
310 | 311 | check = MergeCheck.validate(pull_request, auth_user=auth_user, |
|
311 | 312 | translator=request.translate) |
|
312 | 313 | merge_possible = not check.failed |
|
313 | 314 | |
|
314 | 315 | if not merge_possible: |
|
315 | 316 | error_messages = [] |
|
316 | 317 | for err_type, error_msg in check.errors: |
|
317 | 318 | error_msg = request.translate(error_msg) |
|
318 | 319 | error_messages.append(error_msg) |
|
319 | 320 | |
|
320 | 321 | reasons = ','.join(error_messages) |
|
321 | 322 | raise JSONRPCError( |
|
322 | 323 | 'merge not possible for following reasons: {}'.format(reasons)) |
|
323 | 324 | |
|
324 | 325 | target_repo = pull_request.target_repo |
|
325 | 326 | extras = vcs_operation_context( |
|
326 | 327 | request.environ, repo_name=target_repo.repo_name, |
|
327 | 328 | username=auth_user.username, action='push', |
|
328 | 329 | scm=target_repo.repo_type) |
|
329 | 330 | with pull_request.set_state(PullRequest.STATE_UPDATING): |
|
330 | 331 | merge_response = PullRequestModel().merge_repo( |
|
331 | 332 | pull_request, apiuser, extras=extras) |
|
332 | 333 | if merge_response.executed: |
|
333 | 334 | PullRequestModel().close_pull_request(pull_request.pull_request_id, auth_user) |
|
334 | 335 | |
|
335 | 336 | Session().commit() |
|
336 | 337 | |
|
337 | 338 | # In previous versions the merge response directly contained the merge |
|
338 | 339 | # commit id. It is now contained in the merge reference object. To be |
|
339 | 340 | # backwards compatible we have to extract it again. |
|
340 | 341 | merge_response = merge_response.asdict() |
|
341 | 342 | merge_response['merge_commit_id'] = merge_response['merge_ref'].commit_id |
|
342 | 343 | |
|
343 | 344 | return merge_response |
|
344 | 345 | |
|
345 | 346 | |
|
346 | 347 | @jsonrpc_method() |
|
347 | 348 | def get_pull_request_comments( |
|
348 | 349 | request, apiuser, pullrequestid, repoid=Optional(None)): |
|
349 | 350 | """ |
|
350 | 351 | Get all comments of pull request specified with the `pullrequestid` |
|
351 | 352 | |
|
352 | 353 | :param apiuser: This is filled automatically from the |authtoken|. |
|
353 | 354 | :type apiuser: AuthUser |
|
354 | 355 | :param repoid: Optional repository name or repository ID. |
|
355 | 356 | :type repoid: str or int |
|
356 | 357 | :param pullrequestid: The pull request ID. |
|
357 | 358 | :type pullrequestid: int |
|
358 | 359 | |
|
359 | 360 | Example output: |
|
360 | 361 | |
|
361 | 362 | .. code-block:: bash |
|
362 | 363 | |
|
363 | 364 | id : <id_given_in_input> |
|
364 | 365 | result : [ |
|
365 | 366 | { |
|
366 | 367 | "comment_author": { |
|
367 | 368 | "active": true, |
|
368 | 369 | "full_name_or_username": "Tom Gore", |
|
369 | 370 | "username": "admin" |
|
370 | 371 | }, |
|
371 | 372 | "comment_created_on": "2017-01-02T18:43:45.533", |
|
372 | 373 | "comment_f_path": null, |
|
373 | 374 | "comment_id": 25, |
|
374 | 375 | "comment_lineno": null, |
|
375 | 376 | "comment_status": { |
|
376 | 377 | "status": "under_review", |
|
377 | 378 | "status_lbl": "Under Review" |
|
378 | 379 | }, |
|
379 | 380 | "comment_text": "Example text", |
|
380 | 381 | "comment_type": null, |
|
381 | 382 | "pull_request_version": null |
|
382 | 383 | } |
|
383 | 384 | ], |
|
384 | 385 | error : null |
|
385 | 386 | """ |
|
386 | 387 | |
|
387 | 388 | pull_request = get_pull_request_or_error(pullrequestid) |
|
388 | 389 | if Optional.extract(repoid): |
|
389 | 390 | repo = get_repo_or_error(repoid) |
|
390 | 391 | else: |
|
391 | 392 | repo = pull_request.target_repo |
|
392 | 393 | |
|
393 | 394 | if not PullRequestModel().check_user_read( |
|
394 | 395 | pull_request, apiuser, api=True): |
|
395 | 396 | raise JSONRPCError('repository `%s` or pull request `%s` ' |
|
396 | 397 | 'does not exist' % (repoid, pullrequestid)) |
|
397 | 398 | |
|
398 | 399 | (pull_request_latest, |
|
399 | 400 | pull_request_at_ver, |
|
400 | 401 | pull_request_display_obj, |
|
401 | 402 | at_version) = PullRequestModel().get_pr_version( |
|
402 | 403 | pull_request.pull_request_id, version=None) |
|
403 | 404 | |
|
404 | 405 | versions = pull_request_display_obj.versions() |
|
405 | 406 | ver_map = { |
|
406 | 407 | ver.pull_request_version_id: cnt |
|
407 | 408 | for cnt, ver in enumerate(versions, 1) |
|
408 | 409 | } |
|
409 | 410 | |
|
410 | 411 | # GENERAL COMMENTS with versions # |
|
411 | 412 | q = CommentsModel()._all_general_comments_of_pull_request(pull_request) |
|
412 | 413 | q = q.order_by(ChangesetComment.comment_id.asc()) |
|
413 | 414 | general_comments = q.all() |
|
414 | 415 | |
|
415 | 416 | # INLINE COMMENTS with versions # |
|
416 | 417 | q = CommentsModel()._all_inline_comments_of_pull_request(pull_request) |
|
417 | 418 | q = q.order_by(ChangesetComment.comment_id.asc()) |
|
418 | 419 | inline_comments = q.all() |
|
419 | 420 | |
|
420 | 421 | data = [] |
|
421 | 422 | for comment in inline_comments + general_comments: |
|
422 | 423 | full_data = comment.get_api_data() |
|
423 | 424 | pr_version_id = None |
|
424 | 425 | if comment.pull_request_version_id: |
|
425 | 426 | pr_version_id = 'v{}'.format( |
|
426 | 427 | ver_map[comment.pull_request_version_id]) |
|
427 | 428 | |
|
428 | 429 | # sanitize some entries |
|
429 | 430 | |
|
430 | 431 | full_data['pull_request_version'] = pr_version_id |
|
431 | 432 | full_data['comment_author'] = { |
|
432 | 433 | 'username': full_data['comment_author'].username, |
|
433 | 434 | 'full_name_or_username': full_data['comment_author'].full_name_or_username, |
|
434 | 435 | 'active': full_data['comment_author'].active, |
|
435 | 436 | } |
|
436 | 437 | |
|
437 | 438 | if full_data['comment_status']: |
|
438 | 439 | full_data['comment_status'] = { |
|
439 | 440 | 'status': full_data['comment_status'][0].status, |
|
440 | 441 | 'status_lbl': full_data['comment_status'][0].status_lbl, |
|
441 | 442 | } |
|
442 | 443 | else: |
|
443 | 444 | full_data['comment_status'] = {} |
|
444 | 445 | |
|
445 | 446 | data.append(full_data) |
|
446 | 447 | return data |
|
447 | 448 | |
|
448 | 449 | |
|
449 | 450 | @jsonrpc_method() |
|
450 | 451 | def comment_pull_request( |
|
451 | 452 | request, apiuser, pullrequestid, repoid=Optional(None), |
|
452 | 453 | message=Optional(None), commit_id=Optional(None), status=Optional(None), |
|
453 | 454 | comment_type=Optional(ChangesetComment.COMMENT_TYPE_NOTE), |
|
454 | 455 | resolves_comment_id=Optional(None), extra_recipients=Optional([]), |
|
455 | userid=Optional(OAttr('apiuser'))): | |
|
456 | userid=Optional(OAttr('apiuser')), send_email=Optional(True)): | |
|
456 | 457 | """ |
|
457 | 458 | Comment on the pull request specified with the `pullrequestid`, |
|
458 | 459 | in the |repo| specified by the `repoid`, and optionally change the |
|
459 | 460 | review status. |
|
460 | 461 | |
|
461 | 462 | :param apiuser: This is filled automatically from the |authtoken|. |
|
462 | 463 | :type apiuser: AuthUser |
|
463 | 464 | :param repoid: Optional repository name or repository ID. |
|
464 | 465 | :type repoid: str or int |
|
465 | 466 | :param pullrequestid: The pull request ID. |
|
466 | 467 | :type pullrequestid: int |
|
467 | 468 | :param commit_id: Specify the commit_id for which to set a comment. If |
|
468 | 469 | given commit_id is different than latest in the PR status |
|
469 | 470 | change won't be performed. |
|
470 | 471 | :type commit_id: str |
|
471 | 472 | :param message: The text content of the comment. |
|
472 | 473 | :type message: str |
|
473 | 474 | :param status: (**Optional**) Set the approval status of the pull |
|
474 | 475 | request. One of: 'not_reviewed', 'approved', 'rejected', |
|
475 | 476 | 'under_review' |
|
476 | 477 | :type status: str |
|
477 | 478 | :param comment_type: Comment type, one of: 'note', 'todo' |
|
478 | 479 | :type comment_type: Optional(str), default: 'note' |
|
479 | 480 | :param resolves_comment_id: id of comment which this one will resolve |
|
480 | 481 | :type resolves_comment_id: Optional(int) |
|
481 | 482 | :param extra_recipients: list of user ids or usernames to add |
|
482 | 483 | notifications for this comment. Acts like a CC for notification |
|
483 | 484 | :type extra_recipients: Optional(list) |
|
484 | 485 | :param userid: Comment on the pull request as this user |
|
485 | 486 | :type userid: Optional(str or int) |
|
487 | :param send_email: Define if this comment should also send email notification | |
|
488 | :type send_email: Optional(bool) | |
|
486 | 489 | |
|
487 | 490 | Example output: |
|
488 | 491 | |
|
489 | 492 | .. code-block:: bash |
|
490 | 493 | |
|
491 | 494 | id : <id_given_in_input> |
|
492 | 495 | result : { |
|
493 | 496 | "pull_request_id": "<Integer>", |
|
494 | 497 | "comment_id": "<Integer>", |
|
495 | 498 | "status": {"given": <given_status>, |
|
496 | 499 | "was_changed": <bool status_was_actually_changed> }, |
|
497 | 500 | }, |
|
498 | 501 | error : null |
|
499 | 502 | """ |
|
500 | 503 | pull_request = get_pull_request_or_error(pullrequestid) |
|
501 | 504 | if Optional.extract(repoid): |
|
502 | 505 | repo = get_repo_or_error(repoid) |
|
503 | 506 | else: |
|
504 | 507 | repo = pull_request.target_repo |
|
505 | 508 | |
|
506 | 509 | auth_user = apiuser |
|
507 | 510 | if not isinstance(userid, Optional): |
|
508 | 511 | if (has_superadmin_permission(apiuser) or |
|
509 | 512 | HasRepoPermissionAnyApi('repository.admin')( |
|
510 | 513 | user=apiuser, repo_name=repo.repo_name)): |
|
511 | 514 | apiuser = get_user_or_error(userid) |
|
512 | 515 | auth_user = apiuser.AuthUser() |
|
513 | 516 | else: |
|
514 | 517 | raise JSONRPCError('userid is not the same as your user') |
|
515 | 518 | |
|
516 | 519 | if pull_request.is_closed(): |
|
517 | 520 | raise JSONRPCError( |
|
518 | 521 | 'pull request `%s` comment failed, pull request is closed' % ( |
|
519 | 522 | pullrequestid,)) |
|
520 | 523 | |
|
521 | 524 | if not PullRequestModel().check_user_read( |
|
522 | 525 | pull_request, apiuser, api=True): |
|
523 | 526 | raise JSONRPCError('repository `%s` does not exist' % (repoid,)) |
|
524 | 527 | message = Optional.extract(message) |
|
525 | 528 | status = Optional.extract(status) |
|
526 | 529 | commit_id = Optional.extract(commit_id) |
|
527 | 530 | comment_type = Optional.extract(comment_type) |
|
528 | 531 | resolves_comment_id = Optional.extract(resolves_comment_id) |
|
529 | 532 | extra_recipients = Optional.extract(extra_recipients) |
|
533 | send_email = Optional.extract(send_email, binary=True) | |
|
530 | 534 | |
|
531 | 535 | if not message and not status: |
|
532 | 536 | raise JSONRPCError( |
|
533 | 537 | 'Both message and status parameters are missing. ' |
|
534 | 538 | 'At least one is required.') |
|
535 | 539 | |
|
536 | 540 | if (status not in (st[0] for st in ChangesetStatus.STATUSES) and |
|
537 | 541 | status is not None): |
|
538 | 542 | raise JSONRPCError('Unknown comment status: `%s`' % status) |
|
539 | 543 | |
|
540 | 544 | if commit_id and commit_id not in pull_request.revisions: |
|
541 | 545 | raise JSONRPCError( |
|
542 | 546 | 'Invalid commit_id `%s` for this pull request.' % commit_id) |
|
543 | 547 | |
|
544 | 548 | allowed_to_change_status = PullRequestModel().check_user_change_status( |
|
545 | 549 | pull_request, apiuser) |
|
546 | 550 | |
|
547 | 551 | # if commit_id is passed re-validated if user is allowed to change status |
|
548 | 552 | # based on latest commit_id from the PR |
|
549 | 553 | if commit_id: |
|
550 | 554 | commit_idx = pull_request.revisions.index(commit_id) |
|
551 | 555 | if commit_idx != 0: |
|
552 | 556 | allowed_to_change_status = False |
|
553 | 557 | |
|
554 | 558 | if resolves_comment_id: |
|
555 | 559 | comment = ChangesetComment.get(resolves_comment_id) |
|
556 | 560 | if not comment: |
|
557 | 561 | raise JSONRPCError( |
|
558 | 562 | 'Invalid resolves_comment_id `%s` for this pull request.' |
|
559 | 563 | % resolves_comment_id) |
|
560 | 564 | if comment.comment_type != ChangesetComment.COMMENT_TYPE_TODO: |
|
561 | 565 | raise JSONRPCError( |
|
562 | 566 | 'Comment `%s` is wrong type for setting status to resolved.' |
|
563 | 567 | % resolves_comment_id) |
|
564 | 568 | |
|
565 | 569 | text = message |
|
566 | 570 | status_label = ChangesetStatus.get_status_lbl(status) |
|
567 | 571 | if status and allowed_to_change_status: |
|
568 | 572 | st_message = ('Status change %(transition_icon)s %(status)s' |
|
569 | 573 | % {'transition_icon': '>', 'status': status_label}) |
|
570 | 574 | text = message or st_message |
|
571 | 575 | |
|
572 | 576 | rc_config = SettingsModel().get_all_settings() |
|
573 | 577 | renderer = rc_config.get('rhodecode_markup_renderer', 'rst') |
|
574 | 578 | |
|
575 | 579 | status_change = status and allowed_to_change_status |
|
576 | 580 | comment = CommentsModel().create( |
|
577 | 581 | text=text, |
|
578 | 582 | repo=pull_request.target_repo.repo_id, |
|
579 | 583 | user=apiuser.user_id, |
|
580 | 584 | pull_request=pull_request.pull_request_id, |
|
581 | 585 | f_path=None, |
|
582 | 586 | line_no=None, |
|
583 | 587 | status_change=(status_label if status_change else None), |
|
584 | 588 | status_change_type=(status if status_change else None), |
|
585 | 589 | closing_pr=False, |
|
586 | 590 | renderer=renderer, |
|
587 | 591 | comment_type=comment_type, |
|
588 | 592 | resolves_comment_id=resolves_comment_id, |
|
589 | 593 | auth_user=auth_user, |
|
590 | extra_recipients=extra_recipients | |
|
594 | extra_recipients=extra_recipients, | |
|
595 | send_email=send_email | |
|
591 | 596 | ) |
|
592 | 597 | |
|
593 | 598 | if allowed_to_change_status and status: |
|
594 | 599 | old_calculated_status = pull_request.calculated_review_status() |
|
595 | 600 | ChangesetStatusModel().set_status( |
|
596 | 601 | pull_request.target_repo.repo_id, |
|
597 | 602 | status, |
|
598 | 603 | apiuser.user_id, |
|
599 | 604 | comment, |
|
600 | 605 | pull_request=pull_request.pull_request_id |
|
601 | 606 | ) |
|
602 | 607 | Session().flush() |
|
603 | 608 | |
|
604 | 609 | Session().commit() |
|
605 | 610 | |
|
606 | 611 | PullRequestModel().trigger_pull_request_hook( |
|
607 | 612 | pull_request, apiuser, 'comment', |
|
608 | 613 | data={'comment': comment}) |
|
609 | 614 | |
|
610 | 615 | if allowed_to_change_status and status: |
|
611 | 616 | # we now calculate the status of pull request, and based on that |
|
612 | 617 | # calculation we set the commits status |
|
613 | 618 | calculated_status = pull_request.calculated_review_status() |
|
614 | 619 | if old_calculated_status != calculated_status: |
|
615 | 620 | PullRequestModel().trigger_pull_request_hook( |
|
616 | 621 | pull_request, apiuser, 'review_status_change', |
|
617 | 622 | data={'status': calculated_status}) |
|
618 | 623 | |
|
619 | 624 | data = { |
|
620 | 625 | 'pull_request_id': pull_request.pull_request_id, |
|
621 | 626 | 'comment_id': comment.comment_id if comment else None, |
|
622 | 627 | 'status': {'given': status, 'was_changed': status_change}, |
|
623 | 628 | } |
|
624 | 629 | return data |
|
625 | 630 | |
|
626 | 631 | |
|
627 | 632 | @jsonrpc_method() |
|
628 | 633 | def create_pull_request( |
|
629 | 634 | request, apiuser, source_repo, target_repo, source_ref, target_ref, |
|
630 | 635 | owner=Optional(OAttr('apiuser')), title=Optional(''), description=Optional(''), |
|
631 | 636 | description_renderer=Optional(''), reviewers=Optional(None)): |
|
632 | 637 | """ |
|
633 | 638 | Creates a new pull request. |
|
634 | 639 | |
|
635 | 640 | Accepts refs in the following formats: |
|
636 | 641 | |
|
637 | 642 | * branch:<branch_name>:<sha> |
|
638 | 643 | * branch:<branch_name> |
|
639 | 644 | * bookmark:<bookmark_name>:<sha> (Mercurial only) |
|
640 | 645 | * bookmark:<bookmark_name> (Mercurial only) |
|
641 | 646 | |
|
642 | 647 | :param apiuser: This is filled automatically from the |authtoken|. |
|
643 | 648 | :type apiuser: AuthUser |
|
644 | 649 | :param source_repo: Set the source repository name. |
|
645 | 650 | :type source_repo: str |
|
646 | 651 | :param target_repo: Set the target repository name. |
|
647 | 652 | :type target_repo: str |
|
648 | 653 | :param source_ref: Set the source ref name. |
|
649 | 654 | :type source_ref: str |
|
650 | 655 | :param target_ref: Set the target ref name. |
|
651 | 656 | :type target_ref: str |
|
652 | 657 | :param owner: user_id or username |
|
653 | 658 | :type owner: Optional(str) |
|
654 | 659 | :param title: Optionally Set the pull request title, it's generated otherwise |
|
655 | 660 | :type title: str |
|
656 | 661 | :param description: Set the pull request description. |
|
657 | 662 | :type description: Optional(str) |
|
658 | 663 | :type description_renderer: Optional(str) |
|
659 | 664 | :param description_renderer: Set pull request renderer for the description. |
|
660 | 665 | It should be 'rst', 'markdown' or 'plain'. If not give default |
|
661 | 666 | system renderer will be used |
|
662 | 667 | :param reviewers: Set the new pull request reviewers list. |
|
663 | 668 | Reviewer defined by review rules will be added automatically to the |
|
664 | 669 | defined list. |
|
665 | 670 | :type reviewers: Optional(list) |
|
666 | 671 | Accepts username strings or objects of the format: |
|
667 | 672 | |
|
668 | 673 | [{'username': 'nick', 'reasons': ['original author'], 'mandatory': <bool>}] |
|
669 | 674 | """ |
|
670 | 675 | |
|
671 | 676 | source_db_repo = get_repo_or_error(source_repo) |
|
672 | 677 | target_db_repo = get_repo_or_error(target_repo) |
|
673 | 678 | if not has_superadmin_permission(apiuser): |
|
674 | 679 | _perms = ('repository.admin', 'repository.write', 'repository.read',) |
|
675 | 680 | validate_repo_permissions(apiuser, source_repo, source_db_repo, _perms) |
|
676 | 681 | |
|
677 | 682 | owner = validate_set_owner_permissions(apiuser, owner) |
|
678 | 683 | |
|
679 | 684 | full_source_ref = resolve_ref_or_error(source_ref, source_db_repo) |
|
680 | 685 | full_target_ref = resolve_ref_or_error(target_ref, target_db_repo) |
|
681 | 686 | |
|
682 | 687 | source_scm = source_db_repo.scm_instance() |
|
683 | 688 | target_scm = target_db_repo.scm_instance() |
|
684 | 689 | |
|
685 | 690 | source_commit = get_commit_or_error(full_source_ref, source_db_repo) |
|
686 | 691 | target_commit = get_commit_or_error(full_target_ref, target_db_repo) |
|
687 | 692 | |
|
688 | 693 | ancestor = source_scm.get_common_ancestor( |
|
689 | 694 | source_commit.raw_id, target_commit.raw_id, target_scm) |
|
690 | 695 | if not ancestor: |
|
691 | 696 | raise JSONRPCError('no common ancestor found') |
|
692 | 697 | |
|
693 | 698 | # recalculate target ref based on ancestor |
|
694 | 699 | target_ref_type, target_ref_name, __ = full_target_ref.split(':') |
|
695 | 700 | full_target_ref = ':'.join((target_ref_type, target_ref_name, ancestor)) |
|
696 | 701 | |
|
697 | 702 | commit_ranges = target_scm.compare( |
|
698 | 703 | target_commit.raw_id, source_commit.raw_id, source_scm, |
|
699 | 704 | merge=True, pre_load=[]) |
|
700 | 705 | |
|
701 | 706 | if not commit_ranges: |
|
702 | 707 | raise JSONRPCError('no commits found') |
|
703 | 708 | |
|
704 | 709 | reviewer_objects = Optional.extract(reviewers) or [] |
|
705 | 710 | |
|
706 | 711 | # serialize and validate passed in given reviewers |
|
707 | 712 | if reviewer_objects: |
|
708 | 713 | schema = ReviewerListSchema() |
|
709 | 714 | try: |
|
710 | 715 | reviewer_objects = schema.deserialize(reviewer_objects) |
|
711 | 716 | except Invalid as err: |
|
712 | 717 | raise JSONRPCValidationError(colander_exc=err) |
|
713 | 718 | |
|
714 | 719 | # validate users |
|
715 | 720 | for reviewer_object in reviewer_objects: |
|
716 | 721 | user = get_user_or_error(reviewer_object['username']) |
|
717 | 722 | reviewer_object['user_id'] = user.user_id |
|
718 | 723 | |
|
719 | 724 | get_default_reviewers_data, validate_default_reviewers = \ |
|
720 | 725 | PullRequestModel().get_reviewer_functions() |
|
721 | 726 | |
|
722 | 727 | # recalculate reviewers logic, to make sure we can validate this |
|
723 | 728 | reviewer_rules = get_default_reviewers_data( |
|
724 | 729 | owner, source_db_repo, |
|
725 | 730 | source_commit, target_db_repo, target_commit) |
|
726 | 731 | |
|
727 | 732 | # now MERGE our given with the calculated |
|
728 | 733 | reviewer_objects = reviewer_rules['reviewers'] + reviewer_objects |
|
729 | 734 | |
|
730 | 735 | try: |
|
731 | 736 | reviewers = validate_default_reviewers( |
|
732 | 737 | reviewer_objects, reviewer_rules) |
|
733 | 738 | except ValueError as e: |
|
734 | 739 | raise JSONRPCError('Reviewers Validation: {}'.format(e)) |
|
735 | 740 | |
|
736 | 741 | title = Optional.extract(title) |
|
737 | 742 | if not title: |
|
738 | 743 | title_source_ref = source_ref.split(':', 2)[1] |
|
739 | 744 | title = PullRequestModel().generate_pullrequest_title( |
|
740 | 745 | source=source_repo, |
|
741 | 746 | source_ref=title_source_ref, |
|
742 | 747 | target=target_repo |
|
743 | 748 | ) |
|
744 | 749 | # fetch renderer, if set fallback to plain in case of PR |
|
745 | 750 | rc_config = SettingsModel().get_all_settings() |
|
746 | 751 | default_system_renderer = rc_config.get('rhodecode_markup_renderer', 'plain') |
|
747 | 752 | description = Optional.extract(description) |
|
748 | 753 | description_renderer = Optional.extract(description_renderer) or default_system_renderer |
|
749 | 754 | |
|
750 | 755 | pull_request = PullRequestModel().create( |
|
751 | 756 | created_by=owner.user_id, |
|
752 | 757 | source_repo=source_repo, |
|
753 | 758 | source_ref=full_source_ref, |
|
754 | 759 | target_repo=target_repo, |
|
755 | 760 | target_ref=full_target_ref, |
|
756 | 761 | revisions=[commit.raw_id for commit in reversed(commit_ranges)], |
|
757 | 762 | reviewers=reviewers, |
|
758 | 763 | title=title, |
|
759 | 764 | description=description, |
|
760 | 765 | description_renderer=description_renderer, |
|
761 | 766 | reviewer_data=reviewer_rules, |
|
762 | 767 | auth_user=apiuser |
|
763 | 768 | ) |
|
764 | 769 | |
|
765 | 770 | Session().commit() |
|
766 | 771 | data = { |
|
767 | 772 | 'msg': 'Created new pull request `{}`'.format(title), |
|
768 | 773 | 'pull_request_id': pull_request.pull_request_id, |
|
769 | 774 | } |
|
770 | 775 | return data |
|
771 | 776 | |
|
772 | 777 | |
|
773 | 778 | @jsonrpc_method() |
|
774 | 779 | def update_pull_request( |
|
775 | 780 | request, apiuser, pullrequestid, repoid=Optional(None), |
|
776 | 781 | title=Optional(''), description=Optional(''), description_renderer=Optional(''), |
|
777 | 782 | reviewers=Optional(None), update_commits=Optional(None)): |
|
778 | 783 | """ |
|
779 | 784 | Updates a pull request. |
|
780 | 785 | |
|
781 | 786 | :param apiuser: This is filled automatically from the |authtoken|. |
|
782 | 787 | :type apiuser: AuthUser |
|
783 | 788 | :param repoid: Optional repository name or repository ID. |
|
784 | 789 | :type repoid: str or int |
|
785 | 790 | :param pullrequestid: The pull request ID. |
|
786 | 791 | :type pullrequestid: int |
|
787 | 792 | :param title: Set the pull request title. |
|
788 | 793 | :type title: str |
|
789 | 794 | :param description: Update pull request description. |
|
790 | 795 | :type description: Optional(str) |
|
791 | 796 | :type description_renderer: Optional(str) |
|
792 | 797 | :param description_renderer: Update pull request renderer for the description. |
|
793 | 798 | It should be 'rst', 'markdown' or 'plain' |
|
794 | 799 | :param reviewers: Update pull request reviewers list with new value. |
|
795 | 800 | :type reviewers: Optional(list) |
|
796 | 801 | Accepts username strings or objects of the format: |
|
797 | 802 | |
|
798 | 803 | [{'username': 'nick', 'reasons': ['original author'], 'mandatory': <bool>}] |
|
799 | 804 | |
|
800 | 805 | :param update_commits: Trigger update of commits for this pull request |
|
801 | 806 | :type: update_commits: Optional(bool) |
|
802 | 807 | |
|
803 | 808 | Example output: |
|
804 | 809 | |
|
805 | 810 | .. code-block:: bash |
|
806 | 811 | |
|
807 | 812 | id : <id_given_in_input> |
|
808 | 813 | result : { |
|
809 | 814 | "msg": "Updated pull request `63`", |
|
810 | 815 | "pull_request": <pull_request_object>, |
|
811 | 816 | "updated_reviewers": { |
|
812 | 817 | "added": [ |
|
813 | 818 | "username" |
|
814 | 819 | ], |
|
815 | 820 | "removed": [] |
|
816 | 821 | }, |
|
817 | 822 | "updated_commits": { |
|
818 | 823 | "added": [ |
|
819 | 824 | "<sha1_hash>" |
|
820 | 825 | ], |
|
821 | 826 | "common": [ |
|
822 | 827 | "<sha1_hash>", |
|
823 | 828 | "<sha1_hash>", |
|
824 | 829 | ], |
|
825 | 830 | "removed": [] |
|
826 | 831 | } |
|
827 | 832 | } |
|
828 | 833 | error : null |
|
829 | 834 | """ |
|
830 | 835 | |
|
831 | 836 | pull_request = get_pull_request_or_error(pullrequestid) |
|
832 | 837 | if Optional.extract(repoid): |
|
833 | 838 | repo = get_repo_or_error(repoid) |
|
834 | 839 | else: |
|
835 | 840 | repo = pull_request.target_repo |
|
836 | 841 | |
|
837 | 842 | if not PullRequestModel().check_user_update( |
|
838 | 843 | pull_request, apiuser, api=True): |
|
839 | 844 | raise JSONRPCError( |
|
840 | 845 | 'pull request `%s` update failed, no permission to update.' % ( |
|
841 | 846 | pullrequestid,)) |
|
842 | 847 | if pull_request.is_closed(): |
|
843 | 848 | raise JSONRPCError( |
|
844 | 849 | 'pull request `%s` update failed, pull request is closed' % ( |
|
845 | 850 | pullrequestid,)) |
|
846 | 851 | |
|
847 | 852 | reviewer_objects = Optional.extract(reviewers) or [] |
|
848 | 853 | |
|
849 | 854 | if reviewer_objects: |
|
850 | 855 | schema = ReviewerListSchema() |
|
851 | 856 | try: |
|
852 | 857 | reviewer_objects = schema.deserialize(reviewer_objects) |
|
853 | 858 | except Invalid as err: |
|
854 | 859 | raise JSONRPCValidationError(colander_exc=err) |
|
855 | 860 | |
|
856 | 861 | # validate users |
|
857 | 862 | for reviewer_object in reviewer_objects: |
|
858 | 863 | user = get_user_or_error(reviewer_object['username']) |
|
859 | 864 | reviewer_object['user_id'] = user.user_id |
|
860 | 865 | |
|
861 | 866 | get_default_reviewers_data, get_validated_reviewers = \ |
|
862 | 867 | PullRequestModel().get_reviewer_functions() |
|
863 | 868 | |
|
864 | 869 | # re-use stored rules |
|
865 | 870 | reviewer_rules = pull_request.reviewer_data |
|
866 | 871 | try: |
|
867 | 872 | reviewers = get_validated_reviewers( |
|
868 | 873 | reviewer_objects, reviewer_rules) |
|
869 | 874 | except ValueError as e: |
|
870 | 875 | raise JSONRPCError('Reviewers Validation: {}'.format(e)) |
|
871 | 876 | else: |
|
872 | 877 | reviewers = [] |
|
873 | 878 | |
|
874 | 879 | title = Optional.extract(title) |
|
875 | 880 | description = Optional.extract(description) |
|
876 | 881 | description_renderer = Optional.extract(description_renderer) |
|
877 | 882 | |
|
878 | 883 | if title or description: |
|
879 | 884 | PullRequestModel().edit( |
|
880 | 885 | pull_request, |
|
881 | 886 | title or pull_request.title, |
|
882 | 887 | description or pull_request.description, |
|
883 | 888 | description_renderer or pull_request.description_renderer, |
|
884 | 889 | apiuser) |
|
885 | 890 | Session().commit() |
|
886 | 891 | |
|
887 | 892 | commit_changes = {"added": [], "common": [], "removed": []} |
|
888 | 893 | if str2bool(Optional.extract(update_commits)): |
|
889 | 894 | |
|
890 | 895 | if pull_request.pull_request_state != PullRequest.STATE_CREATED: |
|
891 | 896 | raise JSONRPCError( |
|
892 | 897 | 'Operation forbidden because pull request is in state {}, ' |
|
893 | 898 | 'only state {} is allowed.'.format( |
|
894 | 899 | pull_request.pull_request_state, PullRequest.STATE_CREATED)) |
|
895 | 900 | |
|
896 | 901 | with pull_request.set_state(PullRequest.STATE_UPDATING): |
|
897 | 902 | if PullRequestModel().has_valid_update_type(pull_request): |
|
898 | 903 | db_user = apiuser.get_instance() |
|
899 | 904 | update_response = PullRequestModel().update_commits( |
|
900 | 905 | pull_request, db_user) |
|
901 | 906 | commit_changes = update_response.changes or commit_changes |
|
902 | 907 | Session().commit() |
|
903 | 908 | |
|
904 | 909 | reviewers_changes = {"added": [], "removed": []} |
|
905 | 910 | if reviewers: |
|
906 | 911 | old_calculated_status = pull_request.calculated_review_status() |
|
907 | 912 | added_reviewers, removed_reviewers = \ |
|
908 | 913 | PullRequestModel().update_reviewers(pull_request, reviewers, apiuser) |
|
909 | 914 | |
|
910 | 915 | reviewers_changes['added'] = sorted( |
|
911 | 916 | [get_user_or_error(n).username for n in added_reviewers]) |
|
912 | 917 | reviewers_changes['removed'] = sorted( |
|
913 | 918 | [get_user_or_error(n).username for n in removed_reviewers]) |
|
914 | 919 | Session().commit() |
|
915 | 920 | |
|
916 | 921 | # trigger status changed if change in reviewers changes the status |
|
917 | 922 | calculated_status = pull_request.calculated_review_status() |
|
918 | 923 | if old_calculated_status != calculated_status: |
|
919 | 924 | PullRequestModel().trigger_pull_request_hook( |
|
920 | 925 | pull_request, apiuser, 'review_status_change', |
|
921 | 926 | data={'status': calculated_status}) |
|
922 | 927 | |
|
923 | 928 | data = { |
|
924 | 929 | 'msg': 'Updated pull request `{}`'.format( |
|
925 | 930 | pull_request.pull_request_id), |
|
926 | 931 | 'pull_request': pull_request.get_api_data(), |
|
927 | 932 | 'updated_commits': commit_changes, |
|
928 | 933 | 'updated_reviewers': reviewers_changes |
|
929 | 934 | } |
|
930 | 935 | |
|
931 | 936 | return data |
|
932 | 937 | |
|
933 | 938 | |
|
934 | 939 | @jsonrpc_method() |
|
935 | 940 | def close_pull_request( |
|
936 | 941 | request, apiuser, pullrequestid, repoid=Optional(None), |
|
937 | 942 | userid=Optional(OAttr('apiuser')), message=Optional('')): |
|
938 | 943 | """ |
|
939 | 944 | Close the pull request specified by `pullrequestid`. |
|
940 | 945 | |
|
941 | 946 | :param apiuser: This is filled automatically from the |authtoken|. |
|
942 | 947 | :type apiuser: AuthUser |
|
943 | 948 | :param repoid: Repository name or repository ID to which the pull |
|
944 | 949 | request belongs. |
|
945 | 950 | :type repoid: str or int |
|
946 | 951 | :param pullrequestid: ID of the pull request to be closed. |
|
947 | 952 | :type pullrequestid: int |
|
948 | 953 | :param userid: Close the pull request as this user. |
|
949 | 954 | :type userid: Optional(str or int) |
|
950 | 955 | :param message: Optional message to close the Pull Request with. If not |
|
951 | 956 | specified it will be generated automatically. |
|
952 | 957 | :type message: Optional(str) |
|
953 | 958 | |
|
954 | 959 | Example output: |
|
955 | 960 | |
|
956 | 961 | .. code-block:: bash |
|
957 | 962 | |
|
958 | 963 | "id": <id_given_in_input>, |
|
959 | 964 | "result": { |
|
960 | 965 | "pull_request_id": "<int>", |
|
961 | 966 | "close_status": "<str:status_lbl>, |
|
962 | 967 | "closed": "<bool>" |
|
963 | 968 | }, |
|
964 | 969 | "error": null |
|
965 | 970 | |
|
966 | 971 | """ |
|
967 | 972 | _ = request.translate |
|
968 | 973 | |
|
969 | 974 | pull_request = get_pull_request_or_error(pullrequestid) |
|
970 | 975 | if Optional.extract(repoid): |
|
971 | 976 | repo = get_repo_or_error(repoid) |
|
972 | 977 | else: |
|
973 | 978 | repo = pull_request.target_repo |
|
974 | 979 | |
|
975 | 980 | if not isinstance(userid, Optional): |
|
976 | 981 | if (has_superadmin_permission(apiuser) or |
|
977 | 982 | HasRepoPermissionAnyApi('repository.admin')( |
|
978 | 983 | user=apiuser, repo_name=repo.repo_name)): |
|
979 | 984 | apiuser = get_user_or_error(userid) |
|
980 | 985 | else: |
|
981 | 986 | raise JSONRPCError('userid is not the same as your user') |
|
982 | 987 | |
|
983 | 988 | if pull_request.is_closed(): |
|
984 | 989 | raise JSONRPCError( |
|
985 | 990 | 'pull request `%s` is already closed' % (pullrequestid,)) |
|
986 | 991 | |
|
987 | 992 | # only owner or admin or person with write permissions |
|
988 | 993 | allowed_to_close = PullRequestModel().check_user_update( |
|
989 | 994 | pull_request, apiuser, api=True) |
|
990 | 995 | |
|
991 | 996 | if not allowed_to_close: |
|
992 | 997 | raise JSONRPCError( |
|
993 | 998 | 'pull request `%s` close failed, no permission to close.' % ( |
|
994 | 999 | pullrequestid,)) |
|
995 | 1000 | |
|
996 | 1001 | # message we're using to close the PR, else it's automatically generated |
|
997 | 1002 | message = Optional.extract(message) |
|
998 | 1003 | |
|
999 | 1004 | # finally close the PR, with proper message comment |
|
1000 | 1005 | comment, status = PullRequestModel().close_pull_request_with_comment( |
|
1001 | 1006 | pull_request, apiuser, repo, message=message, auth_user=apiuser) |
|
1002 | 1007 | status_lbl = ChangesetStatus.get_status_lbl(status) |
|
1003 | 1008 | |
|
1004 | 1009 | Session().commit() |
|
1005 | 1010 | |
|
1006 | 1011 | data = { |
|
1007 | 1012 | 'pull_request_id': pull_request.pull_request_id, |
|
1008 | 1013 | 'close_status': status_lbl, |
|
1009 | 1014 | 'closed': True, |
|
1010 | 1015 | } |
|
1011 | 1016 | return data |
@@ -1,2339 +1,2343 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | import time |
|
23 | 23 | |
|
24 | 24 | import rhodecode |
|
25 | 25 | from rhodecode.api import ( |
|
26 | 26 | jsonrpc_method, JSONRPCError, JSONRPCForbidden, JSONRPCValidationError) |
|
27 | 27 | from rhodecode.api.utils import ( |
|
28 | 28 | has_superadmin_permission, Optional, OAttr, get_repo_or_error, |
|
29 | 29 | get_user_group_or_error, get_user_or_error, validate_repo_permissions, |
|
30 | 30 | get_perm_or_error, parse_args, get_origin, build_commit_data, |
|
31 | 31 | validate_set_owner_permissions) |
|
32 | 32 | from rhodecode.lib import audit_logger, rc_cache |
|
33 | 33 | from rhodecode.lib import repo_maintenance |
|
34 | 34 | from rhodecode.lib.auth import HasPermissionAnyApi, HasUserGroupPermissionAnyApi |
|
35 | 35 | from rhodecode.lib.celerylib.utils import get_task_id |
|
36 | 36 | from rhodecode.lib.utils2 import str2bool, time_to_datetime, safe_str, safe_int |
|
37 | 37 | from rhodecode.lib.ext_json import json |
|
38 | 38 | from rhodecode.lib.exceptions import StatusChangeOnClosedPullRequestError |
|
39 | 39 | from rhodecode.lib.vcs import RepositoryError |
|
40 | 40 | from rhodecode.lib.vcs.exceptions import NodeDoesNotExistError |
|
41 | 41 | from rhodecode.model.changeset_status import ChangesetStatusModel |
|
42 | 42 | from rhodecode.model.comment import CommentsModel |
|
43 | 43 | from rhodecode.model.db import ( |
|
44 | 44 | Session, ChangesetStatus, RepositoryField, Repository, RepoGroup, |
|
45 | 45 | ChangesetComment) |
|
46 | 46 | from rhodecode.model.permission import PermissionModel |
|
47 | 47 | from rhodecode.model.repo import RepoModel |
|
48 | 48 | from rhodecode.model.scm import ScmModel, RepoList |
|
49 | 49 | from rhodecode.model.settings import SettingsModel, VcsSettingsModel |
|
50 | 50 | from rhodecode.model import validation_schema |
|
51 | 51 | from rhodecode.model.validation_schema.schemas import repo_schema |
|
52 | 52 | |
|
53 | 53 | log = logging.getLogger(__name__) |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | @jsonrpc_method() |
|
57 | 57 | def get_repo(request, apiuser, repoid, cache=Optional(True)): |
|
58 | 58 | """ |
|
59 | 59 | Gets an existing repository by its name or repository_id. |
|
60 | 60 | |
|
61 | 61 | The members section so the output returns users groups or users |
|
62 | 62 | associated with that repository. |
|
63 | 63 | |
|
64 | 64 | This command can only be run using an |authtoken| with admin rights, |
|
65 | 65 | or users with at least read rights to the |repo|. |
|
66 | 66 | |
|
67 | 67 | :param apiuser: This is filled automatically from the |authtoken|. |
|
68 | 68 | :type apiuser: AuthUser |
|
69 | 69 | :param repoid: The repository name or repository id. |
|
70 | 70 | :type repoid: str or int |
|
71 | 71 | :param cache: use the cached value for last changeset |
|
72 | 72 | :type: cache: Optional(bool) |
|
73 | 73 | |
|
74 | 74 | Example output: |
|
75 | 75 | |
|
76 | 76 | .. code-block:: bash |
|
77 | 77 | |
|
78 | 78 | { |
|
79 | 79 | "error": null, |
|
80 | 80 | "id": <repo_id>, |
|
81 | 81 | "result": { |
|
82 | 82 | "clone_uri": null, |
|
83 | 83 | "created_on": "timestamp", |
|
84 | 84 | "description": "repo description", |
|
85 | 85 | "enable_downloads": false, |
|
86 | 86 | "enable_locking": false, |
|
87 | 87 | "enable_statistics": false, |
|
88 | 88 | "followers": [ |
|
89 | 89 | { |
|
90 | 90 | "active": true, |
|
91 | 91 | "admin": false, |
|
92 | 92 | "api_key": "****************************************", |
|
93 | 93 | "api_keys": [ |
|
94 | 94 | "****************************************" |
|
95 | 95 | ], |
|
96 | 96 | "email": "user@example.com", |
|
97 | 97 | "emails": [ |
|
98 | 98 | "user@example.com" |
|
99 | 99 | ], |
|
100 | 100 | "extern_name": "rhodecode", |
|
101 | 101 | "extern_type": "rhodecode", |
|
102 | 102 | "firstname": "username", |
|
103 | 103 | "ip_addresses": [], |
|
104 | 104 | "language": null, |
|
105 | 105 | "last_login": "2015-09-16T17:16:35.854", |
|
106 | 106 | "lastname": "surname", |
|
107 | 107 | "user_id": <user_id>, |
|
108 | 108 | "username": "name" |
|
109 | 109 | } |
|
110 | 110 | ], |
|
111 | 111 | "fork_of": "parent-repo", |
|
112 | 112 | "landing_rev": [ |
|
113 | 113 | "rev", |
|
114 | 114 | "tip" |
|
115 | 115 | ], |
|
116 | 116 | "last_changeset": { |
|
117 | 117 | "author": "User <user@example.com>", |
|
118 | 118 | "branch": "default", |
|
119 | 119 | "date": "timestamp", |
|
120 | 120 | "message": "last commit message", |
|
121 | 121 | "parents": [ |
|
122 | 122 | { |
|
123 | 123 | "raw_id": "commit-id" |
|
124 | 124 | } |
|
125 | 125 | ], |
|
126 | 126 | "raw_id": "commit-id", |
|
127 | 127 | "revision": <revision number>, |
|
128 | 128 | "short_id": "short id" |
|
129 | 129 | }, |
|
130 | 130 | "lock_reason": null, |
|
131 | 131 | "locked_by": null, |
|
132 | 132 | "locked_date": null, |
|
133 | 133 | "owner": "owner-name", |
|
134 | 134 | "permissions": [ |
|
135 | 135 | { |
|
136 | 136 | "name": "super-admin-name", |
|
137 | 137 | "origin": "super-admin", |
|
138 | 138 | "permission": "repository.admin", |
|
139 | 139 | "type": "user" |
|
140 | 140 | }, |
|
141 | 141 | { |
|
142 | 142 | "name": "owner-name", |
|
143 | 143 | "origin": "owner", |
|
144 | 144 | "permission": "repository.admin", |
|
145 | 145 | "type": "user" |
|
146 | 146 | }, |
|
147 | 147 | { |
|
148 | 148 | "name": "user-group-name", |
|
149 | 149 | "origin": "permission", |
|
150 | 150 | "permission": "repository.write", |
|
151 | 151 | "type": "user_group" |
|
152 | 152 | } |
|
153 | 153 | ], |
|
154 | 154 | "private": true, |
|
155 | 155 | "repo_id": 676, |
|
156 | 156 | "repo_name": "user-group/repo-name", |
|
157 | 157 | "repo_type": "hg" |
|
158 | 158 | } |
|
159 | 159 | } |
|
160 | 160 | """ |
|
161 | 161 | |
|
162 | 162 | repo = get_repo_or_error(repoid) |
|
163 | 163 | cache = Optional.extract(cache) |
|
164 | 164 | |
|
165 | 165 | include_secrets = False |
|
166 | 166 | if has_superadmin_permission(apiuser): |
|
167 | 167 | include_secrets = True |
|
168 | 168 | else: |
|
169 | 169 | # check if we have at least read permission for this repo ! |
|
170 | 170 | _perms = ( |
|
171 | 171 | 'repository.admin', 'repository.write', 'repository.read',) |
|
172 | 172 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
173 | 173 | |
|
174 | 174 | permissions = [] |
|
175 | 175 | for _user in repo.permissions(): |
|
176 | 176 | user_data = { |
|
177 | 177 | 'name': _user.username, |
|
178 | 178 | 'permission': _user.permission, |
|
179 | 179 | 'origin': get_origin(_user), |
|
180 | 180 | 'type': "user", |
|
181 | 181 | } |
|
182 | 182 | permissions.append(user_data) |
|
183 | 183 | |
|
184 | 184 | for _user_group in repo.permission_user_groups(): |
|
185 | 185 | user_group_data = { |
|
186 | 186 | 'name': _user_group.users_group_name, |
|
187 | 187 | 'permission': _user_group.permission, |
|
188 | 188 | 'origin': get_origin(_user_group), |
|
189 | 189 | 'type': "user_group", |
|
190 | 190 | } |
|
191 | 191 | permissions.append(user_group_data) |
|
192 | 192 | |
|
193 | 193 | following_users = [ |
|
194 | 194 | user.user.get_api_data(include_secrets=include_secrets) |
|
195 | 195 | for user in repo.followers] |
|
196 | 196 | |
|
197 | 197 | if not cache: |
|
198 | 198 | repo.update_commit_cache() |
|
199 | 199 | data = repo.get_api_data(include_secrets=include_secrets) |
|
200 | 200 | data['permissions'] = permissions |
|
201 | 201 | data['followers'] = following_users |
|
202 | 202 | return data |
|
203 | 203 | |
|
204 | 204 | |
|
205 | 205 | @jsonrpc_method() |
|
206 | 206 | def get_repos(request, apiuser, root=Optional(None), traverse=Optional(True)): |
|
207 | 207 | """ |
|
208 | 208 | Lists all existing repositories. |
|
209 | 209 | |
|
210 | 210 | This command can only be run using an |authtoken| with admin rights, |
|
211 | 211 | or users with at least read rights to |repos|. |
|
212 | 212 | |
|
213 | 213 | :param apiuser: This is filled automatically from the |authtoken|. |
|
214 | 214 | :type apiuser: AuthUser |
|
215 | 215 | :param root: specify root repository group to fetch repositories. |
|
216 | 216 | filters the returned repositories to be members of given root group. |
|
217 | 217 | :type root: Optional(None) |
|
218 | 218 | :param traverse: traverse given root into subrepositories. With this flag |
|
219 | 219 | set to False, it will only return top-level repositories from `root`. |
|
220 | 220 | if root is empty it will return just top-level repositories. |
|
221 | 221 | :type traverse: Optional(True) |
|
222 | 222 | |
|
223 | 223 | |
|
224 | 224 | Example output: |
|
225 | 225 | |
|
226 | 226 | .. code-block:: bash |
|
227 | 227 | |
|
228 | 228 | id : <id_given_in_input> |
|
229 | 229 | result: [ |
|
230 | 230 | { |
|
231 | 231 | "repo_id" : "<repo_id>", |
|
232 | 232 | "repo_name" : "<reponame>" |
|
233 | 233 | "repo_type" : "<repo_type>", |
|
234 | 234 | "clone_uri" : "<clone_uri>", |
|
235 | 235 | "private": : "<bool>", |
|
236 | 236 | "created_on" : "<datetimecreated>", |
|
237 | 237 | "description" : "<description>", |
|
238 | 238 | "landing_rev": "<landing_rev>", |
|
239 | 239 | "owner": "<repo_owner>", |
|
240 | 240 | "fork_of": "<name_of_fork_parent>", |
|
241 | 241 | "enable_downloads": "<bool>", |
|
242 | 242 | "enable_locking": "<bool>", |
|
243 | 243 | "enable_statistics": "<bool>", |
|
244 | 244 | }, |
|
245 | 245 | ... |
|
246 | 246 | ] |
|
247 | 247 | error: null |
|
248 | 248 | """ |
|
249 | 249 | |
|
250 | 250 | include_secrets = has_superadmin_permission(apiuser) |
|
251 | 251 | _perms = ('repository.read', 'repository.write', 'repository.admin',) |
|
252 | 252 | extras = {'user': apiuser} |
|
253 | 253 | |
|
254 | 254 | root = Optional.extract(root) |
|
255 | 255 | traverse = Optional.extract(traverse, binary=True) |
|
256 | 256 | |
|
257 | 257 | if root: |
|
258 | 258 | # verify parent existance, if it's empty return an error |
|
259 | 259 | parent = RepoGroup.get_by_group_name(root) |
|
260 | 260 | if not parent: |
|
261 | 261 | raise JSONRPCError( |
|
262 | 262 | 'Root repository group `{}` does not exist'.format(root)) |
|
263 | 263 | |
|
264 | 264 | if traverse: |
|
265 | 265 | repos = RepoModel().get_repos_for_root(root=root, traverse=traverse) |
|
266 | 266 | else: |
|
267 | 267 | repos = RepoModel().get_repos_for_root(root=parent) |
|
268 | 268 | else: |
|
269 | 269 | if traverse: |
|
270 | 270 | repos = RepoModel().get_all() |
|
271 | 271 | else: |
|
272 | 272 | # return just top-level |
|
273 | 273 | repos = RepoModel().get_repos_for_root(root=None) |
|
274 | 274 | |
|
275 | 275 | repo_list = RepoList(repos, perm_set=_perms, extra_kwargs=extras) |
|
276 | 276 | return [repo.get_api_data(include_secrets=include_secrets) |
|
277 | 277 | for repo in repo_list] |
|
278 | 278 | |
|
279 | 279 | |
|
280 | 280 | @jsonrpc_method() |
|
281 | 281 | def get_repo_changeset(request, apiuser, repoid, revision, |
|
282 | 282 | details=Optional('basic')): |
|
283 | 283 | """ |
|
284 | 284 | Returns information about a changeset. |
|
285 | 285 | |
|
286 | 286 | Additionally parameters define the amount of details returned by |
|
287 | 287 | this function. |
|
288 | 288 | |
|
289 | 289 | This command can only be run using an |authtoken| with admin rights, |
|
290 | 290 | or users with at least read rights to the |repo|. |
|
291 | 291 | |
|
292 | 292 | :param apiuser: This is filled automatically from the |authtoken|. |
|
293 | 293 | :type apiuser: AuthUser |
|
294 | 294 | :param repoid: The repository name or repository id |
|
295 | 295 | :type repoid: str or int |
|
296 | 296 | :param revision: revision for which listing should be done |
|
297 | 297 | :type revision: str |
|
298 | 298 | :param details: details can be 'basic|extended|full' full gives diff |
|
299 | 299 | info details like the diff itself, and number of changed files etc. |
|
300 | 300 | :type details: Optional(str) |
|
301 | 301 | |
|
302 | 302 | """ |
|
303 | 303 | repo = get_repo_or_error(repoid) |
|
304 | 304 | if not has_superadmin_permission(apiuser): |
|
305 | 305 | _perms = ( |
|
306 | 306 | 'repository.admin', 'repository.write', 'repository.read',) |
|
307 | 307 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
308 | 308 | |
|
309 | 309 | changes_details = Optional.extract(details) |
|
310 | 310 | _changes_details_types = ['basic', 'extended', 'full'] |
|
311 | 311 | if changes_details not in _changes_details_types: |
|
312 | 312 | raise JSONRPCError( |
|
313 | 313 | 'ret_type must be one of %s' % ( |
|
314 | 314 | ','.join(_changes_details_types))) |
|
315 | 315 | |
|
316 | 316 | pre_load = ['author', 'branch', 'date', 'message', 'parents', |
|
317 | 317 | 'status', '_commit', '_file_paths'] |
|
318 | 318 | |
|
319 | 319 | try: |
|
320 | 320 | cs = repo.get_commit(commit_id=revision, pre_load=pre_load) |
|
321 | 321 | except TypeError as e: |
|
322 | 322 | raise JSONRPCError(safe_str(e)) |
|
323 | 323 | _cs_json = cs.__json__() |
|
324 | 324 | _cs_json['diff'] = build_commit_data(cs, changes_details) |
|
325 | 325 | if changes_details == 'full': |
|
326 | 326 | _cs_json['refs'] = cs._get_refs() |
|
327 | 327 | return _cs_json |
|
328 | 328 | |
|
329 | 329 | |
|
330 | 330 | @jsonrpc_method() |
|
331 | 331 | def get_repo_changesets(request, apiuser, repoid, start_rev, limit, |
|
332 | 332 | details=Optional('basic')): |
|
333 | 333 | """ |
|
334 | 334 | Returns a set of commits limited by the number starting |
|
335 | 335 | from the `start_rev` option. |
|
336 | 336 | |
|
337 | 337 | Additional parameters define the amount of details returned by this |
|
338 | 338 | function. |
|
339 | 339 | |
|
340 | 340 | This command can only be run using an |authtoken| with admin rights, |
|
341 | 341 | or users with at least read rights to |repos|. |
|
342 | 342 | |
|
343 | 343 | :param apiuser: This is filled automatically from the |authtoken|. |
|
344 | 344 | :type apiuser: AuthUser |
|
345 | 345 | :param repoid: The repository name or repository ID. |
|
346 | 346 | :type repoid: str or int |
|
347 | 347 | :param start_rev: The starting revision from where to get changesets. |
|
348 | 348 | :type start_rev: str |
|
349 | 349 | :param limit: Limit the number of commits to this amount |
|
350 | 350 | :type limit: str or int |
|
351 | 351 | :param details: Set the level of detail returned. Valid option are: |
|
352 | 352 | ``basic``, ``extended`` and ``full``. |
|
353 | 353 | :type details: Optional(str) |
|
354 | 354 | |
|
355 | 355 | .. note:: |
|
356 | 356 | |
|
357 | 357 | Setting the parameter `details` to the value ``full`` is extensive |
|
358 | 358 | and returns details like the diff itself, and the number |
|
359 | 359 | of changed files. |
|
360 | 360 | |
|
361 | 361 | """ |
|
362 | 362 | repo = get_repo_or_error(repoid) |
|
363 | 363 | if not has_superadmin_permission(apiuser): |
|
364 | 364 | _perms = ( |
|
365 | 365 | 'repository.admin', 'repository.write', 'repository.read',) |
|
366 | 366 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
367 | 367 | |
|
368 | 368 | changes_details = Optional.extract(details) |
|
369 | 369 | _changes_details_types = ['basic', 'extended', 'full'] |
|
370 | 370 | if changes_details not in _changes_details_types: |
|
371 | 371 | raise JSONRPCError( |
|
372 | 372 | 'ret_type must be one of %s' % ( |
|
373 | 373 | ','.join(_changes_details_types))) |
|
374 | 374 | |
|
375 | 375 | limit = int(limit) |
|
376 | 376 | pre_load = ['author', 'branch', 'date', 'message', 'parents', |
|
377 | 377 | 'status', '_commit', '_file_paths'] |
|
378 | 378 | |
|
379 | 379 | vcs_repo = repo.scm_instance() |
|
380 | 380 | # SVN needs a special case to distinguish its index and commit id |
|
381 | 381 | if vcs_repo and vcs_repo.alias == 'svn' and (start_rev == '0'): |
|
382 | 382 | start_rev = vcs_repo.commit_ids[0] |
|
383 | 383 | |
|
384 | 384 | try: |
|
385 | 385 | commits = vcs_repo.get_commits( |
|
386 | 386 | start_id=start_rev, pre_load=pre_load, translate_tags=False) |
|
387 | 387 | except TypeError as e: |
|
388 | 388 | raise JSONRPCError(safe_str(e)) |
|
389 | 389 | except Exception: |
|
390 | 390 | log.exception('Fetching of commits failed') |
|
391 | 391 | raise JSONRPCError('Error occurred during commit fetching') |
|
392 | 392 | |
|
393 | 393 | ret = [] |
|
394 | 394 | for cnt, commit in enumerate(commits): |
|
395 | 395 | if cnt >= limit != -1: |
|
396 | 396 | break |
|
397 | 397 | _cs_json = commit.__json__() |
|
398 | 398 | _cs_json['diff'] = build_commit_data(commit, changes_details) |
|
399 | 399 | if changes_details == 'full': |
|
400 | 400 | _cs_json['refs'] = { |
|
401 | 401 | 'branches': [commit.branch], |
|
402 | 402 | 'bookmarks': getattr(commit, 'bookmarks', []), |
|
403 | 403 | 'tags': commit.tags |
|
404 | 404 | } |
|
405 | 405 | ret.append(_cs_json) |
|
406 | 406 | return ret |
|
407 | 407 | |
|
408 | 408 | |
|
409 | 409 | @jsonrpc_method() |
|
410 | 410 | def get_repo_nodes(request, apiuser, repoid, revision, root_path, |
|
411 | 411 | ret_type=Optional('all'), details=Optional('basic'), |
|
412 | 412 | max_file_bytes=Optional(None)): |
|
413 | 413 | """ |
|
414 | 414 | Returns a list of nodes and children in a flat list for a given |
|
415 | 415 | path at given revision. |
|
416 | 416 | |
|
417 | 417 | It's possible to specify ret_type to show only `files` or `dirs`. |
|
418 | 418 | |
|
419 | 419 | This command can only be run using an |authtoken| with admin rights, |
|
420 | 420 | or users with at least read rights to |repos|. |
|
421 | 421 | |
|
422 | 422 | :param apiuser: This is filled automatically from the |authtoken|. |
|
423 | 423 | :type apiuser: AuthUser |
|
424 | 424 | :param repoid: The repository name or repository ID. |
|
425 | 425 | :type repoid: str or int |
|
426 | 426 | :param revision: The revision for which listing should be done. |
|
427 | 427 | :type revision: str |
|
428 | 428 | :param root_path: The path from which to start displaying. |
|
429 | 429 | :type root_path: str |
|
430 | 430 | :param ret_type: Set the return type. Valid options are |
|
431 | 431 | ``all`` (default), ``files`` and ``dirs``. |
|
432 | 432 | :type ret_type: Optional(str) |
|
433 | 433 | :param details: Returns extended information about nodes, such as |
|
434 | 434 | md5, binary, and or content. |
|
435 | 435 | The valid options are ``basic`` and ``full``. |
|
436 | 436 | :type details: Optional(str) |
|
437 | 437 | :param max_file_bytes: Only return file content under this file size bytes |
|
438 | 438 | :type details: Optional(int) |
|
439 | 439 | |
|
440 | 440 | Example output: |
|
441 | 441 | |
|
442 | 442 | .. code-block:: bash |
|
443 | 443 | |
|
444 | 444 | id : <id_given_in_input> |
|
445 | 445 | result: [ |
|
446 | 446 | { |
|
447 | 447 | "binary": false, |
|
448 | 448 | "content": "File line", |
|
449 | 449 | "extension": "md", |
|
450 | 450 | "lines": 2, |
|
451 | 451 | "md5": "059fa5d29b19c0657e384749480f6422", |
|
452 | 452 | "mimetype": "text/x-minidsrc", |
|
453 | 453 | "name": "file.md", |
|
454 | 454 | "size": 580, |
|
455 | 455 | "type": "file" |
|
456 | 456 | }, |
|
457 | 457 | ... |
|
458 | 458 | ] |
|
459 | 459 | error: null |
|
460 | 460 | """ |
|
461 | 461 | |
|
462 | 462 | repo = get_repo_or_error(repoid) |
|
463 | 463 | if not has_superadmin_permission(apiuser): |
|
464 | 464 | _perms = ('repository.admin', 'repository.write', 'repository.read',) |
|
465 | 465 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
466 | 466 | |
|
467 | 467 | ret_type = Optional.extract(ret_type) |
|
468 | 468 | details = Optional.extract(details) |
|
469 | 469 | _extended_types = ['basic', 'full'] |
|
470 | 470 | if details not in _extended_types: |
|
471 | 471 | raise JSONRPCError('ret_type must be one of %s' % (','.join(_extended_types))) |
|
472 | 472 | extended_info = False |
|
473 | 473 | content = False |
|
474 | 474 | if details == 'basic': |
|
475 | 475 | extended_info = True |
|
476 | 476 | |
|
477 | 477 | if details == 'full': |
|
478 | 478 | extended_info = content = True |
|
479 | 479 | |
|
480 | 480 | _map = {} |
|
481 | 481 | try: |
|
482 | 482 | # check if repo is not empty by any chance, skip quicker if it is. |
|
483 | 483 | _scm = repo.scm_instance() |
|
484 | 484 | if _scm.is_empty(): |
|
485 | 485 | return [] |
|
486 | 486 | |
|
487 | 487 | _d, _f = ScmModel().get_nodes( |
|
488 | 488 | repo, revision, root_path, flat=False, |
|
489 | 489 | extended_info=extended_info, content=content, |
|
490 | 490 | max_file_bytes=max_file_bytes) |
|
491 | 491 | _map = { |
|
492 | 492 | 'all': _d + _f, |
|
493 | 493 | 'files': _f, |
|
494 | 494 | 'dirs': _d, |
|
495 | 495 | } |
|
496 | 496 | return _map[ret_type] |
|
497 | 497 | except KeyError: |
|
498 | 498 | raise JSONRPCError( |
|
499 | 499 | 'ret_type must be one of %s' % (','.join(sorted(_map.keys())))) |
|
500 | 500 | except Exception: |
|
501 | 501 | log.exception("Exception occurred while trying to get repo nodes") |
|
502 | 502 | raise JSONRPCError( |
|
503 | 503 | 'failed to get repo: `%s` nodes' % repo.repo_name |
|
504 | 504 | ) |
|
505 | 505 | |
|
506 | 506 | |
|
507 | 507 | @jsonrpc_method() |
|
508 | 508 | def get_repo_file(request, apiuser, repoid, commit_id, file_path, |
|
509 | 509 | max_file_bytes=Optional(None), details=Optional('basic'), |
|
510 | 510 | cache=Optional(True)): |
|
511 | 511 | """ |
|
512 | 512 | Returns a single file from repository at given revision. |
|
513 | 513 | |
|
514 | 514 | This command can only be run using an |authtoken| with admin rights, |
|
515 | 515 | or users with at least read rights to |repos|. |
|
516 | 516 | |
|
517 | 517 | :param apiuser: This is filled automatically from the |authtoken|. |
|
518 | 518 | :type apiuser: AuthUser |
|
519 | 519 | :param repoid: The repository name or repository ID. |
|
520 | 520 | :type repoid: str or int |
|
521 | 521 | :param commit_id: The revision for which listing should be done. |
|
522 | 522 | :type commit_id: str |
|
523 | 523 | :param file_path: The path from which to start displaying. |
|
524 | 524 | :type file_path: str |
|
525 | 525 | :param details: Returns different set of information about nodes. |
|
526 | 526 | The valid options are ``minimal`` ``basic`` and ``full``. |
|
527 | 527 | :type details: Optional(str) |
|
528 | 528 | :param max_file_bytes: Only return file content under this file size bytes |
|
529 | 529 | :type max_file_bytes: Optional(int) |
|
530 | 530 | :param cache: Use internal caches for fetching files. If disabled fetching |
|
531 | 531 | files is slower but more memory efficient |
|
532 | 532 | :type cache: Optional(bool) |
|
533 | 533 | |
|
534 | 534 | Example output: |
|
535 | 535 | |
|
536 | 536 | .. code-block:: bash |
|
537 | 537 | |
|
538 | 538 | id : <id_given_in_input> |
|
539 | 539 | result: { |
|
540 | 540 | "binary": false, |
|
541 | 541 | "extension": "py", |
|
542 | 542 | "lines": 35, |
|
543 | 543 | "content": "....", |
|
544 | 544 | "md5": "76318336366b0f17ee249e11b0c99c41", |
|
545 | 545 | "mimetype": "text/x-python", |
|
546 | 546 | "name": "python.py", |
|
547 | 547 | "size": 817, |
|
548 | 548 | "type": "file", |
|
549 | 549 | } |
|
550 | 550 | error: null |
|
551 | 551 | """ |
|
552 | 552 | |
|
553 | 553 | repo = get_repo_or_error(repoid) |
|
554 | 554 | if not has_superadmin_permission(apiuser): |
|
555 | 555 | _perms = ('repository.admin', 'repository.write', 'repository.read',) |
|
556 | 556 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
557 | 557 | |
|
558 | 558 | cache = Optional.extract(cache, binary=True) |
|
559 | 559 | details = Optional.extract(details) |
|
560 | 560 | _extended_types = ['minimal', 'minimal+search', 'basic', 'full'] |
|
561 | 561 | if details not in _extended_types: |
|
562 | 562 | raise JSONRPCError( |
|
563 | 563 | 'ret_type must be one of %s, got %s' % (','.join(_extended_types)), details) |
|
564 | 564 | extended_info = False |
|
565 | 565 | content = False |
|
566 | 566 | |
|
567 | 567 | if details == 'minimal': |
|
568 | 568 | extended_info = False |
|
569 | 569 | |
|
570 | 570 | elif details == 'basic': |
|
571 | 571 | extended_info = True |
|
572 | 572 | |
|
573 | 573 | elif details == 'full': |
|
574 | 574 | extended_info = content = True |
|
575 | 575 | |
|
576 | 576 | try: |
|
577 | 577 | # check if repo is not empty by any chance, skip quicker if it is. |
|
578 | 578 | _scm = repo.scm_instance() |
|
579 | 579 | if _scm.is_empty(): |
|
580 | 580 | return None |
|
581 | 581 | |
|
582 | 582 | node = ScmModel().get_node( |
|
583 | 583 | repo, commit_id, file_path, extended_info=extended_info, |
|
584 | 584 | content=content, max_file_bytes=max_file_bytes, cache=cache) |
|
585 | 585 | except NodeDoesNotExistError: |
|
586 | 586 | raise JSONRPCError('There is no file in repo: `{}` at path `{}` for commit: `{}`'.format( |
|
587 | 587 | repo.repo_name, file_path, commit_id)) |
|
588 | 588 | except Exception: |
|
589 | 589 | log.exception("Exception occurred while trying to get repo %s file", |
|
590 | 590 | repo.repo_name) |
|
591 | 591 | raise JSONRPCError('failed to get repo: `{}` file at path {}'.format( |
|
592 | 592 | repo.repo_name, file_path)) |
|
593 | 593 | |
|
594 | 594 | return node |
|
595 | 595 | |
|
596 | 596 | |
|
597 | 597 | @jsonrpc_method() |
|
598 | 598 | def get_repo_fts_tree(request, apiuser, repoid, commit_id, root_path): |
|
599 | 599 | """ |
|
600 | 600 | Returns a list of tree nodes for path at given revision. This api is built |
|
601 | 601 | strictly for usage in full text search building, and shouldn't be consumed |
|
602 | 602 | |
|
603 | 603 | This command can only be run using an |authtoken| with admin rights, |
|
604 | 604 | or users with at least read rights to |repos|. |
|
605 | 605 | |
|
606 | 606 | """ |
|
607 | 607 | |
|
608 | 608 | repo = get_repo_or_error(repoid) |
|
609 | 609 | if not has_superadmin_permission(apiuser): |
|
610 | 610 | _perms = ('repository.admin', 'repository.write', 'repository.read',) |
|
611 | 611 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
612 | 612 | |
|
613 | 613 | repo_id = repo.repo_id |
|
614 | 614 | cache_seconds = safe_int(rhodecode.CONFIG.get('rc_cache.cache_repo.expiration_time')) |
|
615 | 615 | cache_on = cache_seconds > 0 |
|
616 | 616 | |
|
617 | 617 | cache_namespace_uid = 'cache_repo.{}'.format(repo_id) |
|
618 | 618 | region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) |
|
619 | 619 | |
|
620 | 620 | def compute_fts_tree(cache_ver, repo_id, commit_id, root_path): |
|
621 | 621 | return ScmModel().get_fts_data(repo_id, commit_id, root_path) |
|
622 | 622 | |
|
623 | 623 | try: |
|
624 | 624 | # check if repo is not empty by any chance, skip quicker if it is. |
|
625 | 625 | _scm = repo.scm_instance() |
|
626 | 626 | if _scm.is_empty(): |
|
627 | 627 | return [] |
|
628 | 628 | except RepositoryError: |
|
629 | 629 | log.exception("Exception occurred while trying to get repo nodes") |
|
630 | 630 | raise JSONRPCError('failed to get repo: `%s` nodes' % repo.repo_name) |
|
631 | 631 | |
|
632 | 632 | try: |
|
633 | 633 | # we need to resolve commit_id to a FULL sha for cache to work correctly. |
|
634 | 634 | # sending 'master' is a pointer that needs to be translated to current commit. |
|
635 | 635 | commit_id = _scm.get_commit(commit_id=commit_id).raw_id |
|
636 | 636 | log.debug( |
|
637 | 637 | 'Computing FTS REPO TREE for repo_id %s commit_id `%s` ' |
|
638 | 638 | 'with caching: %s[TTL: %ss]' % ( |
|
639 | 639 | repo_id, commit_id, cache_on, cache_seconds or 0)) |
|
640 | 640 | |
|
641 | 641 | tree_files = compute_fts_tree(rc_cache.FILE_TREE_CACHE_VER, repo_id, commit_id, root_path) |
|
642 | 642 | return tree_files |
|
643 | 643 | |
|
644 | 644 | except Exception: |
|
645 | 645 | log.exception("Exception occurred while trying to get repo nodes") |
|
646 | 646 | raise JSONRPCError('failed to get repo: `%s` nodes' % repo.repo_name) |
|
647 | 647 | |
|
648 | 648 | |
|
649 | 649 | @jsonrpc_method() |
|
650 | 650 | def get_repo_refs(request, apiuser, repoid): |
|
651 | 651 | """ |
|
652 | 652 | Returns a dictionary of current references. It returns |
|
653 | 653 | bookmarks, branches, closed_branches, and tags for given repository |
|
654 | 654 | |
|
655 | 655 | It's possible to specify ret_type to show only `files` or `dirs`. |
|
656 | 656 | |
|
657 | 657 | This command can only be run using an |authtoken| with admin rights, |
|
658 | 658 | or users with at least read rights to |repos|. |
|
659 | 659 | |
|
660 | 660 | :param apiuser: This is filled automatically from the |authtoken|. |
|
661 | 661 | :type apiuser: AuthUser |
|
662 | 662 | :param repoid: The repository name or repository ID. |
|
663 | 663 | :type repoid: str or int |
|
664 | 664 | |
|
665 | 665 | Example output: |
|
666 | 666 | |
|
667 | 667 | .. code-block:: bash |
|
668 | 668 | |
|
669 | 669 | id : <id_given_in_input> |
|
670 | 670 | "result": { |
|
671 | 671 | "bookmarks": { |
|
672 | 672 | "dev": "5611d30200f4040ba2ab4f3d64e5b06408a02188", |
|
673 | 673 | "master": "367f590445081d8ec8c2ea0456e73ae1f1c3d6cf" |
|
674 | 674 | }, |
|
675 | 675 | "branches": { |
|
676 | 676 | "default": "5611d30200f4040ba2ab4f3d64e5b06408a02188", |
|
677 | 677 | "stable": "367f590445081d8ec8c2ea0456e73ae1f1c3d6cf" |
|
678 | 678 | }, |
|
679 | 679 | "branches_closed": {}, |
|
680 | 680 | "tags": { |
|
681 | 681 | "tip": "5611d30200f4040ba2ab4f3d64e5b06408a02188", |
|
682 | 682 | "v4.4.0": "1232313f9e6adac5ce5399c2a891dc1e72b79022", |
|
683 | 683 | "v4.4.1": "cbb9f1d329ae5768379cdec55a62ebdd546c4e27", |
|
684 | 684 | "v4.4.2": "24ffe44a27fcd1c5b6936144e176b9f6dd2f3a17", |
|
685 | 685 | } |
|
686 | 686 | } |
|
687 | 687 | error: null |
|
688 | 688 | """ |
|
689 | 689 | |
|
690 | 690 | repo = get_repo_or_error(repoid) |
|
691 | 691 | if not has_superadmin_permission(apiuser): |
|
692 | 692 | _perms = ('repository.admin', 'repository.write', 'repository.read',) |
|
693 | 693 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
694 | 694 | |
|
695 | 695 | try: |
|
696 | 696 | # check if repo is not empty by any chance, skip quicker if it is. |
|
697 | 697 | vcs_instance = repo.scm_instance() |
|
698 | 698 | refs = vcs_instance.refs() |
|
699 | 699 | return refs |
|
700 | 700 | except Exception: |
|
701 | 701 | log.exception("Exception occurred while trying to get repo refs") |
|
702 | 702 | raise JSONRPCError( |
|
703 | 703 | 'failed to get repo: `%s` references' % repo.repo_name |
|
704 | 704 | ) |
|
705 | 705 | |
|
706 | 706 | |
|
707 | 707 | @jsonrpc_method() |
|
708 | 708 | def create_repo( |
|
709 | 709 | request, apiuser, repo_name, repo_type, |
|
710 | 710 | owner=Optional(OAttr('apiuser')), |
|
711 | 711 | description=Optional(''), |
|
712 | 712 | private=Optional(False), |
|
713 | 713 | clone_uri=Optional(None), |
|
714 | 714 | push_uri=Optional(None), |
|
715 | 715 | landing_rev=Optional(None), |
|
716 | 716 | enable_statistics=Optional(False), |
|
717 | 717 | enable_locking=Optional(False), |
|
718 | 718 | enable_downloads=Optional(False), |
|
719 | 719 | copy_permissions=Optional(False)): |
|
720 | 720 | """ |
|
721 | 721 | Creates a repository. |
|
722 | 722 | |
|
723 | 723 | * If the repository name contains "/", repository will be created inside |
|
724 | 724 | a repository group or nested repository groups |
|
725 | 725 | |
|
726 | 726 | For example "foo/bar/repo1" will create |repo| called "repo1" inside |
|
727 | 727 | group "foo/bar". You have to have permissions to access and write to |
|
728 | 728 | the last repository group ("bar" in this example) |
|
729 | 729 | |
|
730 | 730 | This command can only be run using an |authtoken| with at least |
|
731 | 731 | permissions to create repositories, or write permissions to |
|
732 | 732 | parent repository groups. |
|
733 | 733 | |
|
734 | 734 | :param apiuser: This is filled automatically from the |authtoken|. |
|
735 | 735 | :type apiuser: AuthUser |
|
736 | 736 | :param repo_name: Set the repository name. |
|
737 | 737 | :type repo_name: str |
|
738 | 738 | :param repo_type: Set the repository type; 'hg','git', or 'svn'. |
|
739 | 739 | :type repo_type: str |
|
740 | 740 | :param owner: user_id or username |
|
741 | 741 | :type owner: Optional(str) |
|
742 | 742 | :param description: Set the repository description. |
|
743 | 743 | :type description: Optional(str) |
|
744 | 744 | :param private: set repository as private |
|
745 | 745 | :type private: bool |
|
746 | 746 | :param clone_uri: set clone_uri |
|
747 | 747 | :type clone_uri: str |
|
748 | 748 | :param push_uri: set push_uri |
|
749 | 749 | :type push_uri: str |
|
750 | 750 | :param landing_rev: <rev_type>:<rev>, e.g branch:default, book:dev, rev:abcd |
|
751 | 751 | :type landing_rev: str |
|
752 | 752 | :param enable_locking: |
|
753 | 753 | :type enable_locking: bool |
|
754 | 754 | :param enable_downloads: |
|
755 | 755 | :type enable_downloads: bool |
|
756 | 756 | :param enable_statistics: |
|
757 | 757 | :type enable_statistics: bool |
|
758 | 758 | :param copy_permissions: Copy permission from group in which the |
|
759 | 759 | repository is being created. |
|
760 | 760 | :type copy_permissions: bool |
|
761 | 761 | |
|
762 | 762 | |
|
763 | 763 | Example output: |
|
764 | 764 | |
|
765 | 765 | .. code-block:: bash |
|
766 | 766 | |
|
767 | 767 | id : <id_given_in_input> |
|
768 | 768 | result: { |
|
769 | 769 | "msg": "Created new repository `<reponame>`", |
|
770 | 770 | "success": true, |
|
771 | 771 | "task": "<celery task id or None if done sync>" |
|
772 | 772 | } |
|
773 | 773 | error: null |
|
774 | 774 | |
|
775 | 775 | |
|
776 | 776 | Example error output: |
|
777 | 777 | |
|
778 | 778 | .. code-block:: bash |
|
779 | 779 | |
|
780 | 780 | id : <id_given_in_input> |
|
781 | 781 | result : null |
|
782 | 782 | error : { |
|
783 | 783 | 'failed to create repository `<repo_name>`' |
|
784 | 784 | } |
|
785 | 785 | |
|
786 | 786 | """ |
|
787 | 787 | |
|
788 | 788 | owner = validate_set_owner_permissions(apiuser, owner) |
|
789 | 789 | |
|
790 | 790 | description = Optional.extract(description) |
|
791 | 791 | copy_permissions = Optional.extract(copy_permissions) |
|
792 | 792 | clone_uri = Optional.extract(clone_uri) |
|
793 | 793 | push_uri = Optional.extract(push_uri) |
|
794 | 794 | |
|
795 | 795 | defs = SettingsModel().get_default_repo_settings(strip_prefix=True) |
|
796 | 796 | if isinstance(private, Optional): |
|
797 | 797 | private = defs.get('repo_private') or Optional.extract(private) |
|
798 | 798 | if isinstance(repo_type, Optional): |
|
799 | 799 | repo_type = defs.get('repo_type') |
|
800 | 800 | if isinstance(enable_statistics, Optional): |
|
801 | 801 | enable_statistics = defs.get('repo_enable_statistics') |
|
802 | 802 | if isinstance(enable_locking, Optional): |
|
803 | 803 | enable_locking = defs.get('repo_enable_locking') |
|
804 | 804 | if isinstance(enable_downloads, Optional): |
|
805 | 805 | enable_downloads = defs.get('repo_enable_downloads') |
|
806 | 806 | |
|
807 | 807 | landing_ref, _label = ScmModel.backend_landing_ref(repo_type) |
|
808 | 808 | ref_choices, _labels = ScmModel().get_repo_landing_revs(request.translate) |
|
809 | 809 | ref_choices = list(set(ref_choices + [landing_ref])) |
|
810 | 810 | |
|
811 | 811 | landing_commit_ref = Optional.extract(landing_rev) or landing_ref |
|
812 | 812 | |
|
813 | 813 | schema = repo_schema.RepoSchema().bind( |
|
814 | 814 | repo_type_options=rhodecode.BACKENDS.keys(), |
|
815 | 815 | repo_ref_options=ref_choices, |
|
816 | 816 | repo_type=repo_type, |
|
817 | 817 | # user caller |
|
818 | 818 | user=apiuser) |
|
819 | 819 | |
|
820 | 820 | try: |
|
821 | 821 | schema_data = schema.deserialize(dict( |
|
822 | 822 | repo_name=repo_name, |
|
823 | 823 | repo_type=repo_type, |
|
824 | 824 | repo_owner=owner.username, |
|
825 | 825 | repo_description=description, |
|
826 | 826 | repo_landing_commit_ref=landing_commit_ref, |
|
827 | 827 | repo_clone_uri=clone_uri, |
|
828 | 828 | repo_push_uri=push_uri, |
|
829 | 829 | repo_private=private, |
|
830 | 830 | repo_copy_permissions=copy_permissions, |
|
831 | 831 | repo_enable_statistics=enable_statistics, |
|
832 | 832 | repo_enable_downloads=enable_downloads, |
|
833 | 833 | repo_enable_locking=enable_locking)) |
|
834 | 834 | except validation_schema.Invalid as err: |
|
835 | 835 | raise JSONRPCValidationError(colander_exc=err) |
|
836 | 836 | |
|
837 | 837 | try: |
|
838 | 838 | data = { |
|
839 | 839 | 'owner': owner, |
|
840 | 840 | 'repo_name': schema_data['repo_group']['repo_name_without_group'], |
|
841 | 841 | 'repo_name_full': schema_data['repo_name'], |
|
842 | 842 | 'repo_group': schema_data['repo_group']['repo_group_id'], |
|
843 | 843 | 'repo_type': schema_data['repo_type'], |
|
844 | 844 | 'repo_description': schema_data['repo_description'], |
|
845 | 845 | 'repo_private': schema_data['repo_private'], |
|
846 | 846 | 'clone_uri': schema_data['repo_clone_uri'], |
|
847 | 847 | 'push_uri': schema_data['repo_push_uri'], |
|
848 | 848 | 'repo_landing_rev': schema_data['repo_landing_commit_ref'], |
|
849 | 849 | 'enable_statistics': schema_data['repo_enable_statistics'], |
|
850 | 850 | 'enable_locking': schema_data['repo_enable_locking'], |
|
851 | 851 | 'enable_downloads': schema_data['repo_enable_downloads'], |
|
852 | 852 | 'repo_copy_permissions': schema_data['repo_copy_permissions'], |
|
853 | 853 | } |
|
854 | 854 | |
|
855 | 855 | task = RepoModel().create(form_data=data, cur_user=owner.user_id) |
|
856 | 856 | task_id = get_task_id(task) |
|
857 | 857 | # no commit, it's done in RepoModel, or async via celery |
|
858 | 858 | return { |
|
859 | 859 | 'msg': "Created new repository `%s`" % (schema_data['repo_name'],), |
|
860 | 860 | 'success': True, # cannot return the repo data here since fork |
|
861 | 861 | # can be done async |
|
862 | 862 | 'task': task_id |
|
863 | 863 | } |
|
864 | 864 | except Exception: |
|
865 | 865 | log.exception( |
|
866 | 866 | u"Exception while trying to create the repository %s", |
|
867 | 867 | schema_data['repo_name']) |
|
868 | 868 | raise JSONRPCError( |
|
869 | 869 | 'failed to create repository `%s`' % (schema_data['repo_name'],)) |
|
870 | 870 | |
|
871 | 871 | |
|
872 | 872 | @jsonrpc_method() |
|
873 | 873 | def add_field_to_repo(request, apiuser, repoid, key, label=Optional(''), |
|
874 | 874 | description=Optional('')): |
|
875 | 875 | """ |
|
876 | 876 | Adds an extra field to a repository. |
|
877 | 877 | |
|
878 | 878 | This command can only be run using an |authtoken| with at least |
|
879 | 879 | write permissions to the |repo|. |
|
880 | 880 | |
|
881 | 881 | :param apiuser: This is filled automatically from the |authtoken|. |
|
882 | 882 | :type apiuser: AuthUser |
|
883 | 883 | :param repoid: Set the repository name or repository id. |
|
884 | 884 | :type repoid: str or int |
|
885 | 885 | :param key: Create a unique field key for this repository. |
|
886 | 886 | :type key: str |
|
887 | 887 | :param label: |
|
888 | 888 | :type label: Optional(str) |
|
889 | 889 | :param description: |
|
890 | 890 | :type description: Optional(str) |
|
891 | 891 | """ |
|
892 | 892 | repo = get_repo_or_error(repoid) |
|
893 | 893 | if not has_superadmin_permission(apiuser): |
|
894 | 894 | _perms = ('repository.admin',) |
|
895 | 895 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
896 | 896 | |
|
897 | 897 | label = Optional.extract(label) or key |
|
898 | 898 | description = Optional.extract(description) |
|
899 | 899 | |
|
900 | 900 | field = RepositoryField.get_by_key_name(key, repo) |
|
901 | 901 | if field: |
|
902 | 902 | raise JSONRPCError('Field with key ' |
|
903 | 903 | '`%s` exists for repo `%s`' % (key, repoid)) |
|
904 | 904 | |
|
905 | 905 | try: |
|
906 | 906 | RepoModel().add_repo_field(repo, key, field_label=label, |
|
907 | 907 | field_desc=description) |
|
908 | 908 | Session().commit() |
|
909 | 909 | return { |
|
910 | 910 | 'msg': "Added new repository field `%s`" % (key,), |
|
911 | 911 | 'success': True, |
|
912 | 912 | } |
|
913 | 913 | except Exception: |
|
914 | 914 | log.exception("Exception occurred while trying to add field to repo") |
|
915 | 915 | raise JSONRPCError( |
|
916 | 916 | 'failed to create new field for repository `%s`' % (repoid,)) |
|
917 | 917 | |
|
918 | 918 | |
|
919 | 919 | @jsonrpc_method() |
|
920 | 920 | def remove_field_from_repo(request, apiuser, repoid, key): |
|
921 | 921 | """ |
|
922 | 922 | Removes an extra field from a repository. |
|
923 | 923 | |
|
924 | 924 | This command can only be run using an |authtoken| with at least |
|
925 | 925 | write permissions to the |repo|. |
|
926 | 926 | |
|
927 | 927 | :param apiuser: This is filled automatically from the |authtoken|. |
|
928 | 928 | :type apiuser: AuthUser |
|
929 | 929 | :param repoid: Set the repository name or repository ID. |
|
930 | 930 | :type repoid: str or int |
|
931 | 931 | :param key: Set the unique field key for this repository. |
|
932 | 932 | :type key: str |
|
933 | 933 | """ |
|
934 | 934 | |
|
935 | 935 | repo = get_repo_or_error(repoid) |
|
936 | 936 | if not has_superadmin_permission(apiuser): |
|
937 | 937 | _perms = ('repository.admin',) |
|
938 | 938 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
939 | 939 | |
|
940 | 940 | field = RepositoryField.get_by_key_name(key, repo) |
|
941 | 941 | if not field: |
|
942 | 942 | raise JSONRPCError('Field with key `%s` does not ' |
|
943 | 943 | 'exists for repo `%s`' % (key, repoid)) |
|
944 | 944 | |
|
945 | 945 | try: |
|
946 | 946 | RepoModel().delete_repo_field(repo, field_key=key) |
|
947 | 947 | Session().commit() |
|
948 | 948 | return { |
|
949 | 949 | 'msg': "Deleted repository field `%s`" % (key,), |
|
950 | 950 | 'success': True, |
|
951 | 951 | } |
|
952 | 952 | except Exception: |
|
953 | 953 | log.exception( |
|
954 | 954 | "Exception occurred while trying to delete field from repo") |
|
955 | 955 | raise JSONRPCError( |
|
956 | 956 | 'failed to delete field for repository `%s`' % (repoid,)) |
|
957 | 957 | |
|
958 | 958 | |
|
959 | 959 | @jsonrpc_method() |
|
960 | 960 | def update_repo( |
|
961 | 961 | request, apiuser, repoid, repo_name=Optional(None), |
|
962 | 962 | owner=Optional(OAttr('apiuser')), description=Optional(''), |
|
963 | 963 | private=Optional(False), |
|
964 | 964 | clone_uri=Optional(None), push_uri=Optional(None), |
|
965 | 965 | landing_rev=Optional(None), fork_of=Optional(None), |
|
966 | 966 | enable_statistics=Optional(False), |
|
967 | 967 | enable_locking=Optional(False), |
|
968 | 968 | enable_downloads=Optional(False), fields=Optional('')): |
|
969 | 969 | """ |
|
970 | 970 | Updates a repository with the given information. |
|
971 | 971 | |
|
972 | 972 | This command can only be run using an |authtoken| with at least |
|
973 | 973 | admin permissions to the |repo|. |
|
974 | 974 | |
|
975 | 975 | * If the repository name contains "/", repository will be updated |
|
976 | 976 | accordingly with a repository group or nested repository groups |
|
977 | 977 | |
|
978 | 978 | For example repoid=repo-test name="foo/bar/repo-test" will update |repo| |
|
979 | 979 | called "repo-test" and place it inside group "foo/bar". |
|
980 | 980 | You have to have permissions to access and write to the last repository |
|
981 | 981 | group ("bar" in this example) |
|
982 | 982 | |
|
983 | 983 | :param apiuser: This is filled automatically from the |authtoken|. |
|
984 | 984 | :type apiuser: AuthUser |
|
985 | 985 | :param repoid: repository name or repository ID. |
|
986 | 986 | :type repoid: str or int |
|
987 | 987 | :param repo_name: Update the |repo| name, including the |
|
988 | 988 | repository group it's in. |
|
989 | 989 | :type repo_name: str |
|
990 | 990 | :param owner: Set the |repo| owner. |
|
991 | 991 | :type owner: str |
|
992 | 992 | :param fork_of: Set the |repo| as fork of another |repo|. |
|
993 | 993 | :type fork_of: str |
|
994 | 994 | :param description: Update the |repo| description. |
|
995 | 995 | :type description: str |
|
996 | 996 | :param private: Set the |repo| as private. (True | False) |
|
997 | 997 | :type private: bool |
|
998 | 998 | :param clone_uri: Update the |repo| clone URI. |
|
999 | 999 | :type clone_uri: str |
|
1000 | 1000 | :param landing_rev: Set the |repo| landing revision. e.g branch:default, book:dev, rev:abcd |
|
1001 | 1001 | :type landing_rev: str |
|
1002 | 1002 | :param enable_statistics: Enable statistics on the |repo|, (True | False). |
|
1003 | 1003 | :type enable_statistics: bool |
|
1004 | 1004 | :param enable_locking: Enable |repo| locking. |
|
1005 | 1005 | :type enable_locking: bool |
|
1006 | 1006 | :param enable_downloads: Enable downloads from the |repo|, (True | False). |
|
1007 | 1007 | :type enable_downloads: bool |
|
1008 | 1008 | :param fields: Add extra fields to the |repo|. Use the following |
|
1009 | 1009 | example format: ``field_key=field_val,field_key2=fieldval2``. |
|
1010 | 1010 | Escape ', ' with \, |
|
1011 | 1011 | :type fields: str |
|
1012 | 1012 | """ |
|
1013 | 1013 | |
|
1014 | 1014 | repo = get_repo_or_error(repoid) |
|
1015 | 1015 | |
|
1016 | 1016 | include_secrets = False |
|
1017 | 1017 | if not has_superadmin_permission(apiuser): |
|
1018 | 1018 | validate_repo_permissions(apiuser, repoid, repo, ('repository.admin',)) |
|
1019 | 1019 | else: |
|
1020 | 1020 | include_secrets = True |
|
1021 | 1021 | |
|
1022 | 1022 | updates = dict( |
|
1023 | 1023 | repo_name=repo_name |
|
1024 | 1024 | if not isinstance(repo_name, Optional) else repo.repo_name, |
|
1025 | 1025 | |
|
1026 | 1026 | fork_id=fork_of |
|
1027 | 1027 | if not isinstance(fork_of, Optional) else repo.fork.repo_name if repo.fork else None, |
|
1028 | 1028 | |
|
1029 | 1029 | user=owner |
|
1030 | 1030 | if not isinstance(owner, Optional) else repo.user.username, |
|
1031 | 1031 | |
|
1032 | 1032 | repo_description=description |
|
1033 | 1033 | if not isinstance(description, Optional) else repo.description, |
|
1034 | 1034 | |
|
1035 | 1035 | repo_private=private |
|
1036 | 1036 | if not isinstance(private, Optional) else repo.private, |
|
1037 | 1037 | |
|
1038 | 1038 | clone_uri=clone_uri |
|
1039 | 1039 | if not isinstance(clone_uri, Optional) else repo.clone_uri, |
|
1040 | 1040 | |
|
1041 | 1041 | push_uri=push_uri |
|
1042 | 1042 | if not isinstance(push_uri, Optional) else repo.push_uri, |
|
1043 | 1043 | |
|
1044 | 1044 | repo_landing_rev=landing_rev |
|
1045 | 1045 | if not isinstance(landing_rev, Optional) else repo._landing_revision, |
|
1046 | 1046 | |
|
1047 | 1047 | repo_enable_statistics=enable_statistics |
|
1048 | 1048 | if not isinstance(enable_statistics, Optional) else repo.enable_statistics, |
|
1049 | 1049 | |
|
1050 | 1050 | repo_enable_locking=enable_locking |
|
1051 | 1051 | if not isinstance(enable_locking, Optional) else repo.enable_locking, |
|
1052 | 1052 | |
|
1053 | 1053 | repo_enable_downloads=enable_downloads |
|
1054 | 1054 | if not isinstance(enable_downloads, Optional) else repo.enable_downloads) |
|
1055 | 1055 | |
|
1056 | 1056 | landing_ref, _label = ScmModel.backend_landing_ref(repo.repo_type) |
|
1057 | 1057 | ref_choices, _labels = ScmModel().get_repo_landing_revs( |
|
1058 | 1058 | request.translate, repo=repo) |
|
1059 | 1059 | ref_choices = list(set(ref_choices + [landing_ref])) |
|
1060 | 1060 | |
|
1061 | 1061 | old_values = repo.get_api_data() |
|
1062 | 1062 | repo_type = repo.repo_type |
|
1063 | 1063 | schema = repo_schema.RepoSchema().bind( |
|
1064 | 1064 | repo_type_options=rhodecode.BACKENDS.keys(), |
|
1065 | 1065 | repo_ref_options=ref_choices, |
|
1066 | 1066 | repo_type=repo_type, |
|
1067 | 1067 | # user caller |
|
1068 | 1068 | user=apiuser, |
|
1069 | 1069 | old_values=old_values) |
|
1070 | 1070 | try: |
|
1071 | 1071 | schema_data = schema.deserialize(dict( |
|
1072 | 1072 | # we save old value, users cannot change type |
|
1073 | 1073 | repo_type=repo_type, |
|
1074 | 1074 | |
|
1075 | 1075 | repo_name=updates['repo_name'], |
|
1076 | 1076 | repo_owner=updates['user'], |
|
1077 | 1077 | repo_description=updates['repo_description'], |
|
1078 | 1078 | repo_clone_uri=updates['clone_uri'], |
|
1079 | 1079 | repo_push_uri=updates['push_uri'], |
|
1080 | 1080 | repo_fork_of=updates['fork_id'], |
|
1081 | 1081 | repo_private=updates['repo_private'], |
|
1082 | 1082 | repo_landing_commit_ref=updates['repo_landing_rev'], |
|
1083 | 1083 | repo_enable_statistics=updates['repo_enable_statistics'], |
|
1084 | 1084 | repo_enable_downloads=updates['repo_enable_downloads'], |
|
1085 | 1085 | repo_enable_locking=updates['repo_enable_locking'])) |
|
1086 | 1086 | except validation_schema.Invalid as err: |
|
1087 | 1087 | raise JSONRPCValidationError(colander_exc=err) |
|
1088 | 1088 | |
|
1089 | 1089 | # save validated data back into the updates dict |
|
1090 | 1090 | validated_updates = dict( |
|
1091 | 1091 | repo_name=schema_data['repo_group']['repo_name_without_group'], |
|
1092 | 1092 | repo_group=schema_data['repo_group']['repo_group_id'], |
|
1093 | 1093 | |
|
1094 | 1094 | user=schema_data['repo_owner'], |
|
1095 | 1095 | repo_description=schema_data['repo_description'], |
|
1096 | 1096 | repo_private=schema_data['repo_private'], |
|
1097 | 1097 | clone_uri=schema_data['repo_clone_uri'], |
|
1098 | 1098 | push_uri=schema_data['repo_push_uri'], |
|
1099 | 1099 | repo_landing_rev=schema_data['repo_landing_commit_ref'], |
|
1100 | 1100 | repo_enable_statistics=schema_data['repo_enable_statistics'], |
|
1101 | 1101 | repo_enable_locking=schema_data['repo_enable_locking'], |
|
1102 | 1102 | repo_enable_downloads=schema_data['repo_enable_downloads'], |
|
1103 | 1103 | ) |
|
1104 | 1104 | |
|
1105 | 1105 | if schema_data['repo_fork_of']: |
|
1106 | 1106 | fork_repo = get_repo_or_error(schema_data['repo_fork_of']) |
|
1107 | 1107 | validated_updates['fork_id'] = fork_repo.repo_id |
|
1108 | 1108 | |
|
1109 | 1109 | # extra fields |
|
1110 | 1110 | fields = parse_args(Optional.extract(fields), key_prefix='ex_') |
|
1111 | 1111 | if fields: |
|
1112 | 1112 | validated_updates.update(fields) |
|
1113 | 1113 | |
|
1114 | 1114 | try: |
|
1115 | 1115 | RepoModel().update(repo, **validated_updates) |
|
1116 | 1116 | audit_logger.store_api( |
|
1117 | 1117 | 'repo.edit', action_data={'old_data': old_values}, |
|
1118 | 1118 | user=apiuser, repo=repo) |
|
1119 | 1119 | Session().commit() |
|
1120 | 1120 | return { |
|
1121 | 1121 | 'msg': 'updated repo ID:%s %s' % (repo.repo_id, repo.repo_name), |
|
1122 | 1122 | 'repository': repo.get_api_data(include_secrets=include_secrets) |
|
1123 | 1123 | } |
|
1124 | 1124 | except Exception: |
|
1125 | 1125 | log.exception( |
|
1126 | 1126 | u"Exception while trying to update the repository %s", |
|
1127 | 1127 | repoid) |
|
1128 | 1128 | raise JSONRPCError('failed to update repo `%s`' % repoid) |
|
1129 | 1129 | |
|
1130 | 1130 | |
|
1131 | 1131 | @jsonrpc_method() |
|
1132 | 1132 | def fork_repo(request, apiuser, repoid, fork_name, |
|
1133 | 1133 | owner=Optional(OAttr('apiuser')), |
|
1134 | 1134 | description=Optional(''), |
|
1135 | 1135 | private=Optional(False), |
|
1136 | 1136 | clone_uri=Optional(None), |
|
1137 | 1137 | landing_rev=Optional(None), |
|
1138 | 1138 | copy_permissions=Optional(False)): |
|
1139 | 1139 | """ |
|
1140 | 1140 | Creates a fork of the specified |repo|. |
|
1141 | 1141 | |
|
1142 | 1142 | * If the fork_name contains "/", fork will be created inside |
|
1143 | 1143 | a repository group or nested repository groups |
|
1144 | 1144 | |
|
1145 | 1145 | For example "foo/bar/fork-repo" will create fork called "fork-repo" |
|
1146 | 1146 | inside group "foo/bar". You have to have permissions to access and |
|
1147 | 1147 | write to the last repository group ("bar" in this example) |
|
1148 | 1148 | |
|
1149 | 1149 | This command can only be run using an |authtoken| with minimum |
|
1150 | 1150 | read permissions of the forked repo, create fork permissions for an user. |
|
1151 | 1151 | |
|
1152 | 1152 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1153 | 1153 | :type apiuser: AuthUser |
|
1154 | 1154 | :param repoid: Set repository name or repository ID. |
|
1155 | 1155 | :type repoid: str or int |
|
1156 | 1156 | :param fork_name: Set the fork name, including it's repository group membership. |
|
1157 | 1157 | :type fork_name: str |
|
1158 | 1158 | :param owner: Set the fork owner. |
|
1159 | 1159 | :type owner: str |
|
1160 | 1160 | :param description: Set the fork description. |
|
1161 | 1161 | :type description: str |
|
1162 | 1162 | :param copy_permissions: Copy permissions from parent |repo|. The |
|
1163 | 1163 | default is False. |
|
1164 | 1164 | :type copy_permissions: bool |
|
1165 | 1165 | :param private: Make the fork private. The default is False. |
|
1166 | 1166 | :type private: bool |
|
1167 | 1167 | :param landing_rev: Set the landing revision. E.g branch:default, book:dev, rev:abcd |
|
1168 | 1168 | |
|
1169 | 1169 | Example output: |
|
1170 | 1170 | |
|
1171 | 1171 | .. code-block:: bash |
|
1172 | 1172 | |
|
1173 | 1173 | id : <id_for_response> |
|
1174 | 1174 | api_key : "<api_key>" |
|
1175 | 1175 | args: { |
|
1176 | 1176 | "repoid" : "<reponame or repo_id>", |
|
1177 | 1177 | "fork_name": "<forkname>", |
|
1178 | 1178 | "owner": "<username or user_id = Optional(=apiuser)>", |
|
1179 | 1179 | "description": "<description>", |
|
1180 | 1180 | "copy_permissions": "<bool>", |
|
1181 | 1181 | "private": "<bool>", |
|
1182 | 1182 | "landing_rev": "<landing_rev>" |
|
1183 | 1183 | } |
|
1184 | 1184 | |
|
1185 | 1185 | Example error output: |
|
1186 | 1186 | |
|
1187 | 1187 | .. code-block:: bash |
|
1188 | 1188 | |
|
1189 | 1189 | id : <id_given_in_input> |
|
1190 | 1190 | result: { |
|
1191 | 1191 | "msg": "Created fork of `<reponame>` as `<forkname>`", |
|
1192 | 1192 | "success": true, |
|
1193 | 1193 | "task": "<celery task id or None if done sync>" |
|
1194 | 1194 | } |
|
1195 | 1195 | error: null |
|
1196 | 1196 | |
|
1197 | 1197 | """ |
|
1198 | 1198 | |
|
1199 | 1199 | repo = get_repo_or_error(repoid) |
|
1200 | 1200 | repo_name = repo.repo_name |
|
1201 | 1201 | |
|
1202 | 1202 | if not has_superadmin_permission(apiuser): |
|
1203 | 1203 | # check if we have at least read permission for |
|
1204 | 1204 | # this repo that we fork ! |
|
1205 | 1205 | _perms = ( |
|
1206 | 1206 | 'repository.admin', 'repository.write', 'repository.read') |
|
1207 | 1207 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1208 | 1208 | |
|
1209 | 1209 | # check if the regular user has at least fork permissions as well |
|
1210 | 1210 | if not HasPermissionAnyApi('hg.fork.repository')(user=apiuser): |
|
1211 | 1211 | raise JSONRPCForbidden() |
|
1212 | 1212 | |
|
1213 | 1213 | # check if user can set owner parameter |
|
1214 | 1214 | owner = validate_set_owner_permissions(apiuser, owner) |
|
1215 | 1215 | |
|
1216 | 1216 | description = Optional.extract(description) |
|
1217 | 1217 | copy_permissions = Optional.extract(copy_permissions) |
|
1218 | 1218 | clone_uri = Optional.extract(clone_uri) |
|
1219 | 1219 | |
|
1220 | 1220 | landing_ref, _label = ScmModel.backend_landing_ref(repo.repo_type) |
|
1221 | 1221 | ref_choices, _labels = ScmModel().get_repo_landing_revs(request.translate) |
|
1222 | 1222 | ref_choices = list(set(ref_choices + [landing_ref])) |
|
1223 | 1223 | landing_commit_ref = Optional.extract(landing_rev) or landing_ref |
|
1224 | 1224 | |
|
1225 | 1225 | private = Optional.extract(private) |
|
1226 | 1226 | |
|
1227 | 1227 | schema = repo_schema.RepoSchema().bind( |
|
1228 | 1228 | repo_type_options=rhodecode.BACKENDS.keys(), |
|
1229 | 1229 | repo_ref_options=ref_choices, |
|
1230 | 1230 | repo_type=repo.repo_type, |
|
1231 | 1231 | # user caller |
|
1232 | 1232 | user=apiuser) |
|
1233 | 1233 | |
|
1234 | 1234 | try: |
|
1235 | 1235 | schema_data = schema.deserialize(dict( |
|
1236 | 1236 | repo_name=fork_name, |
|
1237 | 1237 | repo_type=repo.repo_type, |
|
1238 | 1238 | repo_owner=owner.username, |
|
1239 | 1239 | repo_description=description, |
|
1240 | 1240 | repo_landing_commit_ref=landing_commit_ref, |
|
1241 | 1241 | repo_clone_uri=clone_uri, |
|
1242 | 1242 | repo_private=private, |
|
1243 | 1243 | repo_copy_permissions=copy_permissions)) |
|
1244 | 1244 | except validation_schema.Invalid as err: |
|
1245 | 1245 | raise JSONRPCValidationError(colander_exc=err) |
|
1246 | 1246 | |
|
1247 | 1247 | try: |
|
1248 | 1248 | data = { |
|
1249 | 1249 | 'fork_parent_id': repo.repo_id, |
|
1250 | 1250 | |
|
1251 | 1251 | 'repo_name': schema_data['repo_group']['repo_name_without_group'], |
|
1252 | 1252 | 'repo_name_full': schema_data['repo_name'], |
|
1253 | 1253 | 'repo_group': schema_data['repo_group']['repo_group_id'], |
|
1254 | 1254 | 'repo_type': schema_data['repo_type'], |
|
1255 | 1255 | 'description': schema_data['repo_description'], |
|
1256 | 1256 | 'private': schema_data['repo_private'], |
|
1257 | 1257 | 'copy_permissions': schema_data['repo_copy_permissions'], |
|
1258 | 1258 | 'landing_rev': schema_data['repo_landing_commit_ref'], |
|
1259 | 1259 | } |
|
1260 | 1260 | |
|
1261 | 1261 | task = RepoModel().create_fork(data, cur_user=owner.user_id) |
|
1262 | 1262 | # no commit, it's done in RepoModel, or async via celery |
|
1263 | 1263 | task_id = get_task_id(task) |
|
1264 | 1264 | |
|
1265 | 1265 | return { |
|
1266 | 1266 | 'msg': 'Created fork of `%s` as `%s`' % ( |
|
1267 | 1267 | repo.repo_name, schema_data['repo_name']), |
|
1268 | 1268 | 'success': True, # cannot return the repo data here since fork |
|
1269 | 1269 | # can be done async |
|
1270 | 1270 | 'task': task_id |
|
1271 | 1271 | } |
|
1272 | 1272 | except Exception: |
|
1273 | 1273 | log.exception( |
|
1274 | 1274 | u"Exception while trying to create fork %s", |
|
1275 | 1275 | schema_data['repo_name']) |
|
1276 | 1276 | raise JSONRPCError( |
|
1277 | 1277 | 'failed to fork repository `%s` as `%s`' % ( |
|
1278 | 1278 | repo_name, schema_data['repo_name'])) |
|
1279 | 1279 | |
|
1280 | 1280 | |
|
1281 | 1281 | @jsonrpc_method() |
|
1282 | 1282 | def delete_repo(request, apiuser, repoid, forks=Optional('')): |
|
1283 | 1283 | """ |
|
1284 | 1284 | Deletes a repository. |
|
1285 | 1285 | |
|
1286 | 1286 | * When the `forks` parameter is set it's possible to detach or delete |
|
1287 | 1287 | forks of deleted repository. |
|
1288 | 1288 | |
|
1289 | 1289 | This command can only be run using an |authtoken| with admin |
|
1290 | 1290 | permissions on the |repo|. |
|
1291 | 1291 | |
|
1292 | 1292 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1293 | 1293 | :type apiuser: AuthUser |
|
1294 | 1294 | :param repoid: Set the repository name or repository ID. |
|
1295 | 1295 | :type repoid: str or int |
|
1296 | 1296 | :param forks: Set to `detach` or `delete` forks from the |repo|. |
|
1297 | 1297 | :type forks: Optional(str) |
|
1298 | 1298 | |
|
1299 | 1299 | Example error output: |
|
1300 | 1300 | |
|
1301 | 1301 | .. code-block:: bash |
|
1302 | 1302 | |
|
1303 | 1303 | id : <id_given_in_input> |
|
1304 | 1304 | result: { |
|
1305 | 1305 | "msg": "Deleted repository `<reponame>`", |
|
1306 | 1306 | "success": true |
|
1307 | 1307 | } |
|
1308 | 1308 | error: null |
|
1309 | 1309 | """ |
|
1310 | 1310 | |
|
1311 | 1311 | repo = get_repo_or_error(repoid) |
|
1312 | 1312 | repo_name = repo.repo_name |
|
1313 | 1313 | if not has_superadmin_permission(apiuser): |
|
1314 | 1314 | _perms = ('repository.admin',) |
|
1315 | 1315 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1316 | 1316 | |
|
1317 | 1317 | try: |
|
1318 | 1318 | handle_forks = Optional.extract(forks) |
|
1319 | 1319 | _forks_msg = '' |
|
1320 | 1320 | _forks = [f for f in repo.forks] |
|
1321 | 1321 | if handle_forks == 'detach': |
|
1322 | 1322 | _forks_msg = ' ' + 'Detached %s forks' % len(_forks) |
|
1323 | 1323 | elif handle_forks == 'delete': |
|
1324 | 1324 | _forks_msg = ' ' + 'Deleted %s forks' % len(_forks) |
|
1325 | 1325 | elif _forks: |
|
1326 | 1326 | raise JSONRPCError( |
|
1327 | 1327 | 'Cannot delete `%s` it still contains attached forks' % |
|
1328 | 1328 | (repo.repo_name,) |
|
1329 | 1329 | ) |
|
1330 | 1330 | old_data = repo.get_api_data() |
|
1331 | 1331 | RepoModel().delete(repo, forks=forks) |
|
1332 | 1332 | |
|
1333 | 1333 | repo = audit_logger.RepoWrap(repo_id=None, |
|
1334 | 1334 | repo_name=repo.repo_name) |
|
1335 | 1335 | |
|
1336 | 1336 | audit_logger.store_api( |
|
1337 | 1337 | 'repo.delete', action_data={'old_data': old_data}, |
|
1338 | 1338 | user=apiuser, repo=repo) |
|
1339 | 1339 | |
|
1340 | 1340 | ScmModel().mark_for_invalidation(repo_name, delete=True) |
|
1341 | 1341 | Session().commit() |
|
1342 | 1342 | return { |
|
1343 | 1343 | 'msg': 'Deleted repository `%s`%s' % (repo_name, _forks_msg), |
|
1344 | 1344 | 'success': True |
|
1345 | 1345 | } |
|
1346 | 1346 | except Exception: |
|
1347 | 1347 | log.exception("Exception occurred while trying to delete repo") |
|
1348 | 1348 | raise JSONRPCError( |
|
1349 | 1349 | 'failed to delete repository `%s`' % (repo_name,) |
|
1350 | 1350 | ) |
|
1351 | 1351 | |
|
1352 | 1352 | |
|
1353 | 1353 | #TODO: marcink, change name ? |
|
1354 | 1354 | @jsonrpc_method() |
|
1355 | 1355 | def invalidate_cache(request, apiuser, repoid, delete_keys=Optional(False)): |
|
1356 | 1356 | """ |
|
1357 | 1357 | Invalidates the cache for the specified repository. |
|
1358 | 1358 | |
|
1359 | 1359 | This command can only be run using an |authtoken| with admin rights to |
|
1360 | 1360 | the specified repository. |
|
1361 | 1361 | |
|
1362 | 1362 | This command takes the following options: |
|
1363 | 1363 | |
|
1364 | 1364 | :param apiuser: This is filled automatically from |authtoken|. |
|
1365 | 1365 | :type apiuser: AuthUser |
|
1366 | 1366 | :param repoid: Sets the repository name or repository ID. |
|
1367 | 1367 | :type repoid: str or int |
|
1368 | 1368 | :param delete_keys: This deletes the invalidated keys instead of |
|
1369 | 1369 | just flagging them. |
|
1370 | 1370 | :type delete_keys: Optional(``True`` | ``False``) |
|
1371 | 1371 | |
|
1372 | 1372 | Example output: |
|
1373 | 1373 | |
|
1374 | 1374 | .. code-block:: bash |
|
1375 | 1375 | |
|
1376 | 1376 | id : <id_given_in_input> |
|
1377 | 1377 | result : { |
|
1378 | 1378 | 'msg': Cache for repository `<repository name>` was invalidated, |
|
1379 | 1379 | 'repository': <repository name> |
|
1380 | 1380 | } |
|
1381 | 1381 | error : null |
|
1382 | 1382 | |
|
1383 | 1383 | Example error output: |
|
1384 | 1384 | |
|
1385 | 1385 | .. code-block:: bash |
|
1386 | 1386 | |
|
1387 | 1387 | id : <id_given_in_input> |
|
1388 | 1388 | result : null |
|
1389 | 1389 | error : { |
|
1390 | 1390 | 'Error occurred during cache invalidation action' |
|
1391 | 1391 | } |
|
1392 | 1392 | |
|
1393 | 1393 | """ |
|
1394 | 1394 | |
|
1395 | 1395 | repo = get_repo_or_error(repoid) |
|
1396 | 1396 | if not has_superadmin_permission(apiuser): |
|
1397 | 1397 | _perms = ('repository.admin', 'repository.write',) |
|
1398 | 1398 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1399 | 1399 | |
|
1400 | 1400 | delete = Optional.extract(delete_keys) |
|
1401 | 1401 | try: |
|
1402 | 1402 | ScmModel().mark_for_invalidation(repo.repo_name, delete=delete) |
|
1403 | 1403 | return { |
|
1404 | 1404 | 'msg': 'Cache for repository `%s` was invalidated' % (repoid,), |
|
1405 | 1405 | 'repository': repo.repo_name |
|
1406 | 1406 | } |
|
1407 | 1407 | except Exception: |
|
1408 | 1408 | log.exception( |
|
1409 | 1409 | "Exception occurred while trying to invalidate repo cache") |
|
1410 | 1410 | raise JSONRPCError( |
|
1411 | 1411 | 'Error occurred during cache invalidation action' |
|
1412 | 1412 | ) |
|
1413 | 1413 | |
|
1414 | 1414 | |
|
1415 | 1415 | #TODO: marcink, change name ? |
|
1416 | 1416 | @jsonrpc_method() |
|
1417 | 1417 | def lock(request, apiuser, repoid, locked=Optional(None), |
|
1418 | 1418 | userid=Optional(OAttr('apiuser'))): |
|
1419 | 1419 | """ |
|
1420 | 1420 | Sets the lock state of the specified |repo| by the given user. |
|
1421 | 1421 | From more information, see :ref:`repo-locking`. |
|
1422 | 1422 | |
|
1423 | 1423 | * If the ``userid`` option is not set, the repository is locked to the |
|
1424 | 1424 | user who called the method. |
|
1425 | 1425 | * If the ``locked`` parameter is not set, the current lock state of the |
|
1426 | 1426 | repository is displayed. |
|
1427 | 1427 | |
|
1428 | 1428 | This command can only be run using an |authtoken| with admin rights to |
|
1429 | 1429 | the specified repository. |
|
1430 | 1430 | |
|
1431 | 1431 | This command takes the following options: |
|
1432 | 1432 | |
|
1433 | 1433 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1434 | 1434 | :type apiuser: AuthUser |
|
1435 | 1435 | :param repoid: Sets the repository name or repository ID. |
|
1436 | 1436 | :type repoid: str or int |
|
1437 | 1437 | :param locked: Sets the lock state. |
|
1438 | 1438 | :type locked: Optional(``True`` | ``False``) |
|
1439 | 1439 | :param userid: Set the repository lock to this user. |
|
1440 | 1440 | :type userid: Optional(str or int) |
|
1441 | 1441 | |
|
1442 | 1442 | Example error output: |
|
1443 | 1443 | |
|
1444 | 1444 | .. code-block:: bash |
|
1445 | 1445 | |
|
1446 | 1446 | id : <id_given_in_input> |
|
1447 | 1447 | result : { |
|
1448 | 1448 | 'repo': '<reponame>', |
|
1449 | 1449 | 'locked': <bool: lock state>, |
|
1450 | 1450 | 'locked_since': <int: lock timestamp>, |
|
1451 | 1451 | 'locked_by': <username of person who made the lock>, |
|
1452 | 1452 | 'lock_reason': <str: reason for locking>, |
|
1453 | 1453 | 'lock_state_changed': <bool: True if lock state has been changed in this request>, |
|
1454 | 1454 | 'msg': 'Repo `<reponame>` locked by `<username>` on <timestamp>.' |
|
1455 | 1455 | or |
|
1456 | 1456 | 'msg': 'Repo `<repository name>` not locked.' |
|
1457 | 1457 | or |
|
1458 | 1458 | 'msg': 'User `<user name>` set lock state for repo `<repository name>` to `<new lock state>`' |
|
1459 | 1459 | } |
|
1460 | 1460 | error : null |
|
1461 | 1461 | |
|
1462 | 1462 | Example error output: |
|
1463 | 1463 | |
|
1464 | 1464 | .. code-block:: bash |
|
1465 | 1465 | |
|
1466 | 1466 | id : <id_given_in_input> |
|
1467 | 1467 | result : null |
|
1468 | 1468 | error : { |
|
1469 | 1469 | 'Error occurred locking repository `<reponame>`' |
|
1470 | 1470 | } |
|
1471 | 1471 | """ |
|
1472 | 1472 | |
|
1473 | 1473 | repo = get_repo_or_error(repoid) |
|
1474 | 1474 | if not has_superadmin_permission(apiuser): |
|
1475 | 1475 | # check if we have at least write permission for this repo ! |
|
1476 | 1476 | _perms = ('repository.admin', 'repository.write',) |
|
1477 | 1477 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1478 | 1478 | |
|
1479 | 1479 | # make sure normal user does not pass someone else userid, |
|
1480 | 1480 | # he is not allowed to do that |
|
1481 | 1481 | if not isinstance(userid, Optional) and userid != apiuser.user_id: |
|
1482 | 1482 | raise JSONRPCError('userid is not the same as your user') |
|
1483 | 1483 | |
|
1484 | 1484 | if isinstance(userid, Optional): |
|
1485 | 1485 | userid = apiuser.user_id |
|
1486 | 1486 | |
|
1487 | 1487 | user = get_user_or_error(userid) |
|
1488 | 1488 | |
|
1489 | 1489 | if isinstance(locked, Optional): |
|
1490 | 1490 | lockobj = repo.locked |
|
1491 | 1491 | |
|
1492 | 1492 | if lockobj[0] is None: |
|
1493 | 1493 | _d = { |
|
1494 | 1494 | 'repo': repo.repo_name, |
|
1495 | 1495 | 'locked': False, |
|
1496 | 1496 | 'locked_since': None, |
|
1497 | 1497 | 'locked_by': None, |
|
1498 | 1498 | 'lock_reason': None, |
|
1499 | 1499 | 'lock_state_changed': False, |
|
1500 | 1500 | 'msg': 'Repo `%s` not locked.' % repo.repo_name |
|
1501 | 1501 | } |
|
1502 | 1502 | return _d |
|
1503 | 1503 | else: |
|
1504 | 1504 | _user_id, _time, _reason = lockobj |
|
1505 | 1505 | lock_user = get_user_or_error(userid) |
|
1506 | 1506 | _d = { |
|
1507 | 1507 | 'repo': repo.repo_name, |
|
1508 | 1508 | 'locked': True, |
|
1509 | 1509 | 'locked_since': _time, |
|
1510 | 1510 | 'locked_by': lock_user.username, |
|
1511 | 1511 | 'lock_reason': _reason, |
|
1512 | 1512 | 'lock_state_changed': False, |
|
1513 | 1513 | 'msg': ('Repo `%s` locked by `%s` on `%s`.' |
|
1514 | 1514 | % (repo.repo_name, lock_user.username, |
|
1515 | 1515 | json.dumps(time_to_datetime(_time)))) |
|
1516 | 1516 | } |
|
1517 | 1517 | return _d |
|
1518 | 1518 | |
|
1519 | 1519 | # force locked state through a flag |
|
1520 | 1520 | else: |
|
1521 | 1521 | locked = str2bool(locked) |
|
1522 | 1522 | lock_reason = Repository.LOCK_API |
|
1523 | 1523 | try: |
|
1524 | 1524 | if locked: |
|
1525 | 1525 | lock_time = time.time() |
|
1526 | 1526 | Repository.lock(repo, user.user_id, lock_time, lock_reason) |
|
1527 | 1527 | else: |
|
1528 | 1528 | lock_time = None |
|
1529 | 1529 | Repository.unlock(repo) |
|
1530 | 1530 | _d = { |
|
1531 | 1531 | 'repo': repo.repo_name, |
|
1532 | 1532 | 'locked': locked, |
|
1533 | 1533 | 'locked_since': lock_time, |
|
1534 | 1534 | 'locked_by': user.username, |
|
1535 | 1535 | 'lock_reason': lock_reason, |
|
1536 | 1536 | 'lock_state_changed': True, |
|
1537 | 1537 | 'msg': ('User `%s` set lock state for repo `%s` to `%s`' |
|
1538 | 1538 | % (user.username, repo.repo_name, locked)) |
|
1539 | 1539 | } |
|
1540 | 1540 | return _d |
|
1541 | 1541 | except Exception: |
|
1542 | 1542 | log.exception( |
|
1543 | 1543 | "Exception occurred while trying to lock repository") |
|
1544 | 1544 | raise JSONRPCError( |
|
1545 | 1545 | 'Error occurred locking repository `%s`' % repo.repo_name |
|
1546 | 1546 | ) |
|
1547 | 1547 | |
|
1548 | 1548 | |
|
1549 | 1549 | @jsonrpc_method() |
|
1550 | 1550 | def comment_commit( |
|
1551 | 1551 | request, apiuser, repoid, commit_id, message, status=Optional(None), |
|
1552 | 1552 | comment_type=Optional(ChangesetComment.COMMENT_TYPE_NOTE), |
|
1553 | 1553 | resolves_comment_id=Optional(None), extra_recipients=Optional([]), |
|
1554 | userid=Optional(OAttr('apiuser'))): | |
|
1554 | userid=Optional(OAttr('apiuser')), send_email=Optional(True)): | |
|
1555 | 1555 | """ |
|
1556 | 1556 | Set a commit comment, and optionally change the status of the commit. |
|
1557 | 1557 | |
|
1558 | 1558 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1559 | 1559 | :type apiuser: AuthUser |
|
1560 | 1560 | :param repoid: Set the repository name or repository ID. |
|
1561 | 1561 | :type repoid: str or int |
|
1562 | 1562 | :param commit_id: Specify the commit_id for which to set a comment. |
|
1563 | 1563 | :type commit_id: str |
|
1564 | 1564 | :param message: The comment text. |
|
1565 | 1565 | :type message: str |
|
1566 | 1566 | :param status: (**Optional**) status of commit, one of: 'not_reviewed', |
|
1567 | 1567 | 'approved', 'rejected', 'under_review' |
|
1568 | 1568 | :type status: str |
|
1569 | 1569 | :param comment_type: Comment type, one of: 'note', 'todo' |
|
1570 | 1570 | :type comment_type: Optional(str), default: 'note' |
|
1571 | 1571 | :param resolves_comment_id: id of comment which this one will resolve |
|
1572 | 1572 | :type resolves_comment_id: Optional(int) |
|
1573 | 1573 | :param extra_recipients: list of user ids or usernames to add |
|
1574 | 1574 | notifications for this comment. Acts like a CC for notification |
|
1575 | 1575 | :type extra_recipients: Optional(list) |
|
1576 | 1576 | :param userid: Set the user name of the comment creator. |
|
1577 | 1577 | :type userid: Optional(str or int) |
|
1578 | :param send_email: Define if this comment should also send email notification | |
|
1579 | :type send_email: Optional(bool) | |
|
1578 | 1580 | |
|
1579 | 1581 | Example error output: |
|
1580 | 1582 | |
|
1581 | 1583 | .. code-block:: bash |
|
1582 | 1584 | |
|
1583 | 1585 | { |
|
1584 | 1586 | "id" : <id_given_in_input>, |
|
1585 | 1587 | "result" : { |
|
1586 | 1588 | "msg": "Commented on commit `<commit_id>` for repository `<repoid>`", |
|
1587 | 1589 | "status_change": null or <status>, |
|
1588 | 1590 | "success": true |
|
1589 | 1591 | }, |
|
1590 | 1592 | "error" : null |
|
1591 | 1593 | } |
|
1592 | 1594 | |
|
1593 | 1595 | """ |
|
1594 | 1596 | repo = get_repo_or_error(repoid) |
|
1595 | 1597 | if not has_superadmin_permission(apiuser): |
|
1596 | 1598 | _perms = ('repository.read', 'repository.write', 'repository.admin') |
|
1597 | 1599 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1598 | 1600 | |
|
1599 | 1601 | try: |
|
1600 | 1602 | commit_id = repo.scm_instance().get_commit(commit_id=commit_id).raw_id |
|
1601 | 1603 | except Exception as e: |
|
1602 | 1604 | log.exception('Failed to fetch commit') |
|
1603 | 1605 | raise JSONRPCError(safe_str(e)) |
|
1604 | 1606 | |
|
1605 | 1607 | if isinstance(userid, Optional): |
|
1606 | 1608 | userid = apiuser.user_id |
|
1607 | 1609 | |
|
1608 | 1610 | user = get_user_or_error(userid) |
|
1609 | 1611 | status = Optional.extract(status) |
|
1610 | 1612 | comment_type = Optional.extract(comment_type) |
|
1611 | 1613 | resolves_comment_id = Optional.extract(resolves_comment_id) |
|
1612 | 1614 | extra_recipients = Optional.extract(extra_recipients) |
|
1615 | send_email = Optional.extract(send_email, binary=True) | |
|
1613 | 1616 | |
|
1614 | 1617 | allowed_statuses = [x[0] for x in ChangesetStatus.STATUSES] |
|
1615 | 1618 | if status and status not in allowed_statuses: |
|
1616 | 1619 | raise JSONRPCError('Bad status, must be on ' |
|
1617 | 1620 | 'of %s got %s' % (allowed_statuses, status,)) |
|
1618 | 1621 | |
|
1619 | 1622 | if resolves_comment_id: |
|
1620 | 1623 | comment = ChangesetComment.get(resolves_comment_id) |
|
1621 | 1624 | if not comment: |
|
1622 | 1625 | raise JSONRPCError( |
|
1623 | 1626 | 'Invalid resolves_comment_id `%s` for this commit.' |
|
1624 | 1627 | % resolves_comment_id) |
|
1625 | 1628 | if comment.comment_type != ChangesetComment.COMMENT_TYPE_TODO: |
|
1626 | 1629 | raise JSONRPCError( |
|
1627 | 1630 | 'Comment `%s` is wrong type for setting status to resolved.' |
|
1628 | 1631 | % resolves_comment_id) |
|
1629 | 1632 | |
|
1630 | 1633 | try: |
|
1631 | 1634 | rc_config = SettingsModel().get_all_settings() |
|
1632 | 1635 | renderer = rc_config.get('rhodecode_markup_renderer', 'rst') |
|
1633 | 1636 | status_change_label = ChangesetStatus.get_status_lbl(status) |
|
1634 | 1637 | comment = CommentsModel().create( |
|
1635 | 1638 | message, repo, user, commit_id=commit_id, |
|
1636 | 1639 | status_change=status_change_label, |
|
1637 | 1640 | status_change_type=status, |
|
1638 | 1641 | renderer=renderer, |
|
1639 | 1642 | comment_type=comment_type, |
|
1640 | 1643 | resolves_comment_id=resolves_comment_id, |
|
1641 | 1644 | auth_user=apiuser, |
|
1642 | extra_recipients=extra_recipients | |
|
1645 | extra_recipients=extra_recipients, | |
|
1646 | send_email=send_email | |
|
1643 | 1647 | ) |
|
1644 | 1648 | if status: |
|
1645 | 1649 | # also do a status change |
|
1646 | 1650 | try: |
|
1647 | 1651 | ChangesetStatusModel().set_status( |
|
1648 | 1652 | repo, status, user, comment, revision=commit_id, |
|
1649 | 1653 | dont_allow_on_closed_pull_request=True |
|
1650 | 1654 | ) |
|
1651 | 1655 | except StatusChangeOnClosedPullRequestError: |
|
1652 | 1656 | log.exception( |
|
1653 | 1657 | "Exception occurred while trying to change repo commit status") |
|
1654 | 1658 | msg = ('Changing status on a changeset associated with ' |
|
1655 | 1659 | 'a closed pull request is not allowed') |
|
1656 | 1660 | raise JSONRPCError(msg) |
|
1657 | 1661 | |
|
1658 | 1662 | Session().commit() |
|
1659 | 1663 | return { |
|
1660 | 1664 | 'msg': ( |
|
1661 | 1665 | 'Commented on commit `%s` for repository `%s`' % ( |
|
1662 | 1666 | comment.revision, repo.repo_name)), |
|
1663 | 1667 | 'status_change': status, |
|
1664 | 1668 | 'success': True, |
|
1665 | 1669 | } |
|
1666 | 1670 | except JSONRPCError: |
|
1667 | 1671 | # catch any inside errors, and re-raise them to prevent from |
|
1668 | 1672 | # below global catch to silence them |
|
1669 | 1673 | raise |
|
1670 | 1674 | except Exception: |
|
1671 | 1675 | log.exception("Exception occurred while trying to comment on commit") |
|
1672 | 1676 | raise JSONRPCError( |
|
1673 | 1677 | 'failed to set comment on repository `%s`' % (repo.repo_name,) |
|
1674 | 1678 | ) |
|
1675 | 1679 | |
|
1676 | 1680 | |
|
1677 | 1681 | @jsonrpc_method() |
|
1678 | 1682 | def get_repo_comments(request, apiuser, repoid, |
|
1679 | 1683 | commit_id=Optional(None), comment_type=Optional(None), |
|
1680 | 1684 | userid=Optional(None)): |
|
1681 | 1685 | """ |
|
1682 | 1686 | Get all comments for a repository |
|
1683 | 1687 | |
|
1684 | 1688 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1685 | 1689 | :type apiuser: AuthUser |
|
1686 | 1690 | :param repoid: Set the repository name or repository ID. |
|
1687 | 1691 | :type repoid: str or int |
|
1688 | 1692 | :param commit_id: Optionally filter the comments by the commit_id |
|
1689 | 1693 | :type commit_id: Optional(str), default: None |
|
1690 | 1694 | :param comment_type: Optionally filter the comments by the comment_type |
|
1691 | 1695 | one of: 'note', 'todo' |
|
1692 | 1696 | :type comment_type: Optional(str), default: None |
|
1693 | 1697 | :param userid: Optionally filter the comments by the author of comment |
|
1694 | 1698 | :type userid: Optional(str or int), Default: None |
|
1695 | 1699 | |
|
1696 | 1700 | Example error output: |
|
1697 | 1701 | |
|
1698 | 1702 | .. code-block:: bash |
|
1699 | 1703 | |
|
1700 | 1704 | { |
|
1701 | 1705 | "id" : <id_given_in_input>, |
|
1702 | 1706 | "result" : [ |
|
1703 | 1707 | { |
|
1704 | 1708 | "comment_author": <USER_DETAILS>, |
|
1705 | 1709 | "comment_created_on": "2017-02-01T14:38:16.309", |
|
1706 | 1710 | "comment_f_path": "file.txt", |
|
1707 | 1711 | "comment_id": 282, |
|
1708 | 1712 | "comment_lineno": "n1", |
|
1709 | 1713 | "comment_resolved_by": null, |
|
1710 | 1714 | "comment_status": [], |
|
1711 | 1715 | "comment_text": "This file needs a header", |
|
1712 | 1716 | "comment_type": "todo" |
|
1713 | 1717 | } |
|
1714 | 1718 | ], |
|
1715 | 1719 | "error" : null |
|
1716 | 1720 | } |
|
1717 | 1721 | |
|
1718 | 1722 | """ |
|
1719 | 1723 | repo = get_repo_or_error(repoid) |
|
1720 | 1724 | if not has_superadmin_permission(apiuser): |
|
1721 | 1725 | _perms = ('repository.read', 'repository.write', 'repository.admin') |
|
1722 | 1726 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1723 | 1727 | |
|
1724 | 1728 | commit_id = Optional.extract(commit_id) |
|
1725 | 1729 | |
|
1726 | 1730 | userid = Optional.extract(userid) |
|
1727 | 1731 | if userid: |
|
1728 | 1732 | user = get_user_or_error(userid) |
|
1729 | 1733 | else: |
|
1730 | 1734 | user = None |
|
1731 | 1735 | |
|
1732 | 1736 | comment_type = Optional.extract(comment_type) |
|
1733 | 1737 | if comment_type and comment_type not in ChangesetComment.COMMENT_TYPES: |
|
1734 | 1738 | raise JSONRPCError( |
|
1735 | 1739 | 'comment_type must be one of `{}` got {}'.format( |
|
1736 | 1740 | ChangesetComment.COMMENT_TYPES, comment_type) |
|
1737 | 1741 | ) |
|
1738 | 1742 | |
|
1739 | 1743 | comments = CommentsModel().get_repository_comments( |
|
1740 | 1744 | repo=repo, comment_type=comment_type, user=user, commit_id=commit_id) |
|
1741 | 1745 | return comments |
|
1742 | 1746 | |
|
1743 | 1747 | |
|
1744 | 1748 | @jsonrpc_method() |
|
1745 | 1749 | def grant_user_permission(request, apiuser, repoid, userid, perm): |
|
1746 | 1750 | """ |
|
1747 | 1751 | Grant permissions for the specified user on the given repository, |
|
1748 | 1752 | or update existing permissions if found. |
|
1749 | 1753 | |
|
1750 | 1754 | This command can only be run using an |authtoken| with admin |
|
1751 | 1755 | permissions on the |repo|. |
|
1752 | 1756 | |
|
1753 | 1757 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1754 | 1758 | :type apiuser: AuthUser |
|
1755 | 1759 | :param repoid: Set the repository name or repository ID. |
|
1756 | 1760 | :type repoid: str or int |
|
1757 | 1761 | :param userid: Set the user name. |
|
1758 | 1762 | :type userid: str |
|
1759 | 1763 | :param perm: Set the user permissions, using the following format |
|
1760 | 1764 | ``(repository.(none|read|write|admin))`` |
|
1761 | 1765 | :type perm: str |
|
1762 | 1766 | |
|
1763 | 1767 | Example output: |
|
1764 | 1768 | |
|
1765 | 1769 | .. code-block:: bash |
|
1766 | 1770 | |
|
1767 | 1771 | id : <id_given_in_input> |
|
1768 | 1772 | result: { |
|
1769 | 1773 | "msg" : "Granted perm: `<perm>` for user: `<username>` in repo: `<reponame>`", |
|
1770 | 1774 | "success": true |
|
1771 | 1775 | } |
|
1772 | 1776 | error: null |
|
1773 | 1777 | """ |
|
1774 | 1778 | |
|
1775 | 1779 | repo = get_repo_or_error(repoid) |
|
1776 | 1780 | user = get_user_or_error(userid) |
|
1777 | 1781 | perm = get_perm_or_error(perm) |
|
1778 | 1782 | if not has_superadmin_permission(apiuser): |
|
1779 | 1783 | _perms = ('repository.admin',) |
|
1780 | 1784 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1781 | 1785 | |
|
1782 | 1786 | perm_additions = [[user.user_id, perm.permission_name, "user"]] |
|
1783 | 1787 | try: |
|
1784 | 1788 | changes = RepoModel().update_permissions( |
|
1785 | 1789 | repo=repo, perm_additions=perm_additions, cur_user=apiuser) |
|
1786 | 1790 | |
|
1787 | 1791 | action_data = { |
|
1788 | 1792 | 'added': changes['added'], |
|
1789 | 1793 | 'updated': changes['updated'], |
|
1790 | 1794 | 'deleted': changes['deleted'], |
|
1791 | 1795 | } |
|
1792 | 1796 | audit_logger.store_api( |
|
1793 | 1797 | 'repo.edit.permissions', action_data=action_data, user=apiuser, repo=repo) |
|
1794 | 1798 | Session().commit() |
|
1795 | 1799 | PermissionModel().flush_user_permission_caches(changes) |
|
1796 | 1800 | |
|
1797 | 1801 | return { |
|
1798 | 1802 | 'msg': 'Granted perm: `%s` for user: `%s` in repo: `%s`' % ( |
|
1799 | 1803 | perm.permission_name, user.username, repo.repo_name |
|
1800 | 1804 | ), |
|
1801 | 1805 | 'success': True |
|
1802 | 1806 | } |
|
1803 | 1807 | except Exception: |
|
1804 | 1808 | log.exception("Exception occurred while trying edit permissions for repo") |
|
1805 | 1809 | raise JSONRPCError( |
|
1806 | 1810 | 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
|
1807 | 1811 | userid, repoid |
|
1808 | 1812 | ) |
|
1809 | 1813 | ) |
|
1810 | 1814 | |
|
1811 | 1815 | |
|
1812 | 1816 | @jsonrpc_method() |
|
1813 | 1817 | def revoke_user_permission(request, apiuser, repoid, userid): |
|
1814 | 1818 | """ |
|
1815 | 1819 | Revoke permission for a user on the specified repository. |
|
1816 | 1820 | |
|
1817 | 1821 | This command can only be run using an |authtoken| with admin |
|
1818 | 1822 | permissions on the |repo|. |
|
1819 | 1823 | |
|
1820 | 1824 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1821 | 1825 | :type apiuser: AuthUser |
|
1822 | 1826 | :param repoid: Set the repository name or repository ID. |
|
1823 | 1827 | :type repoid: str or int |
|
1824 | 1828 | :param userid: Set the user name of revoked user. |
|
1825 | 1829 | :type userid: str or int |
|
1826 | 1830 | |
|
1827 | 1831 | Example error output: |
|
1828 | 1832 | |
|
1829 | 1833 | .. code-block:: bash |
|
1830 | 1834 | |
|
1831 | 1835 | id : <id_given_in_input> |
|
1832 | 1836 | result: { |
|
1833 | 1837 | "msg" : "Revoked perm for user: `<username>` in repo: `<reponame>`", |
|
1834 | 1838 | "success": true |
|
1835 | 1839 | } |
|
1836 | 1840 | error: null |
|
1837 | 1841 | """ |
|
1838 | 1842 | |
|
1839 | 1843 | repo = get_repo_or_error(repoid) |
|
1840 | 1844 | user = get_user_or_error(userid) |
|
1841 | 1845 | if not has_superadmin_permission(apiuser): |
|
1842 | 1846 | _perms = ('repository.admin',) |
|
1843 | 1847 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1844 | 1848 | |
|
1845 | 1849 | perm_deletions = [[user.user_id, None, "user"]] |
|
1846 | 1850 | try: |
|
1847 | 1851 | changes = RepoModel().update_permissions( |
|
1848 | 1852 | repo=repo, perm_deletions=perm_deletions, cur_user=user) |
|
1849 | 1853 | |
|
1850 | 1854 | action_data = { |
|
1851 | 1855 | 'added': changes['added'], |
|
1852 | 1856 | 'updated': changes['updated'], |
|
1853 | 1857 | 'deleted': changes['deleted'], |
|
1854 | 1858 | } |
|
1855 | 1859 | audit_logger.store_api( |
|
1856 | 1860 | 'repo.edit.permissions', action_data=action_data, user=apiuser, repo=repo) |
|
1857 | 1861 | Session().commit() |
|
1858 | 1862 | PermissionModel().flush_user_permission_caches(changes) |
|
1859 | 1863 | |
|
1860 | 1864 | return { |
|
1861 | 1865 | 'msg': 'Revoked perm for user: `%s` in repo: `%s`' % ( |
|
1862 | 1866 | user.username, repo.repo_name |
|
1863 | 1867 | ), |
|
1864 | 1868 | 'success': True |
|
1865 | 1869 | } |
|
1866 | 1870 | except Exception: |
|
1867 | 1871 | log.exception("Exception occurred while trying revoke permissions to repo") |
|
1868 | 1872 | raise JSONRPCError( |
|
1869 | 1873 | 'failed to edit permission for user: `%s` in repo: `%s`' % ( |
|
1870 | 1874 | userid, repoid |
|
1871 | 1875 | ) |
|
1872 | 1876 | ) |
|
1873 | 1877 | |
|
1874 | 1878 | |
|
1875 | 1879 | @jsonrpc_method() |
|
1876 | 1880 | def grant_user_group_permission(request, apiuser, repoid, usergroupid, perm): |
|
1877 | 1881 | """ |
|
1878 | 1882 | Grant permission for a user group on the specified repository, |
|
1879 | 1883 | or update existing permissions. |
|
1880 | 1884 | |
|
1881 | 1885 | This command can only be run using an |authtoken| with admin |
|
1882 | 1886 | permissions on the |repo|. |
|
1883 | 1887 | |
|
1884 | 1888 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1885 | 1889 | :type apiuser: AuthUser |
|
1886 | 1890 | :param repoid: Set the repository name or repository ID. |
|
1887 | 1891 | :type repoid: str or int |
|
1888 | 1892 | :param usergroupid: Specify the ID of the user group. |
|
1889 | 1893 | :type usergroupid: str or int |
|
1890 | 1894 | :param perm: Set the user group permissions using the following |
|
1891 | 1895 | format: (repository.(none|read|write|admin)) |
|
1892 | 1896 | :type perm: str |
|
1893 | 1897 | |
|
1894 | 1898 | Example output: |
|
1895 | 1899 | |
|
1896 | 1900 | .. code-block:: bash |
|
1897 | 1901 | |
|
1898 | 1902 | id : <id_given_in_input> |
|
1899 | 1903 | result : { |
|
1900 | 1904 | "msg" : "Granted perm: `<perm>` for group: `<usersgroupname>` in repo: `<reponame>`", |
|
1901 | 1905 | "success": true |
|
1902 | 1906 | |
|
1903 | 1907 | } |
|
1904 | 1908 | error : null |
|
1905 | 1909 | |
|
1906 | 1910 | Example error output: |
|
1907 | 1911 | |
|
1908 | 1912 | .. code-block:: bash |
|
1909 | 1913 | |
|
1910 | 1914 | id : <id_given_in_input> |
|
1911 | 1915 | result : null |
|
1912 | 1916 | error : { |
|
1913 | 1917 | "failed to edit permission for user group: `<usergroup>` in repo `<repo>`' |
|
1914 | 1918 | } |
|
1915 | 1919 | |
|
1916 | 1920 | """ |
|
1917 | 1921 | |
|
1918 | 1922 | repo = get_repo_or_error(repoid) |
|
1919 | 1923 | perm = get_perm_or_error(perm) |
|
1920 | 1924 | if not has_superadmin_permission(apiuser): |
|
1921 | 1925 | _perms = ('repository.admin',) |
|
1922 | 1926 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1923 | 1927 | |
|
1924 | 1928 | user_group = get_user_group_or_error(usergroupid) |
|
1925 | 1929 | if not has_superadmin_permission(apiuser): |
|
1926 | 1930 | # check if we have at least read permission for this user group ! |
|
1927 | 1931 | _perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin',) |
|
1928 | 1932 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
1929 | 1933 | user=apiuser, user_group_name=user_group.users_group_name): |
|
1930 | 1934 | raise JSONRPCError( |
|
1931 | 1935 | 'user group `%s` does not exist' % (usergroupid,)) |
|
1932 | 1936 | |
|
1933 | 1937 | perm_additions = [[user_group.users_group_id, perm.permission_name, "user_group"]] |
|
1934 | 1938 | try: |
|
1935 | 1939 | changes = RepoModel().update_permissions( |
|
1936 | 1940 | repo=repo, perm_additions=perm_additions, cur_user=apiuser) |
|
1937 | 1941 | action_data = { |
|
1938 | 1942 | 'added': changes['added'], |
|
1939 | 1943 | 'updated': changes['updated'], |
|
1940 | 1944 | 'deleted': changes['deleted'], |
|
1941 | 1945 | } |
|
1942 | 1946 | audit_logger.store_api( |
|
1943 | 1947 | 'repo.edit.permissions', action_data=action_data, user=apiuser, repo=repo) |
|
1944 | 1948 | Session().commit() |
|
1945 | 1949 | PermissionModel().flush_user_permission_caches(changes) |
|
1946 | 1950 | |
|
1947 | 1951 | return { |
|
1948 | 1952 | 'msg': 'Granted perm: `%s` for user group: `%s` in ' |
|
1949 | 1953 | 'repo: `%s`' % ( |
|
1950 | 1954 | perm.permission_name, user_group.users_group_name, |
|
1951 | 1955 | repo.repo_name |
|
1952 | 1956 | ), |
|
1953 | 1957 | 'success': True |
|
1954 | 1958 | } |
|
1955 | 1959 | except Exception: |
|
1956 | 1960 | log.exception( |
|
1957 | 1961 | "Exception occurred while trying change permission on repo") |
|
1958 | 1962 | raise JSONRPCError( |
|
1959 | 1963 | 'failed to edit permission for user group: `%s` in ' |
|
1960 | 1964 | 'repo: `%s`' % ( |
|
1961 | 1965 | usergroupid, repo.repo_name |
|
1962 | 1966 | ) |
|
1963 | 1967 | ) |
|
1964 | 1968 | |
|
1965 | 1969 | |
|
1966 | 1970 | @jsonrpc_method() |
|
1967 | 1971 | def revoke_user_group_permission(request, apiuser, repoid, usergroupid): |
|
1968 | 1972 | """ |
|
1969 | 1973 | Revoke the permissions of a user group on a given repository. |
|
1970 | 1974 | |
|
1971 | 1975 | This command can only be run using an |authtoken| with admin |
|
1972 | 1976 | permissions on the |repo|. |
|
1973 | 1977 | |
|
1974 | 1978 | :param apiuser: This is filled automatically from the |authtoken|. |
|
1975 | 1979 | :type apiuser: AuthUser |
|
1976 | 1980 | :param repoid: Set the repository name or repository ID. |
|
1977 | 1981 | :type repoid: str or int |
|
1978 | 1982 | :param usergroupid: Specify the user group ID. |
|
1979 | 1983 | :type usergroupid: str or int |
|
1980 | 1984 | |
|
1981 | 1985 | Example output: |
|
1982 | 1986 | |
|
1983 | 1987 | .. code-block:: bash |
|
1984 | 1988 | |
|
1985 | 1989 | id : <id_given_in_input> |
|
1986 | 1990 | result: { |
|
1987 | 1991 | "msg" : "Revoked perm for group: `<usersgroupname>` in repo: `<reponame>`", |
|
1988 | 1992 | "success": true |
|
1989 | 1993 | } |
|
1990 | 1994 | error: null |
|
1991 | 1995 | """ |
|
1992 | 1996 | |
|
1993 | 1997 | repo = get_repo_or_error(repoid) |
|
1994 | 1998 | if not has_superadmin_permission(apiuser): |
|
1995 | 1999 | _perms = ('repository.admin',) |
|
1996 | 2000 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
1997 | 2001 | |
|
1998 | 2002 | user_group = get_user_group_or_error(usergroupid) |
|
1999 | 2003 | if not has_superadmin_permission(apiuser): |
|
2000 | 2004 | # check if we have at least read permission for this user group ! |
|
2001 | 2005 | _perms = ('usergroup.read', 'usergroup.write', 'usergroup.admin',) |
|
2002 | 2006 | if not HasUserGroupPermissionAnyApi(*_perms)( |
|
2003 | 2007 | user=apiuser, user_group_name=user_group.users_group_name): |
|
2004 | 2008 | raise JSONRPCError( |
|
2005 | 2009 | 'user group `%s` does not exist' % (usergroupid,)) |
|
2006 | 2010 | |
|
2007 | 2011 | perm_deletions = [[user_group.users_group_id, None, "user_group"]] |
|
2008 | 2012 | try: |
|
2009 | 2013 | changes = RepoModel().update_permissions( |
|
2010 | 2014 | repo=repo, perm_deletions=perm_deletions, cur_user=apiuser) |
|
2011 | 2015 | action_data = { |
|
2012 | 2016 | 'added': changes['added'], |
|
2013 | 2017 | 'updated': changes['updated'], |
|
2014 | 2018 | 'deleted': changes['deleted'], |
|
2015 | 2019 | } |
|
2016 | 2020 | audit_logger.store_api( |
|
2017 | 2021 | 'repo.edit.permissions', action_data=action_data, user=apiuser, repo=repo) |
|
2018 | 2022 | Session().commit() |
|
2019 | 2023 | PermissionModel().flush_user_permission_caches(changes) |
|
2020 | 2024 | |
|
2021 | 2025 | return { |
|
2022 | 2026 | 'msg': 'Revoked perm for user group: `%s` in repo: `%s`' % ( |
|
2023 | 2027 | user_group.users_group_name, repo.repo_name |
|
2024 | 2028 | ), |
|
2025 | 2029 | 'success': True |
|
2026 | 2030 | } |
|
2027 | 2031 | except Exception: |
|
2028 | 2032 | log.exception("Exception occurred while trying revoke " |
|
2029 | 2033 | "user group permission on repo") |
|
2030 | 2034 | raise JSONRPCError( |
|
2031 | 2035 | 'failed to edit permission for user group: `%s` in ' |
|
2032 | 2036 | 'repo: `%s`' % ( |
|
2033 | 2037 | user_group.users_group_name, repo.repo_name |
|
2034 | 2038 | ) |
|
2035 | 2039 | ) |
|
2036 | 2040 | |
|
2037 | 2041 | |
|
2038 | 2042 | @jsonrpc_method() |
|
2039 | 2043 | def pull(request, apiuser, repoid, remote_uri=Optional(None)): |
|
2040 | 2044 | """ |
|
2041 | 2045 | Triggers a pull on the given repository from a remote location. You |
|
2042 | 2046 | can use this to keep remote repositories up-to-date. |
|
2043 | 2047 | |
|
2044 | 2048 | This command can only be run using an |authtoken| with admin |
|
2045 | 2049 | rights to the specified repository. For more information, |
|
2046 | 2050 | see :ref:`config-token-ref`. |
|
2047 | 2051 | |
|
2048 | 2052 | This command takes the following options: |
|
2049 | 2053 | |
|
2050 | 2054 | :param apiuser: This is filled automatically from the |authtoken|. |
|
2051 | 2055 | :type apiuser: AuthUser |
|
2052 | 2056 | :param repoid: The repository name or repository ID. |
|
2053 | 2057 | :type repoid: str or int |
|
2054 | 2058 | :param remote_uri: Optional remote URI to pass in for pull |
|
2055 | 2059 | :type remote_uri: str |
|
2056 | 2060 | |
|
2057 | 2061 | Example output: |
|
2058 | 2062 | |
|
2059 | 2063 | .. code-block:: bash |
|
2060 | 2064 | |
|
2061 | 2065 | id : <id_given_in_input> |
|
2062 | 2066 | result : { |
|
2063 | 2067 | "msg": "Pulled from url `<remote_url>` on repo `<repository name>`" |
|
2064 | 2068 | "repository": "<repository name>" |
|
2065 | 2069 | } |
|
2066 | 2070 | error : null |
|
2067 | 2071 | |
|
2068 | 2072 | Example error output: |
|
2069 | 2073 | |
|
2070 | 2074 | .. code-block:: bash |
|
2071 | 2075 | |
|
2072 | 2076 | id : <id_given_in_input> |
|
2073 | 2077 | result : null |
|
2074 | 2078 | error : { |
|
2075 | 2079 | "Unable to push changes from `<remote_url>`" |
|
2076 | 2080 | } |
|
2077 | 2081 | |
|
2078 | 2082 | """ |
|
2079 | 2083 | |
|
2080 | 2084 | repo = get_repo_or_error(repoid) |
|
2081 | 2085 | remote_uri = Optional.extract(remote_uri) |
|
2082 | 2086 | remote_uri_display = remote_uri or repo.clone_uri_hidden |
|
2083 | 2087 | if not has_superadmin_permission(apiuser): |
|
2084 | 2088 | _perms = ('repository.admin',) |
|
2085 | 2089 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
2086 | 2090 | |
|
2087 | 2091 | try: |
|
2088 | 2092 | ScmModel().pull_changes( |
|
2089 | 2093 | repo.repo_name, apiuser.username, remote_uri=remote_uri) |
|
2090 | 2094 | return { |
|
2091 | 2095 | 'msg': 'Pulled from url `%s` on repo `%s`' % ( |
|
2092 | 2096 | remote_uri_display, repo.repo_name), |
|
2093 | 2097 | 'repository': repo.repo_name |
|
2094 | 2098 | } |
|
2095 | 2099 | except Exception: |
|
2096 | 2100 | log.exception("Exception occurred while trying to " |
|
2097 | 2101 | "pull changes from remote location") |
|
2098 | 2102 | raise JSONRPCError( |
|
2099 | 2103 | 'Unable to pull changes from `%s`' % remote_uri_display |
|
2100 | 2104 | ) |
|
2101 | 2105 | |
|
2102 | 2106 | |
|
2103 | 2107 | @jsonrpc_method() |
|
2104 | 2108 | def strip(request, apiuser, repoid, revision, branch): |
|
2105 | 2109 | """ |
|
2106 | 2110 | Strips the given revision from the specified repository. |
|
2107 | 2111 | |
|
2108 | 2112 | * This will remove the revision and all of its decendants. |
|
2109 | 2113 | |
|
2110 | 2114 | This command can only be run using an |authtoken| with admin rights to |
|
2111 | 2115 | the specified repository. |
|
2112 | 2116 | |
|
2113 | 2117 | This command takes the following options: |
|
2114 | 2118 | |
|
2115 | 2119 | :param apiuser: This is filled automatically from the |authtoken|. |
|
2116 | 2120 | :type apiuser: AuthUser |
|
2117 | 2121 | :param repoid: The repository name or repository ID. |
|
2118 | 2122 | :type repoid: str or int |
|
2119 | 2123 | :param revision: The revision you wish to strip. |
|
2120 | 2124 | :type revision: str |
|
2121 | 2125 | :param branch: The branch from which to strip the revision. |
|
2122 | 2126 | :type branch: str |
|
2123 | 2127 | |
|
2124 | 2128 | Example output: |
|
2125 | 2129 | |
|
2126 | 2130 | .. code-block:: bash |
|
2127 | 2131 | |
|
2128 | 2132 | id : <id_given_in_input> |
|
2129 | 2133 | result : { |
|
2130 | 2134 | "msg": "'Stripped commit <commit_hash> from repo `<repository name>`'" |
|
2131 | 2135 | "repository": "<repository name>" |
|
2132 | 2136 | } |
|
2133 | 2137 | error : null |
|
2134 | 2138 | |
|
2135 | 2139 | Example error output: |
|
2136 | 2140 | |
|
2137 | 2141 | .. code-block:: bash |
|
2138 | 2142 | |
|
2139 | 2143 | id : <id_given_in_input> |
|
2140 | 2144 | result : null |
|
2141 | 2145 | error : { |
|
2142 | 2146 | "Unable to strip commit <commit_hash> from repo `<repository name>`" |
|
2143 | 2147 | } |
|
2144 | 2148 | |
|
2145 | 2149 | """ |
|
2146 | 2150 | |
|
2147 | 2151 | repo = get_repo_or_error(repoid) |
|
2148 | 2152 | if not has_superadmin_permission(apiuser): |
|
2149 | 2153 | _perms = ('repository.admin',) |
|
2150 | 2154 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
2151 | 2155 | |
|
2152 | 2156 | try: |
|
2153 | 2157 | ScmModel().strip(repo, revision, branch) |
|
2154 | 2158 | audit_logger.store_api( |
|
2155 | 2159 | 'repo.commit.strip', action_data={'commit_id': revision}, |
|
2156 | 2160 | repo=repo, |
|
2157 | 2161 | user=apiuser, commit=True) |
|
2158 | 2162 | |
|
2159 | 2163 | return { |
|
2160 | 2164 | 'msg': 'Stripped commit %s from repo `%s`' % ( |
|
2161 | 2165 | revision, repo.repo_name), |
|
2162 | 2166 | 'repository': repo.repo_name |
|
2163 | 2167 | } |
|
2164 | 2168 | except Exception: |
|
2165 | 2169 | log.exception("Exception while trying to strip") |
|
2166 | 2170 | raise JSONRPCError( |
|
2167 | 2171 | 'Unable to strip commit %s from repo `%s`' % ( |
|
2168 | 2172 | revision, repo.repo_name) |
|
2169 | 2173 | ) |
|
2170 | 2174 | |
|
2171 | 2175 | |
|
2172 | 2176 | @jsonrpc_method() |
|
2173 | 2177 | def get_repo_settings(request, apiuser, repoid, key=Optional(None)): |
|
2174 | 2178 | """ |
|
2175 | 2179 | Returns all settings for a repository. If key is given it only returns the |
|
2176 | 2180 | setting identified by the key or null. |
|
2177 | 2181 | |
|
2178 | 2182 | :param apiuser: This is filled automatically from the |authtoken|. |
|
2179 | 2183 | :type apiuser: AuthUser |
|
2180 | 2184 | :param repoid: The repository name or repository id. |
|
2181 | 2185 | :type repoid: str or int |
|
2182 | 2186 | :param key: Key of the setting to return. |
|
2183 | 2187 | :type: key: Optional(str) |
|
2184 | 2188 | |
|
2185 | 2189 | Example output: |
|
2186 | 2190 | |
|
2187 | 2191 | .. code-block:: bash |
|
2188 | 2192 | |
|
2189 | 2193 | { |
|
2190 | 2194 | "error": null, |
|
2191 | 2195 | "id": 237, |
|
2192 | 2196 | "result": { |
|
2193 | 2197 | "extensions_largefiles": true, |
|
2194 | 2198 | "extensions_evolve": true, |
|
2195 | 2199 | "hooks_changegroup_push_logger": true, |
|
2196 | 2200 | "hooks_changegroup_repo_size": false, |
|
2197 | 2201 | "hooks_outgoing_pull_logger": true, |
|
2198 | 2202 | "phases_publish": "True", |
|
2199 | 2203 | "rhodecode_hg_use_rebase_for_merging": true, |
|
2200 | 2204 | "rhodecode_pr_merge_enabled": true, |
|
2201 | 2205 | "rhodecode_use_outdated_comments": true |
|
2202 | 2206 | } |
|
2203 | 2207 | } |
|
2204 | 2208 | """ |
|
2205 | 2209 | |
|
2206 | 2210 | # Restrict access to this api method to admins only. |
|
2207 | 2211 | if not has_superadmin_permission(apiuser): |
|
2208 | 2212 | raise JSONRPCForbidden() |
|
2209 | 2213 | |
|
2210 | 2214 | try: |
|
2211 | 2215 | repo = get_repo_or_error(repoid) |
|
2212 | 2216 | settings_model = VcsSettingsModel(repo=repo) |
|
2213 | 2217 | settings = settings_model.get_global_settings() |
|
2214 | 2218 | settings.update(settings_model.get_repo_settings()) |
|
2215 | 2219 | |
|
2216 | 2220 | # If only a single setting is requested fetch it from all settings. |
|
2217 | 2221 | key = Optional.extract(key) |
|
2218 | 2222 | if key is not None: |
|
2219 | 2223 | settings = settings.get(key, None) |
|
2220 | 2224 | except Exception: |
|
2221 | 2225 | msg = 'Failed to fetch settings for repository `{}`'.format(repoid) |
|
2222 | 2226 | log.exception(msg) |
|
2223 | 2227 | raise JSONRPCError(msg) |
|
2224 | 2228 | |
|
2225 | 2229 | return settings |
|
2226 | 2230 | |
|
2227 | 2231 | |
|
2228 | 2232 | @jsonrpc_method() |
|
2229 | 2233 | def set_repo_settings(request, apiuser, repoid, settings): |
|
2230 | 2234 | """ |
|
2231 | 2235 | Update repository settings. Returns true on success. |
|
2232 | 2236 | |
|
2233 | 2237 | :param apiuser: This is filled automatically from the |authtoken|. |
|
2234 | 2238 | :type apiuser: AuthUser |
|
2235 | 2239 | :param repoid: The repository name or repository id. |
|
2236 | 2240 | :type repoid: str or int |
|
2237 | 2241 | :param settings: The new settings for the repository. |
|
2238 | 2242 | :type: settings: dict |
|
2239 | 2243 | |
|
2240 | 2244 | Example output: |
|
2241 | 2245 | |
|
2242 | 2246 | .. code-block:: bash |
|
2243 | 2247 | |
|
2244 | 2248 | { |
|
2245 | 2249 | "error": null, |
|
2246 | 2250 | "id": 237, |
|
2247 | 2251 | "result": true |
|
2248 | 2252 | } |
|
2249 | 2253 | """ |
|
2250 | 2254 | # Restrict access to this api method to admins only. |
|
2251 | 2255 | if not has_superadmin_permission(apiuser): |
|
2252 | 2256 | raise JSONRPCForbidden() |
|
2253 | 2257 | |
|
2254 | 2258 | if type(settings) is not dict: |
|
2255 | 2259 | raise JSONRPCError('Settings have to be a JSON Object.') |
|
2256 | 2260 | |
|
2257 | 2261 | try: |
|
2258 | 2262 | settings_model = VcsSettingsModel(repo=repoid) |
|
2259 | 2263 | |
|
2260 | 2264 | # Merge global, repo and incoming settings. |
|
2261 | 2265 | new_settings = settings_model.get_global_settings() |
|
2262 | 2266 | new_settings.update(settings_model.get_repo_settings()) |
|
2263 | 2267 | new_settings.update(settings) |
|
2264 | 2268 | |
|
2265 | 2269 | # Update the settings. |
|
2266 | 2270 | inherit_global_settings = new_settings.get( |
|
2267 | 2271 | 'inherit_global_settings', False) |
|
2268 | 2272 | settings_model.create_or_update_repo_settings( |
|
2269 | 2273 | new_settings, inherit_global_settings=inherit_global_settings) |
|
2270 | 2274 | Session().commit() |
|
2271 | 2275 | except Exception: |
|
2272 | 2276 | msg = 'Failed to update settings for repository `{}`'.format(repoid) |
|
2273 | 2277 | log.exception(msg) |
|
2274 | 2278 | raise JSONRPCError(msg) |
|
2275 | 2279 | |
|
2276 | 2280 | # Indicate success. |
|
2277 | 2281 | return True |
|
2278 | 2282 | |
|
2279 | 2283 | |
|
2280 | 2284 | @jsonrpc_method() |
|
2281 | 2285 | def maintenance(request, apiuser, repoid): |
|
2282 | 2286 | """ |
|
2283 | 2287 | Triggers a maintenance on the given repository. |
|
2284 | 2288 | |
|
2285 | 2289 | This command can only be run using an |authtoken| with admin |
|
2286 | 2290 | rights to the specified repository. For more information, |
|
2287 | 2291 | see :ref:`config-token-ref`. |
|
2288 | 2292 | |
|
2289 | 2293 | This command takes the following options: |
|
2290 | 2294 | |
|
2291 | 2295 | :param apiuser: This is filled automatically from the |authtoken|. |
|
2292 | 2296 | :type apiuser: AuthUser |
|
2293 | 2297 | :param repoid: The repository name or repository ID. |
|
2294 | 2298 | :type repoid: str or int |
|
2295 | 2299 | |
|
2296 | 2300 | Example output: |
|
2297 | 2301 | |
|
2298 | 2302 | .. code-block:: bash |
|
2299 | 2303 | |
|
2300 | 2304 | id : <id_given_in_input> |
|
2301 | 2305 | result : { |
|
2302 | 2306 | "msg": "executed maintenance command", |
|
2303 | 2307 | "executed_actions": [ |
|
2304 | 2308 | <action_message>, <action_message2>... |
|
2305 | 2309 | ], |
|
2306 | 2310 | "repository": "<repository name>" |
|
2307 | 2311 | } |
|
2308 | 2312 | error : null |
|
2309 | 2313 | |
|
2310 | 2314 | Example error output: |
|
2311 | 2315 | |
|
2312 | 2316 | .. code-block:: bash |
|
2313 | 2317 | |
|
2314 | 2318 | id : <id_given_in_input> |
|
2315 | 2319 | result : null |
|
2316 | 2320 | error : { |
|
2317 | 2321 | "Unable to execute maintenance on `<reponame>`" |
|
2318 | 2322 | } |
|
2319 | 2323 | |
|
2320 | 2324 | """ |
|
2321 | 2325 | |
|
2322 | 2326 | repo = get_repo_or_error(repoid) |
|
2323 | 2327 | if not has_superadmin_permission(apiuser): |
|
2324 | 2328 | _perms = ('repository.admin',) |
|
2325 | 2329 | validate_repo_permissions(apiuser, repoid, repo, _perms) |
|
2326 | 2330 | |
|
2327 | 2331 | try: |
|
2328 | 2332 | maintenance = repo_maintenance.RepoMaintenance() |
|
2329 | 2333 | executed_actions = maintenance.execute(repo) |
|
2330 | 2334 | |
|
2331 | 2335 | return { |
|
2332 | 2336 | 'msg': 'executed maintenance command', |
|
2333 | 2337 | 'executed_actions': executed_actions, |
|
2334 | 2338 | 'repository': repo.repo_name |
|
2335 | 2339 | } |
|
2336 | 2340 | except Exception: |
|
2337 | 2341 | log.exception("Exception occurred while trying to run maintenance") |
|
2338 | 2342 | raise JSONRPCError( |
|
2339 | 2343 | 'Unable to execute maintenance on `%s`' % repo.repo_name) |
@@ -1,419 +1,421 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | import inspect | |
|
22 | 21 | import logging |
|
23 | 22 | import itertools |
|
24 | 23 | import base64 |
|
25 | 24 | |
|
26 | 25 | from pyramid import compat |
|
27 | 26 | |
|
28 | 27 | from rhodecode.api import ( |
|
29 | 28 | jsonrpc_method, JSONRPCError, JSONRPCForbidden, find_methods) |
|
30 | 29 | |
|
31 | 30 | from rhodecode.api.utils import ( |
|
32 | 31 | Optional, OAttr, has_superadmin_permission, get_user_or_error) |
|
33 | 32 | from rhodecode.lib.utils import repo2db_mapper |
|
34 | 33 | from rhodecode.lib import system_info |
|
35 | 34 | from rhodecode.lib import user_sessions |
|
36 | 35 | from rhodecode.lib import exc_tracking |
|
37 | 36 | from rhodecode.lib.ext_json import json |
|
38 | 37 | from rhodecode.lib.utils2 import safe_int |
|
39 | 38 | from rhodecode.model.db import UserIpMap |
|
40 | 39 | from rhodecode.model.scm import ScmModel |
|
41 | 40 | from rhodecode.model.settings import VcsSettingsModel |
|
42 | 41 | from rhodecode.apps.file_store import utils |
|
43 | 42 | from rhodecode.apps.file_store.exceptions import FileNotAllowedException, \ |
|
44 | 43 | FileOverSizeException |
|
45 | 44 | |
|
46 | 45 | log = logging.getLogger(__name__) |
|
47 | 46 | |
|
48 | 47 | |
|
49 | 48 | @jsonrpc_method() |
|
50 | 49 | def get_server_info(request, apiuser): |
|
51 | 50 | """ |
|
52 | 51 | Returns the |RCE| server information. |
|
53 | 52 | |
|
54 | 53 | This includes the running version of |RCE| and all installed |
|
55 | 54 | packages. This command takes the following options: |
|
56 | 55 | |
|
57 | 56 | :param apiuser: This is filled automatically from the |authtoken|. |
|
58 | 57 | :type apiuser: AuthUser |
|
59 | 58 | |
|
60 | 59 | Example output: |
|
61 | 60 | |
|
62 | 61 | .. code-block:: bash |
|
63 | 62 | |
|
64 | 63 | id : <id_given_in_input> |
|
65 | 64 | result : { |
|
66 | 65 | 'modules': [<module name>,...] |
|
67 | 66 | 'py_version': <python version>, |
|
68 | 67 | 'platform': <platform type>, |
|
69 | 68 | 'rhodecode_version': <rhodecode version> |
|
70 | 69 | } |
|
71 | 70 | error : null |
|
72 | 71 | """ |
|
73 | 72 | |
|
74 | 73 | if not has_superadmin_permission(apiuser): |
|
75 | 74 | raise JSONRPCForbidden() |
|
76 | 75 | |
|
77 | 76 | server_info = ScmModel().get_server_info(request.environ) |
|
78 | 77 | # rhodecode-index requires those |
|
79 | 78 | |
|
80 | 79 | server_info['index_storage'] = server_info['search']['value']['location'] |
|
81 | 80 | server_info['storage'] = server_info['storage']['value']['path'] |
|
82 | 81 | |
|
83 | 82 | return server_info |
|
84 | 83 | |
|
85 | 84 | |
|
86 | 85 | @jsonrpc_method() |
|
87 | 86 | def get_repo_store(request, apiuser): |
|
88 | 87 | """ |
|
89 | 88 | Returns the |RCE| repository storage information. |
|
90 | 89 | |
|
91 | 90 | :param apiuser: This is filled automatically from the |authtoken|. |
|
92 | 91 | :type apiuser: AuthUser |
|
93 | 92 | |
|
94 | 93 | Example output: |
|
95 | 94 | |
|
96 | 95 | .. code-block:: bash |
|
97 | 96 | |
|
98 | 97 | id : <id_given_in_input> |
|
99 | 98 | result : { |
|
100 | 99 | 'modules': [<module name>,...] |
|
101 | 100 | 'py_version': <python version>, |
|
102 | 101 | 'platform': <platform type>, |
|
103 | 102 | 'rhodecode_version': <rhodecode version> |
|
104 | 103 | } |
|
105 | 104 | error : null |
|
106 | 105 | """ |
|
107 | 106 | |
|
108 | 107 | if not has_superadmin_permission(apiuser): |
|
109 | 108 | raise JSONRPCForbidden() |
|
110 | 109 | |
|
111 | 110 | path = VcsSettingsModel().get_repos_location() |
|
112 | 111 | return {"path": path} |
|
113 | 112 | |
|
114 | 113 | |
|
115 | 114 | @jsonrpc_method() |
|
116 | 115 | def get_ip(request, apiuser, userid=Optional(OAttr('apiuser'))): |
|
117 | 116 | """ |
|
118 | 117 | Displays the IP Address as seen from the |RCE| server. |
|
119 | 118 | |
|
120 | 119 | * This command displays the IP Address, as well as all the defined IP |
|
121 | 120 | addresses for the specified user. If the ``userid`` is not set, the |
|
122 | 121 | data returned is for the user calling the method. |
|
123 | 122 | |
|
124 | 123 | This command can only be run using an |authtoken| with admin rights to |
|
125 | 124 | the specified repository. |
|
126 | 125 | |
|
127 | 126 | This command takes the following options: |
|
128 | 127 | |
|
129 | 128 | :param apiuser: This is filled automatically from |authtoken|. |
|
130 | 129 | :type apiuser: AuthUser |
|
131 | 130 | :param userid: Sets the userid for which associated IP Address data |
|
132 | 131 | is returned. |
|
133 | 132 | :type userid: Optional(str or int) |
|
134 | 133 | |
|
135 | 134 | Example output: |
|
136 | 135 | |
|
137 | 136 | .. code-block:: bash |
|
138 | 137 | |
|
139 | 138 | id : <id_given_in_input> |
|
140 | 139 | result : { |
|
141 | 140 | "server_ip_addr": "<ip_from_clien>", |
|
142 | 141 | "user_ips": [ |
|
143 | 142 | { |
|
144 | 143 | "ip_addr": "<ip_with_mask>", |
|
145 | 144 | "ip_range": ["<start_ip>", "<end_ip>"], |
|
146 | 145 | }, |
|
147 | 146 | ... |
|
148 | 147 | ] |
|
149 | 148 | } |
|
150 | 149 | |
|
151 | 150 | """ |
|
152 | 151 | if not has_superadmin_permission(apiuser): |
|
153 | 152 | raise JSONRPCForbidden() |
|
154 | 153 | |
|
155 | 154 | userid = Optional.extract(userid, evaluate_locals=locals()) |
|
156 | 155 | userid = getattr(userid, 'user_id', userid) |
|
157 | 156 | |
|
158 | 157 | user = get_user_or_error(userid) |
|
159 | 158 | ips = UserIpMap.query().filter(UserIpMap.user == user).all() |
|
160 | 159 | return { |
|
161 | 160 | 'server_ip_addr': request.rpc_ip_addr, |
|
162 | 161 | 'user_ips': ips |
|
163 | 162 | } |
|
164 | 163 | |
|
165 | 164 | |
|
166 | 165 | @jsonrpc_method() |
|
167 | 166 | def rescan_repos(request, apiuser, remove_obsolete=Optional(False)): |
|
168 | 167 | """ |
|
169 | 168 | Triggers a rescan of the specified repositories. |
|
170 | 169 | |
|
171 | 170 | * If the ``remove_obsolete`` option is set, it also deletes repositories |
|
172 | 171 | that are found in the database but not on the file system, so called |
|
173 | 172 | "clean zombies". |
|
174 | 173 | |
|
175 | 174 | This command can only be run using an |authtoken| with admin rights to |
|
176 | 175 | the specified repository. |
|
177 | 176 | |
|
178 | 177 | This command takes the following options: |
|
179 | 178 | |
|
180 | 179 | :param apiuser: This is filled automatically from the |authtoken|. |
|
181 | 180 | :type apiuser: AuthUser |
|
182 | 181 | :param remove_obsolete: Deletes repositories from the database that |
|
183 | 182 | are not found on the filesystem. |
|
184 | 183 | :type remove_obsolete: Optional(``True`` | ``False``) |
|
185 | 184 | |
|
186 | 185 | Example output: |
|
187 | 186 | |
|
188 | 187 | .. code-block:: bash |
|
189 | 188 | |
|
190 | 189 | id : <id_given_in_input> |
|
191 | 190 | result : { |
|
192 | 191 | 'added': [<added repository name>,...] |
|
193 | 192 | 'removed': [<removed repository name>,...] |
|
194 | 193 | } |
|
195 | 194 | error : null |
|
196 | 195 | |
|
197 | 196 | Example error output: |
|
198 | 197 | |
|
199 | 198 | .. code-block:: bash |
|
200 | 199 | |
|
201 | 200 | id : <id_given_in_input> |
|
202 | 201 | result : null |
|
203 | 202 | error : { |
|
204 | 203 | 'Error occurred during rescan repositories action' |
|
205 | 204 | } |
|
206 | 205 | |
|
207 | 206 | """ |
|
208 | 207 | if not has_superadmin_permission(apiuser): |
|
209 | 208 | raise JSONRPCForbidden() |
|
210 | 209 | |
|
211 | 210 | try: |
|
212 | 211 | rm_obsolete = Optional.extract(remove_obsolete) |
|
213 | 212 | added, removed = repo2db_mapper(ScmModel().repo_scan(), |
|
214 | 213 | remove_obsolete=rm_obsolete) |
|
215 | 214 | return {'added': added, 'removed': removed} |
|
216 | 215 | except Exception: |
|
217 | 216 | log.exception('Failed to run repo rescann') |
|
218 | 217 | raise JSONRPCError( |
|
219 | 218 | 'Error occurred during rescan repositories action' |
|
220 | 219 | ) |
|
221 | 220 | |
|
222 | 221 | |
|
223 | 222 | @jsonrpc_method() |
|
224 | 223 | def cleanup_sessions(request, apiuser, older_then=Optional(60)): |
|
225 | 224 | """ |
|
226 | 225 | Triggers a session cleanup action. |
|
227 | 226 | |
|
228 | 227 | If the ``older_then`` option is set, only sessions that hasn't been |
|
229 | 228 | accessed in the given number of days will be removed. |
|
230 | 229 | |
|
231 | 230 | This command can only be run using an |authtoken| with admin rights to |
|
232 | 231 | the specified repository. |
|
233 | 232 | |
|
234 | 233 | This command takes the following options: |
|
235 | 234 | |
|
236 | 235 | :param apiuser: This is filled automatically from the |authtoken|. |
|
237 | 236 | :type apiuser: AuthUser |
|
238 | 237 | :param older_then: Deletes session that hasn't been accessed |
|
239 | 238 | in given number of days. |
|
240 | 239 | :type older_then: Optional(int) |
|
241 | 240 | |
|
242 | 241 | Example output: |
|
243 | 242 | |
|
244 | 243 | .. code-block:: bash |
|
245 | 244 | |
|
246 | 245 | id : <id_given_in_input> |
|
247 | 246 | result: { |
|
248 | 247 | "backend": "<type of backend>", |
|
249 | 248 | "sessions_removed": <number_of_removed_sessions> |
|
250 | 249 | } |
|
251 | 250 | error : null |
|
252 | 251 | |
|
253 | 252 | Example error output: |
|
254 | 253 | |
|
255 | 254 | .. code-block:: bash |
|
256 | 255 | |
|
257 | 256 | id : <id_given_in_input> |
|
258 | 257 | result : null |
|
259 | 258 | error : { |
|
260 | 259 | 'Error occurred during session cleanup' |
|
261 | 260 | } |
|
262 | 261 | |
|
263 | 262 | """ |
|
264 | 263 | if not has_superadmin_permission(apiuser): |
|
265 | 264 | raise JSONRPCForbidden() |
|
266 | 265 | |
|
267 | 266 | older_then = safe_int(Optional.extract(older_then)) or 60 |
|
268 | 267 | older_than_seconds = 60 * 60 * 24 * older_then |
|
269 | 268 | |
|
270 | 269 | config = system_info.rhodecode_config().get_value()['value']['config'] |
|
271 | 270 | session_model = user_sessions.get_session_handler( |
|
272 | 271 | config.get('beaker.session.type', 'memory'))(config) |
|
273 | 272 | |
|
274 | 273 | backend = session_model.SESSION_TYPE |
|
275 | 274 | try: |
|
276 | 275 | cleaned = session_model.clean_sessions( |
|
277 | 276 | older_than_seconds=older_than_seconds) |
|
278 | 277 | return {'sessions_removed': cleaned, 'backend': backend} |
|
279 | 278 | except user_sessions.CleanupCommand as msg: |
|
280 | 279 | return {'cleanup_command': msg.message, 'backend': backend} |
|
281 | 280 | except Exception as e: |
|
282 | 281 | log.exception('Failed session cleanup') |
|
283 | 282 | raise JSONRPCError( |
|
284 | 283 | 'Error occurred during session cleanup' |
|
285 | 284 | ) |
|
286 | 285 | |
|
287 | 286 | |
|
288 | 287 | @jsonrpc_method() |
|
289 | 288 | def get_method(request, apiuser, pattern=Optional('*')): |
|
290 | 289 | """ |
|
291 | 290 | Returns list of all available API methods. By default match pattern |
|
292 | 291 | os "*" but any other pattern can be specified. eg *comment* will return |
|
293 | 292 | all methods with comment inside them. If just single method is matched |
|
294 | 293 | returned data will also include method specification |
|
295 | 294 | |
|
296 | 295 | This command can only be run using an |authtoken| with admin rights to |
|
297 | 296 | the specified repository. |
|
298 | 297 | |
|
299 | 298 | This command takes the following options: |
|
300 | 299 | |
|
301 | 300 | :param apiuser: This is filled automatically from the |authtoken|. |
|
302 | 301 | :type apiuser: AuthUser |
|
303 | 302 | :param pattern: pattern to match method names against |
|
304 | 303 | :type pattern: Optional("*") |
|
305 | 304 | |
|
306 | 305 | Example output: |
|
307 | 306 | |
|
308 | 307 | .. code-block:: bash |
|
309 | 308 | |
|
310 | 309 | id : <id_given_in_input> |
|
311 | 310 | "result": [ |
|
312 | 311 | "changeset_comment", |
|
313 | 312 | "comment_pull_request", |
|
314 | 313 | "comment_commit" |
|
315 | 314 | ] |
|
316 | 315 | error : null |
|
317 | 316 | |
|
318 | 317 | .. code-block:: bash |
|
319 | 318 | |
|
320 | 319 | id : <id_given_in_input> |
|
321 | 320 | "result": [ |
|
322 | 321 | "comment_commit", |
|
323 | 322 | { |
|
324 | 323 | "apiuser": "<RequiredType>", |
|
325 | 324 | "comment_type": "<Optional:u'note'>", |
|
326 | 325 | "commit_id": "<RequiredType>", |
|
327 | 326 | "message": "<RequiredType>", |
|
328 | 327 | "repoid": "<RequiredType>", |
|
329 | 328 | "request": "<RequiredType>", |
|
330 | 329 | "resolves_comment_id": "<Optional:None>", |
|
331 | 330 | "status": "<Optional:None>", |
|
332 | 331 | "userid": "<Optional:<OptionalAttr:apiuser>>" |
|
333 | 332 | } |
|
334 | 333 | ] |
|
335 | 334 | error : null |
|
336 | 335 | """ |
|
336 | from rhodecode.config.patches import inspect_getargspec | |
|
337 | inspect = inspect_getargspec() | |
|
338 | ||
|
337 | 339 | if not has_superadmin_permission(apiuser): |
|
338 | 340 | raise JSONRPCForbidden() |
|
339 | 341 | |
|
340 | 342 | pattern = Optional.extract(pattern) |
|
341 | 343 | |
|
342 | 344 | matches = find_methods(request.registry.jsonrpc_methods, pattern) |
|
343 | 345 | |
|
344 | 346 | args_desc = [] |
|
345 | 347 | if len(matches) == 1: |
|
346 | 348 | func = matches[matches.keys()[0]] |
|
347 | 349 | |
|
348 | 350 | argspec = inspect.getargspec(func) |
|
349 | 351 | arglist = argspec[0] |
|
350 | 352 | defaults = map(repr, argspec[3] or []) |
|
351 | 353 | |
|
352 | 354 | default_empty = '<RequiredType>' |
|
353 | 355 | |
|
354 | 356 | # kw arguments required by this method |
|
355 | 357 | func_kwargs = dict(itertools.izip_longest( |
|
356 | 358 | reversed(arglist), reversed(defaults), fillvalue=default_empty)) |
|
357 | 359 | args_desc.append(func_kwargs) |
|
358 | 360 | |
|
359 | 361 | return matches.keys() + args_desc |
|
360 | 362 | |
|
361 | 363 | |
|
362 | 364 | @jsonrpc_method() |
|
363 | 365 | def store_exception(request, apiuser, exc_data_json, prefix=Optional('rhodecode')): |
|
364 | 366 | """ |
|
365 | 367 | Stores sent exception inside the built-in exception tracker in |RCE| server. |
|
366 | 368 | |
|
367 | 369 | This command can only be run using an |authtoken| with admin rights to |
|
368 | 370 | the specified repository. |
|
369 | 371 | |
|
370 | 372 | This command takes the following options: |
|
371 | 373 | |
|
372 | 374 | :param apiuser: This is filled automatically from the |authtoken|. |
|
373 | 375 | :type apiuser: AuthUser |
|
374 | 376 | |
|
375 | 377 | :param exc_data_json: JSON data with exception e.g |
|
376 | 378 | {"exc_traceback": "Value `1` is not allowed", "exc_type_name": "ValueError"} |
|
377 | 379 | :type exc_data_json: JSON data |
|
378 | 380 | |
|
379 | 381 | :param prefix: prefix for error type, e.g 'rhodecode', 'vcsserver', 'rhodecode-tools' |
|
380 | 382 | :type prefix: Optional("rhodecode") |
|
381 | 383 | |
|
382 | 384 | Example output: |
|
383 | 385 | |
|
384 | 386 | .. code-block:: bash |
|
385 | 387 | |
|
386 | 388 | id : <id_given_in_input> |
|
387 | 389 | "result": { |
|
388 | 390 | "exc_id": 139718459226384, |
|
389 | 391 | "exc_url": "http://localhost:8080/_admin/settings/exceptions/139718459226384" |
|
390 | 392 | } |
|
391 | 393 | error : null |
|
392 | 394 | """ |
|
393 | 395 | if not has_superadmin_permission(apiuser): |
|
394 | 396 | raise JSONRPCForbidden() |
|
395 | 397 | |
|
396 | 398 | prefix = Optional.extract(prefix) |
|
397 | 399 | exc_id = exc_tracking.generate_id() |
|
398 | 400 | |
|
399 | 401 | try: |
|
400 | 402 | exc_data = json.loads(exc_data_json) |
|
401 | 403 | except Exception: |
|
402 | 404 | log.error('Failed to parse JSON: %r', exc_data_json) |
|
403 | 405 | raise JSONRPCError('Failed to parse JSON data from exc_data_json field. ' |
|
404 | 406 | 'Please make sure it contains a valid JSON.') |
|
405 | 407 | |
|
406 | 408 | try: |
|
407 | 409 | exc_traceback = exc_data['exc_traceback'] |
|
408 | 410 | exc_type_name = exc_data['exc_type_name'] |
|
409 | 411 | except KeyError as err: |
|
410 | 412 | raise JSONRPCError('Missing exc_traceback, or exc_type_name ' |
|
411 | 413 | 'in exc_data_json field. Missing: {}'.format(err)) |
|
412 | 414 | |
|
413 | 415 | exc_tracking._store_exception( |
|
414 | 416 | exc_id=exc_id, exc_traceback=exc_traceback, |
|
415 | 417 | exc_type_name=exc_type_name, prefix=prefix) |
|
416 | 418 | |
|
417 | 419 | exc_url = request.route_url( |
|
418 | 420 | 'admin_settings_exception_tracker_show', exception_id=exc_id) |
|
419 | 421 | return {'exc_id': exc_id, 'exc_url': exc_url} |
@@ -1,802 +1,803 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import time |
|
22 | 22 | import logging |
|
23 | 23 | import operator |
|
24 | 24 | |
|
25 | 25 | from pyramid import compat |
|
26 | 26 | from pyramid.httpexceptions import HTTPFound, HTTPForbidden, HTTPBadRequest |
|
27 | 27 | |
|
28 | 28 | from rhodecode.lib import helpers as h, diffs, rc_cache |
|
29 | 29 | from rhodecode.lib.utils2 import ( |
|
30 | 30 | StrictAttributeDict, str2bool, safe_int, datetime_to_time, safe_unicode) |
|
31 | 31 | from rhodecode.lib.markup_renderer import MarkupRenderer, relative_links |
|
32 | 32 | from rhodecode.lib.vcs.backends.base import EmptyCommit |
|
33 | 33 | from rhodecode.lib.vcs.exceptions import RepositoryRequirementError |
|
34 | 34 | from rhodecode.model import repo |
|
35 | 35 | from rhodecode.model import repo_group |
|
36 | 36 | from rhodecode.model import user_group |
|
37 | 37 | from rhodecode.model import user |
|
38 | 38 | from rhodecode.model.db import User |
|
39 | 39 | from rhodecode.model.scm import ScmModel |
|
40 | from rhodecode.model.settings import VcsSettingsModel | |
|
40 | from rhodecode.model.settings import VcsSettingsModel, IssueTrackerSettingsModel | |
|
41 | 41 | from rhodecode.model.repo import ReadmeFinder |
|
42 | 42 | |
|
43 | 43 | log = logging.getLogger(__name__) |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | ADMIN_PREFIX = '/_admin' |
|
47 | 47 | STATIC_FILE_PREFIX = '/_static' |
|
48 | 48 | |
|
49 | 49 | URL_NAME_REQUIREMENTS = { |
|
50 | 50 | # group name can have a slash in them, but they must not end with a slash |
|
51 | 51 | 'group_name': r'.*?[^/]', |
|
52 | 52 | 'repo_group_name': r'.*?[^/]', |
|
53 | 53 | # repo names can have a slash in them, but they must not end with a slash |
|
54 | 54 | 'repo_name': r'.*?[^/]', |
|
55 | 55 | # file path eats up everything at the end |
|
56 | 56 | 'f_path': r'.*', |
|
57 | 57 | # reference types |
|
58 | 58 | 'source_ref_type': '(branch|book|tag|rev|\%\(source_ref_type\)s)', |
|
59 | 59 | 'target_ref_type': '(branch|book|tag|rev|\%\(target_ref_type\)s)', |
|
60 | 60 | } |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | def add_route_with_slash(config,name, pattern, **kw): |
|
64 | 64 | config.add_route(name, pattern, **kw) |
|
65 | 65 | if not pattern.endswith('/'): |
|
66 | 66 | config.add_route(name + '_slash', pattern + '/', **kw) |
|
67 | 67 | |
|
68 | 68 | |
|
69 | 69 | def add_route_requirements(route_path, requirements=None): |
|
70 | 70 | """ |
|
71 | 71 | Adds regex requirements to pyramid routes using a mapping dict |
|
72 | 72 | e.g:: |
|
73 | 73 | add_route_requirements('{repo_name}/settings') |
|
74 | 74 | """ |
|
75 | 75 | requirements = requirements or URL_NAME_REQUIREMENTS |
|
76 | 76 | for key, regex in requirements.items(): |
|
77 | 77 | route_path = route_path.replace('{%s}' % key, '{%s:%s}' % (key, regex)) |
|
78 | 78 | return route_path |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | def get_format_ref_id(repo): |
|
82 | 82 | """Returns a `repo` specific reference formatter function""" |
|
83 | 83 | if h.is_svn(repo): |
|
84 | 84 | return _format_ref_id_svn |
|
85 | 85 | else: |
|
86 | 86 | return _format_ref_id |
|
87 | 87 | |
|
88 | 88 | |
|
89 | 89 | def _format_ref_id(name, raw_id): |
|
90 | 90 | """Default formatting of a given reference `name`""" |
|
91 | 91 | return name |
|
92 | 92 | |
|
93 | 93 | |
|
94 | 94 | def _format_ref_id_svn(name, raw_id): |
|
95 | 95 | """Special way of formatting a reference for Subversion including path""" |
|
96 | 96 | return '%s@%s' % (name, raw_id) |
|
97 | 97 | |
|
98 | 98 | |
|
99 | 99 | class TemplateArgs(StrictAttributeDict): |
|
100 | 100 | pass |
|
101 | 101 | |
|
102 | 102 | |
|
103 | 103 | class BaseAppView(object): |
|
104 | 104 | |
|
105 | 105 | def __init__(self, context, request): |
|
106 | 106 | self.request = request |
|
107 | 107 | self.context = context |
|
108 | 108 | self.session = request.session |
|
109 | 109 | if not hasattr(request, 'user'): |
|
110 | 110 | # NOTE(marcink): edge case, we ended up in matched route |
|
111 | 111 | # but probably of web-app context, e.g API CALL/VCS CALL |
|
112 | 112 | if hasattr(request, 'vcs_call') or hasattr(request, 'rpc_method'): |
|
113 | 113 | log.warning('Unable to process request `%s` in this scope', request) |
|
114 | 114 | raise HTTPBadRequest() |
|
115 | 115 | |
|
116 | 116 | self._rhodecode_user = request.user # auth user |
|
117 | 117 | self._rhodecode_db_user = self._rhodecode_user.get_instance() |
|
118 | 118 | self._maybe_needs_password_change( |
|
119 | 119 | request.matched_route.name, self._rhodecode_db_user) |
|
120 | 120 | |
|
121 | 121 | def _maybe_needs_password_change(self, view_name, user_obj): |
|
122 | 122 | log.debug('Checking if user %s needs password change on view %s', |
|
123 | 123 | user_obj, view_name) |
|
124 | 124 | skip_user_views = [ |
|
125 | 125 | 'logout', 'login', |
|
126 | 126 | 'my_account_password', 'my_account_password_update' |
|
127 | 127 | ] |
|
128 | 128 | |
|
129 | 129 | if not user_obj: |
|
130 | 130 | return |
|
131 | 131 | |
|
132 | 132 | if user_obj.username == User.DEFAULT_USER: |
|
133 | 133 | return |
|
134 | 134 | |
|
135 | 135 | now = time.time() |
|
136 | 136 | should_change = user_obj.user_data.get('force_password_change') |
|
137 | 137 | change_after = safe_int(should_change) or 0 |
|
138 | 138 | if should_change and now > change_after: |
|
139 | 139 | log.debug('User %s requires password change', user_obj) |
|
140 | 140 | h.flash('You are required to change your password', 'warning', |
|
141 | 141 | ignore_duplicate=True) |
|
142 | 142 | |
|
143 | 143 | if view_name not in skip_user_views: |
|
144 | 144 | raise HTTPFound( |
|
145 | 145 | self.request.route_path('my_account_password')) |
|
146 | 146 | |
|
147 | 147 | def _log_creation_exception(self, e, repo_name): |
|
148 | 148 | _ = self.request.translate |
|
149 | 149 | reason = None |
|
150 | 150 | if len(e.args) == 2: |
|
151 | 151 | reason = e.args[1] |
|
152 | 152 | |
|
153 | 153 | if reason == 'INVALID_CERTIFICATE': |
|
154 | 154 | log.exception( |
|
155 | 155 | 'Exception creating a repository: invalid certificate') |
|
156 | 156 | msg = (_('Error creating repository %s: invalid certificate') |
|
157 | 157 | % repo_name) |
|
158 | 158 | else: |
|
159 | 159 | log.exception("Exception creating a repository") |
|
160 | 160 | msg = (_('Error creating repository %s') |
|
161 | 161 | % repo_name) |
|
162 | 162 | return msg |
|
163 | 163 | |
|
164 | 164 | def _get_local_tmpl_context(self, include_app_defaults=True): |
|
165 | 165 | c = TemplateArgs() |
|
166 | 166 | c.auth_user = self.request.user |
|
167 | 167 | # TODO(marcink): migrate the usage of c.rhodecode_user to c.auth_user |
|
168 | 168 | c.rhodecode_user = self.request.user |
|
169 | 169 | |
|
170 | 170 | if include_app_defaults: |
|
171 | 171 | from rhodecode.lib.base import attach_context_attributes |
|
172 | 172 | attach_context_attributes(c, self.request, self.request.user.user_id) |
|
173 | 173 | |
|
174 | 174 | c.is_super_admin = c.auth_user.is_admin |
|
175 | 175 | |
|
176 | 176 | c.can_create_repo = c.is_super_admin |
|
177 | 177 | c.can_create_repo_group = c.is_super_admin |
|
178 | 178 | c.can_create_user_group = c.is_super_admin |
|
179 | 179 | |
|
180 | 180 | c.is_delegated_admin = False |
|
181 | 181 | |
|
182 | 182 | if not c.auth_user.is_default and not c.is_super_admin: |
|
183 | 183 | c.can_create_repo = h.HasPermissionAny('hg.create.repository')( |
|
184 | 184 | user=self.request.user) |
|
185 | 185 | repositories = c.auth_user.repositories_admin or c.can_create_repo |
|
186 | 186 | |
|
187 | 187 | c.can_create_repo_group = h.HasPermissionAny('hg.repogroup.create.true')( |
|
188 | 188 | user=self.request.user) |
|
189 | 189 | repository_groups = c.auth_user.repository_groups_admin or c.can_create_repo_group |
|
190 | 190 | |
|
191 | 191 | c.can_create_user_group = h.HasPermissionAny('hg.usergroup.create.true')( |
|
192 | 192 | user=self.request.user) |
|
193 | 193 | user_groups = c.auth_user.user_groups_admin or c.can_create_user_group |
|
194 | 194 | # delegated admin can create, or manage some objects |
|
195 | 195 | c.is_delegated_admin = repositories or repository_groups or user_groups |
|
196 | 196 | return c |
|
197 | 197 | |
|
198 | 198 | def _get_template_context(self, tmpl_args, **kwargs): |
|
199 | 199 | |
|
200 | 200 | local_tmpl_args = { |
|
201 | 201 | 'defaults': {}, |
|
202 | 202 | 'errors': {}, |
|
203 | 203 | 'c': tmpl_args |
|
204 | 204 | } |
|
205 | 205 | local_tmpl_args.update(kwargs) |
|
206 | 206 | return local_tmpl_args |
|
207 | 207 | |
|
208 | 208 | def load_default_context(self): |
|
209 | 209 | """ |
|
210 | 210 | example: |
|
211 | 211 | |
|
212 | 212 | def load_default_context(self): |
|
213 | 213 | c = self._get_local_tmpl_context() |
|
214 | 214 | c.custom_var = 'foobar' |
|
215 | 215 | |
|
216 | 216 | return c |
|
217 | 217 | """ |
|
218 | 218 | raise NotImplementedError('Needs implementation in view class') |
|
219 | 219 | |
|
220 | 220 | |
|
221 | 221 | class RepoAppView(BaseAppView): |
|
222 | 222 | |
|
223 | 223 | def __init__(self, context, request): |
|
224 | 224 | super(RepoAppView, self).__init__(context, request) |
|
225 | 225 | self.db_repo = request.db_repo |
|
226 | 226 | self.db_repo_name = self.db_repo.repo_name |
|
227 | 227 | self.db_repo_pull_requests = ScmModel().get_pull_requests(self.db_repo) |
|
228 | 228 | self.db_repo_artifacts = ScmModel().get_artifacts(self.db_repo) |
|
229 | self.db_repo_patterns = IssueTrackerSettingsModel(repo=self.db_repo) | |
|
229 | 230 | |
|
230 | 231 | def _handle_missing_requirements(self, error): |
|
231 | 232 | log.error( |
|
232 | 233 | 'Requirements are missing for repository %s: %s', |
|
233 | 234 | self.db_repo_name, safe_unicode(error)) |
|
234 | 235 | |
|
235 | 236 | def _get_local_tmpl_context(self, include_app_defaults=True): |
|
236 | 237 | _ = self.request.translate |
|
237 | 238 | c = super(RepoAppView, self)._get_local_tmpl_context( |
|
238 | 239 | include_app_defaults=include_app_defaults) |
|
239 | 240 | |
|
240 | 241 | # register common vars for this type of view |
|
241 | 242 | c.rhodecode_db_repo = self.db_repo |
|
242 | 243 | c.repo_name = self.db_repo_name |
|
243 | 244 | c.repository_pull_requests = self.db_repo_pull_requests |
|
244 | 245 | c.repository_artifacts = self.db_repo_artifacts |
|
245 | 246 | c.repository_is_user_following = ScmModel().is_following_repo( |
|
246 | 247 | self.db_repo_name, self._rhodecode_user.user_id) |
|
247 | 248 | self.path_filter = PathFilter(None) |
|
248 | 249 | |
|
249 | 250 | c.repository_requirements_missing = {} |
|
250 | 251 | try: |
|
251 | 252 | self.rhodecode_vcs_repo = self.db_repo.scm_instance() |
|
252 | 253 | # NOTE(marcink): |
|
253 | 254 | # comparison to None since if it's an object __bool__ is expensive to |
|
254 | 255 | # calculate |
|
255 | 256 | if self.rhodecode_vcs_repo is not None: |
|
256 | 257 | path_perms = self.rhodecode_vcs_repo.get_path_permissions( |
|
257 | 258 | c.auth_user.username) |
|
258 | 259 | self.path_filter = PathFilter(path_perms) |
|
259 | 260 | except RepositoryRequirementError as e: |
|
260 | 261 | c.repository_requirements_missing = {'error': str(e)} |
|
261 | 262 | self._handle_missing_requirements(e) |
|
262 | 263 | self.rhodecode_vcs_repo = None |
|
263 | 264 | |
|
264 | 265 | c.path_filter = self.path_filter # used by atom_feed_entry.mako |
|
265 | 266 | |
|
266 | 267 | if self.rhodecode_vcs_repo is None: |
|
267 | 268 | # unable to fetch this repo as vcs instance, report back to user |
|
268 | 269 | h.flash(_( |
|
269 | 270 | "The repository `%(repo_name)s` cannot be loaded in filesystem. " |
|
270 | 271 | "Please check if it exist, or is not damaged.") % |
|
271 | 272 | {'repo_name': c.repo_name}, |
|
272 | 273 | category='error', ignore_duplicate=True) |
|
273 | 274 | if c.repository_requirements_missing: |
|
274 | 275 | route = self.request.matched_route.name |
|
275 | 276 | if route.startswith(('edit_repo', 'repo_summary')): |
|
276 | 277 | # allow summary and edit repo on missing requirements |
|
277 | 278 | return c |
|
278 | 279 | |
|
279 | 280 | raise HTTPFound( |
|
280 | 281 | h.route_path('repo_summary', repo_name=self.db_repo_name)) |
|
281 | 282 | |
|
282 | 283 | else: # redirect if we don't show missing requirements |
|
283 | 284 | raise HTTPFound(h.route_path('home')) |
|
284 | 285 | |
|
285 | 286 | c.has_origin_repo_read_perm = False |
|
286 | 287 | if self.db_repo.fork: |
|
287 | 288 | c.has_origin_repo_read_perm = h.HasRepoPermissionAny( |
|
288 | 289 | 'repository.write', 'repository.read', 'repository.admin')( |
|
289 | 290 | self.db_repo.fork.repo_name, 'summary fork link') |
|
290 | 291 | |
|
291 | 292 | return c |
|
292 | 293 | |
|
293 | 294 | def _get_f_path_unchecked(self, matchdict, default=None): |
|
294 | 295 | """ |
|
295 | 296 | Should only be used by redirects, everything else should call _get_f_path |
|
296 | 297 | """ |
|
297 | 298 | f_path = matchdict.get('f_path') |
|
298 | 299 | if f_path: |
|
299 | 300 | # fix for multiple initial slashes that causes errors for GIT |
|
300 | 301 | return f_path.lstrip('/') |
|
301 | 302 | |
|
302 | 303 | return default |
|
303 | 304 | |
|
304 | 305 | def _get_f_path(self, matchdict, default=None): |
|
305 | 306 | f_path_match = self._get_f_path_unchecked(matchdict, default) |
|
306 | 307 | return self.path_filter.assert_path_permissions(f_path_match) |
|
307 | 308 | |
|
308 | 309 | def _get_general_setting(self, target_repo, settings_key, default=False): |
|
309 | 310 | settings_model = VcsSettingsModel(repo=target_repo) |
|
310 | 311 | settings = settings_model.get_general_settings() |
|
311 | 312 | return settings.get(settings_key, default) |
|
312 | 313 | |
|
313 | 314 | def _get_repo_setting(self, target_repo, settings_key, default=False): |
|
314 | 315 | settings_model = VcsSettingsModel(repo=target_repo) |
|
315 | 316 | settings = settings_model.get_repo_settings_inherited() |
|
316 | 317 | return settings.get(settings_key, default) |
|
317 | 318 | |
|
318 | 319 | def _get_readme_data(self, db_repo, renderer_type, commit_id=None, path='/'): |
|
319 | 320 | log.debug('Looking for README file at path %s', path) |
|
320 | 321 | if commit_id: |
|
321 | 322 | landing_commit_id = commit_id |
|
322 | 323 | else: |
|
323 | 324 | landing_commit = db_repo.get_landing_commit() |
|
324 | 325 | if isinstance(landing_commit, EmptyCommit): |
|
325 | 326 | return None, None |
|
326 | 327 | landing_commit_id = landing_commit.raw_id |
|
327 | 328 | |
|
328 | 329 | cache_namespace_uid = 'cache_repo.{}'.format(db_repo.repo_id) |
|
329 | 330 | region = rc_cache.get_or_create_region('cache_repo', cache_namespace_uid) |
|
330 | 331 | start = time.time() |
|
331 | 332 | |
|
332 | 333 | @region.conditional_cache_on_arguments(namespace=cache_namespace_uid) |
|
333 | 334 | def generate_repo_readme(repo_id, _commit_id, _repo_name, _readme_search_path, _renderer_type): |
|
334 | 335 | readme_data = None |
|
335 | 336 | readme_filename = None |
|
336 | 337 | |
|
337 | 338 | commit = db_repo.get_commit(_commit_id) |
|
338 | 339 | log.debug("Searching for a README file at commit %s.", _commit_id) |
|
339 | 340 | readme_node = ReadmeFinder(_renderer_type).search(commit, path=_readme_search_path) |
|
340 | 341 | |
|
341 | 342 | if readme_node: |
|
342 | 343 | log.debug('Found README node: %s', readme_node) |
|
343 | 344 | relative_urls = { |
|
344 | 345 | 'raw': h.route_path( |
|
345 | 346 | 'repo_file_raw', repo_name=_repo_name, |
|
346 | 347 | commit_id=commit.raw_id, f_path=readme_node.path), |
|
347 | 348 | 'standard': h.route_path( |
|
348 | 349 | 'repo_files', repo_name=_repo_name, |
|
349 | 350 | commit_id=commit.raw_id, f_path=readme_node.path), |
|
350 | 351 | } |
|
351 | 352 | readme_data = self._render_readme_or_none(commit, readme_node, relative_urls) |
|
352 | 353 | readme_filename = readme_node.unicode_path |
|
353 | 354 | |
|
354 | 355 | return readme_data, readme_filename |
|
355 | 356 | |
|
356 | 357 | readme_data, readme_filename = generate_repo_readme( |
|
357 | 358 | db_repo.repo_id, landing_commit_id, db_repo.repo_name, path, renderer_type,) |
|
358 | 359 | compute_time = time.time() - start |
|
359 | 360 | log.debug('Repo README for path %s generated and computed in %.4fs', |
|
360 | 361 | path, compute_time) |
|
361 | 362 | return readme_data, readme_filename |
|
362 | 363 | |
|
363 | 364 | def _render_readme_or_none(self, commit, readme_node, relative_urls): |
|
364 | 365 | log.debug('Found README file `%s` rendering...', readme_node.path) |
|
365 | 366 | renderer = MarkupRenderer() |
|
366 | 367 | try: |
|
367 | 368 | html_source = renderer.render( |
|
368 | 369 | readme_node.content, filename=readme_node.path) |
|
369 | 370 | if relative_urls: |
|
370 | 371 | return relative_links(html_source, relative_urls) |
|
371 | 372 | return html_source |
|
372 | 373 | except Exception: |
|
373 | 374 | log.exception( |
|
374 | 375 | "Exception while trying to render the README") |
|
375 | 376 | |
|
376 | 377 | def get_recache_flag(self): |
|
377 | 378 | for flag_name in ['force_recache', 'force-recache', 'no-cache']: |
|
378 | 379 | flag_val = self.request.GET.get(flag_name) |
|
379 | 380 | if str2bool(flag_val): |
|
380 | 381 | return True |
|
381 | 382 | return False |
|
382 | 383 | |
|
383 | 384 | |
|
384 | 385 | class PathFilter(object): |
|
385 | 386 | |
|
386 | 387 | # Expects and instance of BasePathPermissionChecker or None |
|
387 | 388 | def __init__(self, permission_checker): |
|
388 | 389 | self.permission_checker = permission_checker |
|
389 | 390 | |
|
390 | 391 | def assert_path_permissions(self, path): |
|
391 | 392 | if self.path_access_allowed(path): |
|
392 | 393 | return path |
|
393 | 394 | raise HTTPForbidden() |
|
394 | 395 | |
|
395 | 396 | def path_access_allowed(self, path): |
|
396 | 397 | log.debug('Checking ACL permissions for PathFilter for `%s`', path) |
|
397 | 398 | if self.permission_checker: |
|
398 | 399 | return path and self.permission_checker.has_access(path) |
|
399 | 400 | return True |
|
400 | 401 | |
|
401 | 402 | def filter_patchset(self, patchset): |
|
402 | 403 | if not self.permission_checker or not patchset: |
|
403 | 404 | return patchset, False |
|
404 | 405 | had_filtered = False |
|
405 | 406 | filtered_patchset = [] |
|
406 | 407 | for patch in patchset: |
|
407 | 408 | filename = patch.get('filename', None) |
|
408 | 409 | if not filename or self.permission_checker.has_access(filename): |
|
409 | 410 | filtered_patchset.append(patch) |
|
410 | 411 | else: |
|
411 | 412 | had_filtered = True |
|
412 | 413 | if had_filtered: |
|
413 | 414 | if isinstance(patchset, diffs.LimitedDiffContainer): |
|
414 | 415 | filtered_patchset = diffs.LimitedDiffContainer(patchset.diff_limit, patchset.cur_diff_size, filtered_patchset) |
|
415 | 416 | return filtered_patchset, True |
|
416 | 417 | else: |
|
417 | 418 | return patchset, False |
|
418 | 419 | |
|
419 | 420 | def render_patchset_filtered(self, diffset, patchset, source_ref=None, target_ref=None): |
|
420 | 421 | filtered_patchset, has_hidden_changes = self.filter_patchset(patchset) |
|
421 | 422 | result = diffset.render_patchset( |
|
422 | 423 | filtered_patchset, source_ref=source_ref, target_ref=target_ref) |
|
423 | 424 | result.has_hidden_changes = has_hidden_changes |
|
424 | 425 | return result |
|
425 | 426 | |
|
426 | 427 | def get_raw_patch(self, diff_processor): |
|
427 | 428 | if self.permission_checker is None: |
|
428 | 429 | return diff_processor.as_raw() |
|
429 | 430 | elif self.permission_checker.has_full_access: |
|
430 | 431 | return diff_processor.as_raw() |
|
431 | 432 | else: |
|
432 | 433 | return '# Repository has user-specific filters, raw patch generation is disabled.' |
|
433 | 434 | |
|
434 | 435 | @property |
|
435 | 436 | def is_enabled(self): |
|
436 | 437 | return self.permission_checker is not None |
|
437 | 438 | |
|
438 | 439 | |
|
439 | 440 | class RepoGroupAppView(BaseAppView): |
|
440 | 441 | def __init__(self, context, request): |
|
441 | 442 | super(RepoGroupAppView, self).__init__(context, request) |
|
442 | 443 | self.db_repo_group = request.db_repo_group |
|
443 | 444 | self.db_repo_group_name = self.db_repo_group.group_name |
|
444 | 445 | |
|
445 | 446 | def _get_local_tmpl_context(self, include_app_defaults=True): |
|
446 | 447 | _ = self.request.translate |
|
447 | 448 | c = super(RepoGroupAppView, self)._get_local_tmpl_context( |
|
448 | 449 | include_app_defaults=include_app_defaults) |
|
449 | 450 | c.repo_group = self.db_repo_group |
|
450 | 451 | return c |
|
451 | 452 | |
|
452 | 453 | def _revoke_perms_on_yourself(self, form_result): |
|
453 | 454 | _updates = filter(lambda u: self._rhodecode_user.user_id == int(u[0]), |
|
454 | 455 | form_result['perm_updates']) |
|
455 | 456 | _additions = filter(lambda u: self._rhodecode_user.user_id == int(u[0]), |
|
456 | 457 | form_result['perm_additions']) |
|
457 | 458 | _deletions = filter(lambda u: self._rhodecode_user.user_id == int(u[0]), |
|
458 | 459 | form_result['perm_deletions']) |
|
459 | 460 | admin_perm = 'group.admin' |
|
460 | 461 | if _updates and _updates[0][1] != admin_perm or \ |
|
461 | 462 | _additions and _additions[0][1] != admin_perm or \ |
|
462 | 463 | _deletions and _deletions[0][1] != admin_perm: |
|
463 | 464 | return True |
|
464 | 465 | return False |
|
465 | 466 | |
|
466 | 467 | |
|
467 | 468 | class UserGroupAppView(BaseAppView): |
|
468 | 469 | def __init__(self, context, request): |
|
469 | 470 | super(UserGroupAppView, self).__init__(context, request) |
|
470 | 471 | self.db_user_group = request.db_user_group |
|
471 | 472 | self.db_user_group_name = self.db_user_group.users_group_name |
|
472 | 473 | |
|
473 | 474 | |
|
474 | 475 | class UserAppView(BaseAppView): |
|
475 | 476 | def __init__(self, context, request): |
|
476 | 477 | super(UserAppView, self).__init__(context, request) |
|
477 | 478 | self.db_user = request.db_user |
|
478 | 479 | self.db_user_id = self.db_user.user_id |
|
479 | 480 | |
|
480 | 481 | _ = self.request.translate |
|
481 | 482 | if not request.db_user_supports_default: |
|
482 | 483 | if self.db_user.username == User.DEFAULT_USER: |
|
483 | 484 | h.flash(_("Editing user `{}` is disabled.".format( |
|
484 | 485 | User.DEFAULT_USER)), category='warning') |
|
485 | 486 | raise HTTPFound(h.route_path('users')) |
|
486 | 487 | |
|
487 | 488 | |
|
488 | 489 | class DataGridAppView(object): |
|
489 | 490 | """ |
|
490 | 491 | Common class to have re-usable grid rendering components |
|
491 | 492 | """ |
|
492 | 493 | |
|
493 | 494 | def _extract_ordering(self, request, column_map=None): |
|
494 | 495 | column_map = column_map or {} |
|
495 | 496 | column_index = safe_int(request.GET.get('order[0][column]')) |
|
496 | 497 | order_dir = request.GET.get( |
|
497 | 498 | 'order[0][dir]', 'desc') |
|
498 | 499 | order_by = request.GET.get( |
|
499 | 500 | 'columns[%s][data][sort]' % column_index, 'name_raw') |
|
500 | 501 | |
|
501 | 502 | # translate datatable to DB columns |
|
502 | 503 | order_by = column_map.get(order_by) or order_by |
|
503 | 504 | |
|
504 | 505 | search_q = request.GET.get('search[value]') |
|
505 | 506 | return search_q, order_by, order_dir |
|
506 | 507 | |
|
507 | 508 | def _extract_chunk(self, request): |
|
508 | 509 | start = safe_int(request.GET.get('start'), 0) |
|
509 | 510 | length = safe_int(request.GET.get('length'), 25) |
|
510 | 511 | draw = safe_int(request.GET.get('draw')) |
|
511 | 512 | return draw, start, length |
|
512 | 513 | |
|
513 | 514 | def _get_order_col(self, order_by, model): |
|
514 | 515 | if isinstance(order_by, compat.string_types): |
|
515 | 516 | try: |
|
516 | 517 | return operator.attrgetter(order_by)(model) |
|
517 | 518 | except AttributeError: |
|
518 | 519 | return None |
|
519 | 520 | else: |
|
520 | 521 | return order_by |
|
521 | 522 | |
|
522 | 523 | |
|
523 | 524 | class BaseReferencesView(RepoAppView): |
|
524 | 525 | """ |
|
525 | 526 | Base for reference view for branches, tags and bookmarks. |
|
526 | 527 | """ |
|
527 | 528 | def load_default_context(self): |
|
528 | 529 | c = self._get_local_tmpl_context() |
|
529 | 530 | |
|
530 | 531 | |
|
531 | 532 | return c |
|
532 | 533 | |
|
533 | 534 | def load_refs_context(self, ref_items, partials_template): |
|
534 | 535 | _render = self.request.get_partial_renderer(partials_template) |
|
535 | 536 | pre_load = ["author", "date", "message", "parents"] |
|
536 | 537 | |
|
537 | 538 | is_svn = h.is_svn(self.rhodecode_vcs_repo) |
|
538 | 539 | is_hg = h.is_hg(self.rhodecode_vcs_repo) |
|
539 | 540 | |
|
540 | 541 | format_ref_id = get_format_ref_id(self.rhodecode_vcs_repo) |
|
541 | 542 | |
|
542 | 543 | closed_refs = {} |
|
543 | 544 | if is_hg: |
|
544 | 545 | closed_refs = self.rhodecode_vcs_repo.branches_closed |
|
545 | 546 | |
|
546 | 547 | data = [] |
|
547 | 548 | for ref_name, commit_id in ref_items: |
|
548 | 549 | commit = self.rhodecode_vcs_repo.get_commit( |
|
549 | 550 | commit_id=commit_id, pre_load=pre_load) |
|
550 | 551 | closed = ref_name in closed_refs |
|
551 | 552 | |
|
552 | 553 | # TODO: johbo: Unify generation of reference links |
|
553 | 554 | use_commit_id = '/' in ref_name or is_svn |
|
554 | 555 | |
|
555 | 556 | if use_commit_id: |
|
556 | 557 | files_url = h.route_path( |
|
557 | 558 | 'repo_files', |
|
558 | 559 | repo_name=self.db_repo_name, |
|
559 | 560 | f_path=ref_name if is_svn else '', |
|
560 | 561 | commit_id=commit_id) |
|
561 | 562 | |
|
562 | 563 | else: |
|
563 | 564 | files_url = h.route_path( |
|
564 | 565 | 'repo_files', |
|
565 | 566 | repo_name=self.db_repo_name, |
|
566 | 567 | f_path=ref_name if is_svn else '', |
|
567 | 568 | commit_id=ref_name, |
|
568 | 569 | _query=dict(at=ref_name)) |
|
569 | 570 | |
|
570 | 571 | data.append({ |
|
571 | 572 | "name": _render('name', ref_name, files_url, closed), |
|
572 | 573 | "name_raw": ref_name, |
|
573 | 574 | "date": _render('date', commit.date), |
|
574 | 575 | "date_raw": datetime_to_time(commit.date), |
|
575 | 576 | "author": _render('author', commit.author), |
|
576 | 577 | "commit": _render( |
|
577 | 578 | 'commit', commit.message, commit.raw_id, commit.idx), |
|
578 | 579 | "commit_raw": commit.idx, |
|
579 | 580 | "compare": _render( |
|
580 | 581 | 'compare', format_ref_id(ref_name, commit.raw_id)), |
|
581 | 582 | }) |
|
582 | 583 | |
|
583 | 584 | return data |
|
584 | 585 | |
|
585 | 586 | |
|
586 | 587 | class RepoRoutePredicate(object): |
|
587 | 588 | def __init__(self, val, config): |
|
588 | 589 | self.val = val |
|
589 | 590 | |
|
590 | 591 | def text(self): |
|
591 | 592 | return 'repo_route = %s' % self.val |
|
592 | 593 | |
|
593 | 594 | phash = text |
|
594 | 595 | |
|
595 | 596 | def __call__(self, info, request): |
|
596 | 597 | if hasattr(request, 'vcs_call'): |
|
597 | 598 | # skip vcs calls |
|
598 | 599 | return |
|
599 | 600 | |
|
600 | 601 | repo_name = info['match']['repo_name'] |
|
601 | 602 | repo_model = repo.RepoModel() |
|
602 | 603 | |
|
603 | 604 | by_name_match = repo_model.get_by_repo_name(repo_name, cache=False) |
|
604 | 605 | |
|
605 | 606 | def redirect_if_creating(route_info, db_repo): |
|
606 | 607 | skip_views = ['edit_repo_advanced_delete'] |
|
607 | 608 | route = route_info['route'] |
|
608 | 609 | # we should skip delete view so we can actually "remove" repositories |
|
609 | 610 | # if they get stuck in creating state. |
|
610 | 611 | if route.name in skip_views: |
|
611 | 612 | return |
|
612 | 613 | |
|
613 | 614 | if db_repo.repo_state in [repo.Repository.STATE_PENDING]: |
|
614 | 615 | repo_creating_url = request.route_path( |
|
615 | 616 | 'repo_creating', repo_name=db_repo.repo_name) |
|
616 | 617 | raise HTTPFound(repo_creating_url) |
|
617 | 618 | |
|
618 | 619 | if by_name_match: |
|
619 | 620 | # register this as request object we can re-use later |
|
620 | 621 | request.db_repo = by_name_match |
|
621 | 622 | redirect_if_creating(info, by_name_match) |
|
622 | 623 | return True |
|
623 | 624 | |
|
624 | 625 | by_id_match = repo_model.get_repo_by_id(repo_name) |
|
625 | 626 | if by_id_match: |
|
626 | 627 | request.db_repo = by_id_match |
|
627 | 628 | redirect_if_creating(info, by_id_match) |
|
628 | 629 | return True |
|
629 | 630 | |
|
630 | 631 | return False |
|
631 | 632 | |
|
632 | 633 | |
|
633 | 634 | class RepoForbidArchivedRoutePredicate(object): |
|
634 | 635 | def __init__(self, val, config): |
|
635 | 636 | self.val = val |
|
636 | 637 | |
|
637 | 638 | def text(self): |
|
638 | 639 | return 'repo_forbid_archived = %s' % self.val |
|
639 | 640 | |
|
640 | 641 | phash = text |
|
641 | 642 | |
|
642 | 643 | def __call__(self, info, request): |
|
643 | 644 | _ = request.translate |
|
644 | 645 | rhodecode_db_repo = request.db_repo |
|
645 | 646 | |
|
646 | 647 | log.debug( |
|
647 | 648 | '%s checking if archived flag for repo for %s', |
|
648 | 649 | self.__class__.__name__, rhodecode_db_repo.repo_name) |
|
649 | 650 | |
|
650 | 651 | if rhodecode_db_repo.archived: |
|
651 | 652 | log.warning('Current view is not supported for archived repo:%s', |
|
652 | 653 | rhodecode_db_repo.repo_name) |
|
653 | 654 | |
|
654 | 655 | h.flash( |
|
655 | 656 | h.literal(_('Action not supported for archived repository.')), |
|
656 | 657 | category='warning') |
|
657 | 658 | summary_url = request.route_path( |
|
658 | 659 | 'repo_summary', repo_name=rhodecode_db_repo.repo_name) |
|
659 | 660 | raise HTTPFound(summary_url) |
|
660 | 661 | return True |
|
661 | 662 | |
|
662 | 663 | |
|
663 | 664 | class RepoTypeRoutePredicate(object): |
|
664 | 665 | def __init__(self, val, config): |
|
665 | 666 | self.val = val or ['hg', 'git', 'svn'] |
|
666 | 667 | |
|
667 | 668 | def text(self): |
|
668 | 669 | return 'repo_accepted_type = %s' % self.val |
|
669 | 670 | |
|
670 | 671 | phash = text |
|
671 | 672 | |
|
672 | 673 | def __call__(self, info, request): |
|
673 | 674 | if hasattr(request, 'vcs_call'): |
|
674 | 675 | # skip vcs calls |
|
675 | 676 | return |
|
676 | 677 | |
|
677 | 678 | rhodecode_db_repo = request.db_repo |
|
678 | 679 | |
|
679 | 680 | log.debug( |
|
680 | 681 | '%s checking repo type for %s in %s', |
|
681 | 682 | self.__class__.__name__, rhodecode_db_repo.repo_type, self.val) |
|
682 | 683 | |
|
683 | 684 | if rhodecode_db_repo.repo_type in self.val: |
|
684 | 685 | return True |
|
685 | 686 | else: |
|
686 | 687 | log.warning('Current view is not supported for repo type:%s', |
|
687 | 688 | rhodecode_db_repo.repo_type) |
|
688 | 689 | return False |
|
689 | 690 | |
|
690 | 691 | |
|
691 | 692 | class RepoGroupRoutePredicate(object): |
|
692 | 693 | def __init__(self, val, config): |
|
693 | 694 | self.val = val |
|
694 | 695 | |
|
695 | 696 | def text(self): |
|
696 | 697 | return 'repo_group_route = %s' % self.val |
|
697 | 698 | |
|
698 | 699 | phash = text |
|
699 | 700 | |
|
700 | 701 | def __call__(self, info, request): |
|
701 | 702 | if hasattr(request, 'vcs_call'): |
|
702 | 703 | # skip vcs calls |
|
703 | 704 | return |
|
704 | 705 | |
|
705 | 706 | repo_group_name = info['match']['repo_group_name'] |
|
706 | 707 | repo_group_model = repo_group.RepoGroupModel() |
|
707 | 708 | by_name_match = repo_group_model.get_by_group_name(repo_group_name, cache=False) |
|
708 | 709 | |
|
709 | 710 | if by_name_match: |
|
710 | 711 | # register this as request object we can re-use later |
|
711 | 712 | request.db_repo_group = by_name_match |
|
712 | 713 | return True |
|
713 | 714 | |
|
714 | 715 | return False |
|
715 | 716 | |
|
716 | 717 | |
|
717 | 718 | class UserGroupRoutePredicate(object): |
|
718 | 719 | def __init__(self, val, config): |
|
719 | 720 | self.val = val |
|
720 | 721 | |
|
721 | 722 | def text(self): |
|
722 | 723 | return 'user_group_route = %s' % self.val |
|
723 | 724 | |
|
724 | 725 | phash = text |
|
725 | 726 | |
|
726 | 727 | def __call__(self, info, request): |
|
727 | 728 | if hasattr(request, 'vcs_call'): |
|
728 | 729 | # skip vcs calls |
|
729 | 730 | return |
|
730 | 731 | |
|
731 | 732 | user_group_id = info['match']['user_group_id'] |
|
732 | 733 | user_group_model = user_group.UserGroup() |
|
733 | 734 | by_id_match = user_group_model.get(user_group_id, cache=False) |
|
734 | 735 | |
|
735 | 736 | if by_id_match: |
|
736 | 737 | # register this as request object we can re-use later |
|
737 | 738 | request.db_user_group = by_id_match |
|
738 | 739 | return True |
|
739 | 740 | |
|
740 | 741 | return False |
|
741 | 742 | |
|
742 | 743 | |
|
743 | 744 | class UserRoutePredicateBase(object): |
|
744 | 745 | supports_default = None |
|
745 | 746 | |
|
746 | 747 | def __init__(self, val, config): |
|
747 | 748 | self.val = val |
|
748 | 749 | |
|
749 | 750 | def text(self): |
|
750 | 751 | raise NotImplementedError() |
|
751 | 752 | |
|
752 | 753 | def __call__(self, info, request): |
|
753 | 754 | if hasattr(request, 'vcs_call'): |
|
754 | 755 | # skip vcs calls |
|
755 | 756 | return |
|
756 | 757 | |
|
757 | 758 | user_id = info['match']['user_id'] |
|
758 | 759 | user_model = user.User() |
|
759 | 760 | by_id_match = user_model.get(user_id, cache=False) |
|
760 | 761 | |
|
761 | 762 | if by_id_match: |
|
762 | 763 | # register this as request object we can re-use later |
|
763 | 764 | request.db_user = by_id_match |
|
764 | 765 | request.db_user_supports_default = self.supports_default |
|
765 | 766 | return True |
|
766 | 767 | |
|
767 | 768 | return False |
|
768 | 769 | |
|
769 | 770 | |
|
770 | 771 | class UserRoutePredicate(UserRoutePredicateBase): |
|
771 | 772 | supports_default = False |
|
772 | 773 | |
|
773 | 774 | def text(self): |
|
774 | 775 | return 'user_route = %s' % self.val |
|
775 | 776 | |
|
776 | 777 | phash = text |
|
777 | 778 | |
|
778 | 779 | |
|
779 | 780 | class UserRouteWithDefaultPredicate(UserRoutePredicateBase): |
|
780 | 781 | supports_default = True |
|
781 | 782 | |
|
782 | 783 | def text(self): |
|
783 | 784 | return 'user_with_default_route = %s' % self.val |
|
784 | 785 | |
|
785 | 786 | phash = text |
|
786 | 787 | |
|
787 | 788 | |
|
788 | 789 | def includeme(config): |
|
789 | 790 | config.add_route_predicate( |
|
790 | 791 | 'repo_route', RepoRoutePredicate) |
|
791 | 792 | config.add_route_predicate( |
|
792 | 793 | 'repo_accepted_types', RepoTypeRoutePredicate) |
|
793 | 794 | config.add_route_predicate( |
|
794 | 795 | 'repo_forbid_when_archived', RepoForbidArchivedRoutePredicate) |
|
795 | 796 | config.add_route_predicate( |
|
796 | 797 | 'repo_group_route', RepoGroupRoutePredicate) |
|
797 | 798 | config.add_route_predicate( |
|
798 | 799 | 'user_group_route', UserGroupRoutePredicate) |
|
799 | 800 | config.add_route_predicate( |
|
800 | 801 | 'user_route_with_default', UserRouteWithDefaultPredicate) |
|
801 | 802 | config.add_route_predicate( |
|
802 | 803 | 'user_route', UserRoutePredicate) |
@@ -1,782 +1,782 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | import logging |
|
23 | 23 | import collections |
|
24 | 24 | |
|
25 | 25 | import datetime |
|
26 | 26 | import formencode |
|
27 | 27 | import formencode.htmlfill |
|
28 | 28 | |
|
29 | 29 | import rhodecode |
|
30 | 30 | from pyramid.view import view_config |
|
31 | 31 | from pyramid.httpexceptions import HTTPFound, HTTPNotFound |
|
32 | 32 | from pyramid.renderers import render |
|
33 | 33 | from pyramid.response import Response |
|
34 | 34 | |
|
35 | 35 | from rhodecode.apps._base import BaseAppView |
|
36 | 36 | from rhodecode.apps._base.navigation import navigation_list |
|
37 | 37 | from rhodecode.apps.svn_support.config_keys import generate_config |
|
38 | 38 | from rhodecode.lib import helpers as h |
|
39 | 39 | from rhodecode.lib.auth import ( |
|
40 | 40 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) |
|
41 | 41 | from rhodecode.lib.celerylib import tasks, run_task |
|
42 | 42 | from rhodecode.lib.utils import repo2db_mapper |
|
43 | 43 | from rhodecode.lib.utils2 import str2bool, safe_unicode, AttributeDict |
|
44 | 44 | from rhodecode.lib.index import searcher_from_config |
|
45 | 45 | |
|
46 | 46 | from rhodecode.model.db import RhodeCodeUi, Repository |
|
47 | 47 | from rhodecode.model.forms import (ApplicationSettingsForm, |
|
48 | 48 | ApplicationUiSettingsForm, ApplicationVisualisationForm, |
|
49 | 49 | LabsSettingsForm, IssueTrackerPatternsForm) |
|
50 | 50 | from rhodecode.model.repo_group import RepoGroupModel |
|
51 | 51 | |
|
52 | 52 | from rhodecode.model.scm import ScmModel |
|
53 | 53 | from rhodecode.model.notification import EmailNotificationModel |
|
54 | 54 | from rhodecode.model.meta import Session |
|
55 | 55 | from rhodecode.model.settings import ( |
|
56 | 56 | IssueTrackerSettingsModel, VcsSettingsModel, SettingNotFound, |
|
57 | 57 | SettingsModel) |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | log = logging.getLogger(__name__) |
|
61 | 61 | |
|
62 | 62 | |
|
63 | 63 | class AdminSettingsView(BaseAppView): |
|
64 | 64 | |
|
65 | 65 | def load_default_context(self): |
|
66 | 66 | c = self._get_local_tmpl_context() |
|
67 | 67 | c.labs_active = str2bool( |
|
68 | 68 | rhodecode.CONFIG.get('labs_settings_active', 'true')) |
|
69 | 69 | c.navlist = navigation_list(self.request) |
|
70 | 70 | |
|
71 | 71 | return c |
|
72 | 72 | |
|
73 | 73 | @classmethod |
|
74 | 74 | def _get_ui_settings(cls): |
|
75 | 75 | ret = RhodeCodeUi.query().all() |
|
76 | 76 | |
|
77 | 77 | if not ret: |
|
78 | 78 | raise Exception('Could not get application ui settings !') |
|
79 | 79 | settings = {} |
|
80 | 80 | for each in ret: |
|
81 | 81 | k = each.ui_key |
|
82 | 82 | v = each.ui_value |
|
83 | 83 | if k == '/': |
|
84 | 84 | k = 'root_path' |
|
85 | 85 | |
|
86 | 86 | if k in ['push_ssl', 'publish', 'enabled']: |
|
87 | 87 | v = str2bool(v) |
|
88 | 88 | |
|
89 | 89 | if k.find('.') != -1: |
|
90 | 90 | k = k.replace('.', '_') |
|
91 | 91 | |
|
92 | 92 | if each.ui_section in ['hooks', 'extensions']: |
|
93 | 93 | v = each.ui_active |
|
94 | 94 | |
|
95 | 95 | settings[each.ui_section + '_' + k] = v |
|
96 | 96 | return settings |
|
97 | 97 | |
|
98 | 98 | @classmethod |
|
99 | 99 | def _form_defaults(cls): |
|
100 | 100 | defaults = SettingsModel().get_all_settings() |
|
101 | 101 | defaults.update(cls._get_ui_settings()) |
|
102 | 102 | |
|
103 | 103 | defaults.update({ |
|
104 | 104 | 'new_svn_branch': '', |
|
105 | 105 | 'new_svn_tag': '', |
|
106 | 106 | }) |
|
107 | 107 | return defaults |
|
108 | 108 | |
|
109 | 109 | @LoginRequired() |
|
110 | 110 | @HasPermissionAllDecorator('hg.admin') |
|
111 | 111 | @view_config( |
|
112 | 112 | route_name='admin_settings_vcs', request_method='GET', |
|
113 | 113 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
114 | 114 | def settings_vcs(self): |
|
115 | 115 | c = self.load_default_context() |
|
116 | 116 | c.active = 'vcs' |
|
117 | 117 | model = VcsSettingsModel() |
|
118 | 118 | c.svn_branch_patterns = model.get_global_svn_branch_patterns() |
|
119 | 119 | c.svn_tag_patterns = model.get_global_svn_tag_patterns() |
|
120 | 120 | |
|
121 | 121 | settings = self.request.registry.settings |
|
122 | 122 | c.svn_proxy_generate_config = settings[generate_config] |
|
123 | 123 | |
|
124 | 124 | defaults = self._form_defaults() |
|
125 | 125 | |
|
126 | 126 | model.create_largeobjects_dirs_if_needed(defaults['paths_root_path']) |
|
127 | 127 | |
|
128 | 128 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
129 | 129 | self._get_template_context(c), self.request) |
|
130 | 130 | html = formencode.htmlfill.render( |
|
131 | 131 | data, |
|
132 | 132 | defaults=defaults, |
|
133 | 133 | encoding="UTF-8", |
|
134 | 134 | force_defaults=False |
|
135 | 135 | ) |
|
136 | 136 | return Response(html) |
|
137 | 137 | |
|
138 | 138 | @LoginRequired() |
|
139 | 139 | @HasPermissionAllDecorator('hg.admin') |
|
140 | 140 | @CSRFRequired() |
|
141 | 141 | @view_config( |
|
142 | 142 | route_name='admin_settings_vcs_update', request_method='POST', |
|
143 | 143 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
144 | 144 | def settings_vcs_update(self): |
|
145 | 145 | _ = self.request.translate |
|
146 | 146 | c = self.load_default_context() |
|
147 | 147 | c.active = 'vcs' |
|
148 | 148 | |
|
149 | 149 | model = VcsSettingsModel() |
|
150 | 150 | c.svn_branch_patterns = model.get_global_svn_branch_patterns() |
|
151 | 151 | c.svn_tag_patterns = model.get_global_svn_tag_patterns() |
|
152 | 152 | |
|
153 | 153 | settings = self.request.registry.settings |
|
154 | 154 | c.svn_proxy_generate_config = settings[generate_config] |
|
155 | 155 | |
|
156 | 156 | application_form = ApplicationUiSettingsForm(self.request.translate)() |
|
157 | 157 | |
|
158 | 158 | try: |
|
159 | 159 | form_result = application_form.to_python(dict(self.request.POST)) |
|
160 | 160 | except formencode.Invalid as errors: |
|
161 | 161 | h.flash( |
|
162 | 162 | _("Some form inputs contain invalid data."), |
|
163 | 163 | category='error') |
|
164 | 164 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
165 | 165 | self._get_template_context(c), self.request) |
|
166 | 166 | html = formencode.htmlfill.render( |
|
167 | 167 | data, |
|
168 | 168 | defaults=errors.value, |
|
169 | 169 | errors=errors.error_dict or {}, |
|
170 | 170 | prefix_error=False, |
|
171 | 171 | encoding="UTF-8", |
|
172 | 172 | force_defaults=False |
|
173 | 173 | ) |
|
174 | 174 | return Response(html) |
|
175 | 175 | |
|
176 | 176 | try: |
|
177 | 177 | if c.visual.allow_repo_location_change: |
|
178 | 178 | model.update_global_path_setting(form_result['paths_root_path']) |
|
179 | 179 | |
|
180 | 180 | model.update_global_ssl_setting(form_result['web_push_ssl']) |
|
181 | 181 | model.update_global_hook_settings(form_result) |
|
182 | 182 | |
|
183 | 183 | model.create_or_update_global_svn_settings(form_result) |
|
184 | 184 | model.create_or_update_global_hg_settings(form_result) |
|
185 | 185 | model.create_or_update_global_git_settings(form_result) |
|
186 | 186 | model.create_or_update_global_pr_settings(form_result) |
|
187 | 187 | except Exception: |
|
188 | 188 | log.exception("Exception while updating settings") |
|
189 | 189 | h.flash(_('Error occurred during updating ' |
|
190 | 190 | 'application settings'), category='error') |
|
191 | 191 | else: |
|
192 | 192 | Session().commit() |
|
193 | 193 | h.flash(_('Updated VCS settings'), category='success') |
|
194 | 194 | raise HTTPFound(h.route_path('admin_settings_vcs')) |
|
195 | 195 | |
|
196 | 196 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
197 | 197 | self._get_template_context(c), self.request) |
|
198 | 198 | html = formencode.htmlfill.render( |
|
199 | 199 | data, |
|
200 | 200 | defaults=self._form_defaults(), |
|
201 | 201 | encoding="UTF-8", |
|
202 | 202 | force_defaults=False |
|
203 | 203 | ) |
|
204 | 204 | return Response(html) |
|
205 | 205 | |
|
206 | 206 | @LoginRequired() |
|
207 | 207 | @HasPermissionAllDecorator('hg.admin') |
|
208 | 208 | @CSRFRequired() |
|
209 | 209 | @view_config( |
|
210 | 210 | route_name='admin_settings_vcs_svn_pattern_delete', request_method='POST', |
|
211 | 211 | renderer='json_ext', xhr=True) |
|
212 | 212 | def settings_vcs_delete_svn_pattern(self): |
|
213 | 213 | delete_pattern_id = self.request.POST.get('delete_svn_pattern') |
|
214 | 214 | model = VcsSettingsModel() |
|
215 | 215 | try: |
|
216 | 216 | model.delete_global_svn_pattern(delete_pattern_id) |
|
217 | 217 | except SettingNotFound: |
|
218 | 218 | log.exception( |
|
219 | 219 | 'Failed to delete svn_pattern with id %s', delete_pattern_id) |
|
220 | 220 | raise HTTPNotFound() |
|
221 | 221 | |
|
222 | 222 | Session().commit() |
|
223 | 223 | return True |
|
224 | 224 | |
|
225 | 225 | @LoginRequired() |
|
226 | 226 | @HasPermissionAllDecorator('hg.admin') |
|
227 | 227 | @view_config( |
|
228 | 228 | route_name='admin_settings_mapping', request_method='GET', |
|
229 | 229 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
230 | 230 | def settings_mapping(self): |
|
231 | 231 | c = self.load_default_context() |
|
232 | 232 | c.active = 'mapping' |
|
233 | 233 | |
|
234 | 234 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
235 | 235 | self._get_template_context(c), self.request) |
|
236 | 236 | html = formencode.htmlfill.render( |
|
237 | 237 | data, |
|
238 | 238 | defaults=self._form_defaults(), |
|
239 | 239 | encoding="UTF-8", |
|
240 | 240 | force_defaults=False |
|
241 | 241 | ) |
|
242 | 242 | return Response(html) |
|
243 | 243 | |
|
244 | 244 | @LoginRequired() |
|
245 | 245 | @HasPermissionAllDecorator('hg.admin') |
|
246 | 246 | @CSRFRequired() |
|
247 | 247 | @view_config( |
|
248 | 248 | route_name='admin_settings_mapping_update', request_method='POST', |
|
249 | 249 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
250 | 250 | def settings_mapping_update(self): |
|
251 | 251 | _ = self.request.translate |
|
252 | 252 | c = self.load_default_context() |
|
253 | 253 | c.active = 'mapping' |
|
254 | 254 | rm_obsolete = self.request.POST.get('destroy', False) |
|
255 | 255 | invalidate_cache = self.request.POST.get('invalidate', False) |
|
256 | 256 | log.debug( |
|
257 | 257 | 'rescanning repo location with destroy obsolete=%s', rm_obsolete) |
|
258 | 258 | |
|
259 | 259 | if invalidate_cache: |
|
260 | 260 | log.debug('invalidating all repositories cache') |
|
261 | 261 | for repo in Repository.get_all(): |
|
262 | 262 | ScmModel().mark_for_invalidation(repo.repo_name, delete=True) |
|
263 | 263 | |
|
264 | 264 | filesystem_repos = ScmModel().repo_scan() |
|
265 | 265 | added, removed = repo2db_mapper(filesystem_repos, rm_obsolete) |
|
266 | 266 | _repr = lambda l: ', '.join(map(safe_unicode, l)) or '-' |
|
267 | 267 | h.flash(_('Repositories successfully ' |
|
268 | 268 | 'rescanned added: %s ; removed: %s') % |
|
269 | 269 | (_repr(added), _repr(removed)), |
|
270 | 270 | category='success') |
|
271 | 271 | raise HTTPFound(h.route_path('admin_settings_mapping')) |
|
272 | 272 | |
|
273 | 273 | @LoginRequired() |
|
274 | 274 | @HasPermissionAllDecorator('hg.admin') |
|
275 | 275 | @view_config( |
|
276 | 276 | route_name='admin_settings', request_method='GET', |
|
277 | 277 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
278 | 278 | @view_config( |
|
279 | 279 | route_name='admin_settings_global', request_method='GET', |
|
280 | 280 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
281 | 281 | def settings_global(self): |
|
282 | 282 | c = self.load_default_context() |
|
283 | 283 | c.active = 'global' |
|
284 | 284 | c.personal_repo_group_default_pattern = RepoGroupModel()\ |
|
285 | 285 | .get_personal_group_name_pattern() |
|
286 | 286 | |
|
287 | 287 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
288 | 288 | self._get_template_context(c), self.request) |
|
289 | 289 | html = formencode.htmlfill.render( |
|
290 | 290 | data, |
|
291 | 291 | defaults=self._form_defaults(), |
|
292 | 292 | encoding="UTF-8", |
|
293 | 293 | force_defaults=False |
|
294 | 294 | ) |
|
295 | 295 | return Response(html) |
|
296 | 296 | |
|
297 | 297 | @LoginRequired() |
|
298 | 298 | @HasPermissionAllDecorator('hg.admin') |
|
299 | 299 | @CSRFRequired() |
|
300 | 300 | @view_config( |
|
301 | 301 | route_name='admin_settings_update', request_method='POST', |
|
302 | 302 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
303 | 303 | @view_config( |
|
304 | 304 | route_name='admin_settings_global_update', request_method='POST', |
|
305 | 305 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
306 | 306 | def settings_global_update(self): |
|
307 | 307 | _ = self.request.translate |
|
308 | 308 | c = self.load_default_context() |
|
309 | 309 | c.active = 'global' |
|
310 | 310 | c.personal_repo_group_default_pattern = RepoGroupModel()\ |
|
311 | 311 | .get_personal_group_name_pattern() |
|
312 | 312 | application_form = ApplicationSettingsForm(self.request.translate)() |
|
313 | 313 | try: |
|
314 | 314 | form_result = application_form.to_python(dict(self.request.POST)) |
|
315 | 315 | except formencode.Invalid as errors: |
|
316 | 316 | h.flash( |
|
317 | 317 | _("Some form inputs contain invalid data."), |
|
318 | 318 | category='error') |
|
319 | 319 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
320 | 320 | self._get_template_context(c), self.request) |
|
321 | 321 | html = formencode.htmlfill.render( |
|
322 | 322 | data, |
|
323 | 323 | defaults=errors.value, |
|
324 | 324 | errors=errors.error_dict or {}, |
|
325 | 325 | prefix_error=False, |
|
326 | 326 | encoding="UTF-8", |
|
327 | 327 | force_defaults=False |
|
328 | 328 | ) |
|
329 | 329 | return Response(html) |
|
330 | 330 | |
|
331 | 331 | settings = [ |
|
332 | 332 | ('title', 'rhodecode_title', 'unicode'), |
|
333 | 333 | ('realm', 'rhodecode_realm', 'unicode'), |
|
334 | 334 | ('pre_code', 'rhodecode_pre_code', 'unicode'), |
|
335 | 335 | ('post_code', 'rhodecode_post_code', 'unicode'), |
|
336 | 336 | ('captcha_public_key', 'rhodecode_captcha_public_key', 'unicode'), |
|
337 | 337 | ('captcha_private_key', 'rhodecode_captcha_private_key', 'unicode'), |
|
338 | 338 | ('create_personal_repo_group', 'rhodecode_create_personal_repo_group', 'bool'), |
|
339 | 339 | ('personal_repo_group_pattern', 'rhodecode_personal_repo_group_pattern', 'unicode'), |
|
340 | 340 | ] |
|
341 | 341 | try: |
|
342 | 342 | for setting, form_key, type_ in settings: |
|
343 | 343 | sett = SettingsModel().create_or_update_setting( |
|
344 | 344 | setting, form_result[form_key], type_) |
|
345 | 345 | Session().add(sett) |
|
346 | 346 | |
|
347 | 347 | Session().commit() |
|
348 | 348 | SettingsModel().invalidate_settings_cache() |
|
349 | 349 | h.flash(_('Updated application settings'), category='success') |
|
350 | 350 | except Exception: |
|
351 | 351 | log.exception("Exception while updating application settings") |
|
352 | 352 | h.flash( |
|
353 | 353 | _('Error occurred during updating application settings'), |
|
354 | 354 | category='error') |
|
355 | 355 | |
|
356 | 356 | raise HTTPFound(h.route_path('admin_settings_global')) |
|
357 | 357 | |
|
358 | 358 | @LoginRequired() |
|
359 | 359 | @HasPermissionAllDecorator('hg.admin') |
|
360 | 360 | @view_config( |
|
361 | 361 | route_name='admin_settings_visual', request_method='GET', |
|
362 | 362 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
363 | 363 | def settings_visual(self): |
|
364 | 364 | c = self.load_default_context() |
|
365 | 365 | c.active = 'visual' |
|
366 | 366 | |
|
367 | 367 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
368 | 368 | self._get_template_context(c), self.request) |
|
369 | 369 | html = formencode.htmlfill.render( |
|
370 | 370 | data, |
|
371 | 371 | defaults=self._form_defaults(), |
|
372 | 372 | encoding="UTF-8", |
|
373 | 373 | force_defaults=False |
|
374 | 374 | ) |
|
375 | 375 | return Response(html) |
|
376 | 376 | |
|
377 | 377 | @LoginRequired() |
|
378 | 378 | @HasPermissionAllDecorator('hg.admin') |
|
379 | 379 | @CSRFRequired() |
|
380 | 380 | @view_config( |
|
381 | 381 | route_name='admin_settings_visual_update', request_method='POST', |
|
382 | 382 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
383 | 383 | def settings_visual_update(self): |
|
384 | 384 | _ = self.request.translate |
|
385 | 385 | c = self.load_default_context() |
|
386 | 386 | c.active = 'visual' |
|
387 | 387 | application_form = ApplicationVisualisationForm(self.request.translate)() |
|
388 | 388 | try: |
|
389 | 389 | form_result = application_form.to_python(dict(self.request.POST)) |
|
390 | 390 | except formencode.Invalid as errors: |
|
391 | 391 | h.flash( |
|
392 | 392 | _("Some form inputs contain invalid data."), |
|
393 | 393 | category='error') |
|
394 | 394 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
395 | 395 | self._get_template_context(c), self.request) |
|
396 | 396 | html = formencode.htmlfill.render( |
|
397 | 397 | data, |
|
398 | 398 | defaults=errors.value, |
|
399 | 399 | errors=errors.error_dict or {}, |
|
400 | 400 | prefix_error=False, |
|
401 | 401 | encoding="UTF-8", |
|
402 | 402 | force_defaults=False |
|
403 | 403 | ) |
|
404 | 404 | return Response(html) |
|
405 | 405 | |
|
406 | 406 | try: |
|
407 | 407 | settings = [ |
|
408 | 408 | ('show_public_icon', 'rhodecode_show_public_icon', 'bool'), |
|
409 | 409 | ('show_private_icon', 'rhodecode_show_private_icon', 'bool'), |
|
410 | 410 | ('stylify_metatags', 'rhodecode_stylify_metatags', 'bool'), |
|
411 | 411 | ('repository_fields', 'rhodecode_repository_fields', 'bool'), |
|
412 | 412 | ('dashboard_items', 'rhodecode_dashboard_items', 'int'), |
|
413 | 413 | ('admin_grid_items', 'rhodecode_admin_grid_items', 'int'), |
|
414 | 414 | ('show_version', 'rhodecode_show_version', 'bool'), |
|
415 | 415 | ('use_gravatar', 'rhodecode_use_gravatar', 'bool'), |
|
416 | 416 | ('markup_renderer', 'rhodecode_markup_renderer', 'unicode'), |
|
417 | 417 | ('gravatar_url', 'rhodecode_gravatar_url', 'unicode'), |
|
418 | 418 | ('clone_uri_tmpl', 'rhodecode_clone_uri_tmpl', 'unicode'), |
|
419 | 419 | ('clone_uri_ssh_tmpl', 'rhodecode_clone_uri_ssh_tmpl', 'unicode'), |
|
420 | 420 | ('support_url', 'rhodecode_support_url', 'unicode'), |
|
421 | 421 | ('show_revision_number', 'rhodecode_show_revision_number', 'bool'), |
|
422 | 422 | ('show_sha_length', 'rhodecode_show_sha_length', 'int'), |
|
423 | 423 | ] |
|
424 | 424 | for setting, form_key, type_ in settings: |
|
425 | 425 | sett = SettingsModel().create_or_update_setting( |
|
426 | 426 | setting, form_result[form_key], type_) |
|
427 | 427 | Session().add(sett) |
|
428 | 428 | |
|
429 | 429 | Session().commit() |
|
430 | 430 | SettingsModel().invalidate_settings_cache() |
|
431 | 431 | h.flash(_('Updated visualisation settings'), category='success') |
|
432 | 432 | except Exception: |
|
433 | 433 | log.exception("Exception updating visualization settings") |
|
434 | 434 | h.flash(_('Error occurred during updating ' |
|
435 | 435 | 'visualisation settings'), |
|
436 | 436 | category='error') |
|
437 | 437 | |
|
438 | 438 | raise HTTPFound(h.route_path('admin_settings_visual')) |
|
439 | 439 | |
|
440 | 440 | @LoginRequired() |
|
441 | 441 | @HasPermissionAllDecorator('hg.admin') |
|
442 | 442 | @view_config( |
|
443 | 443 | route_name='admin_settings_issuetracker', request_method='GET', |
|
444 | 444 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
445 | 445 | def settings_issuetracker(self): |
|
446 | 446 | c = self.load_default_context() |
|
447 | 447 | c.active = 'issuetracker' |
|
448 | 448 | defaults = c.rc_config |
|
449 | 449 | |
|
450 | 450 | entry_key = 'rhodecode_issuetracker_pat_' |
|
451 | 451 | |
|
452 | 452 | c.issuetracker_entries = {} |
|
453 | 453 | for k, v in defaults.items(): |
|
454 | 454 | if k.startswith(entry_key): |
|
455 | 455 | uid = k[len(entry_key):] |
|
456 | 456 | c.issuetracker_entries[uid] = None |
|
457 | 457 | |
|
458 | 458 | for uid in c.issuetracker_entries: |
|
459 | 459 | c.issuetracker_entries[uid] = AttributeDict({ |
|
460 | 460 | 'pat': defaults.get('rhodecode_issuetracker_pat_' + uid), |
|
461 | 461 | 'url': defaults.get('rhodecode_issuetracker_url_' + uid), |
|
462 | 462 | 'pref': defaults.get('rhodecode_issuetracker_pref_' + uid), |
|
463 | 463 | 'desc': defaults.get('rhodecode_issuetracker_desc_' + uid), |
|
464 | 464 | }) |
|
465 | 465 | |
|
466 | 466 | return self._get_template_context(c) |
|
467 | 467 | |
|
468 | 468 | @LoginRequired() |
|
469 | 469 | @HasPermissionAllDecorator('hg.admin') |
|
470 | 470 | @CSRFRequired() |
|
471 | 471 | @view_config( |
|
472 | 472 | route_name='admin_settings_issuetracker_test', request_method='POST', |
|
473 | 473 | renderer='string', xhr=True) |
|
474 | 474 | def settings_issuetracker_test(self): |
|
475 | 475 | return h.urlify_commit_message( |
|
476 | 476 | self.request.POST.get('test_text', ''), |
|
477 | 477 | 'repo_group/test_repo1') |
|
478 | 478 | |
|
479 | 479 | @LoginRequired() |
|
480 | 480 | @HasPermissionAllDecorator('hg.admin') |
|
481 | 481 | @CSRFRequired() |
|
482 | 482 | @view_config( |
|
483 | 483 | route_name='admin_settings_issuetracker_update', request_method='POST', |
|
484 | 484 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
485 | 485 | def settings_issuetracker_update(self): |
|
486 | 486 | _ = self.request.translate |
|
487 | 487 | self.load_default_context() |
|
488 | 488 | settings_model = IssueTrackerSettingsModel() |
|
489 | 489 | |
|
490 | 490 | try: |
|
491 | 491 | form = IssueTrackerPatternsForm(self.request.translate)() |
|
492 | 492 | data = form.to_python(self.request.POST) |
|
493 | 493 | except formencode.Invalid as errors: |
|
494 | 494 | log.exception('Failed to add new pattern') |
|
495 | 495 | error = errors |
|
496 | 496 | h.flash(_('Invalid issue tracker pattern: {}'.format(error)), |
|
497 | 497 | category='error') |
|
498 | 498 | raise HTTPFound(h.route_path('admin_settings_issuetracker')) |
|
499 | 499 | |
|
500 | 500 | if data: |
|
501 | 501 | for uid in data.get('delete_patterns', []): |
|
502 | 502 | settings_model.delete_entries(uid) |
|
503 | 503 | |
|
504 | 504 | for pattern in data.get('patterns', []): |
|
505 | 505 | for setting, value, type_ in pattern: |
|
506 | 506 | sett = settings_model.create_or_update_setting( |
|
507 | 507 | setting, value, type_) |
|
508 | 508 | Session().add(sett) |
|
509 | 509 | |
|
510 | 510 | Session().commit() |
|
511 | 511 | |
|
512 | 512 | SettingsModel().invalidate_settings_cache() |
|
513 | 513 | h.flash(_('Updated issue tracker entries'), category='success') |
|
514 | 514 | raise HTTPFound(h.route_path('admin_settings_issuetracker')) |
|
515 | 515 | |
|
516 | 516 | @LoginRequired() |
|
517 | 517 | @HasPermissionAllDecorator('hg.admin') |
|
518 | 518 | @CSRFRequired() |
|
519 | 519 | @view_config( |
|
520 | 520 | route_name='admin_settings_issuetracker_delete', request_method='POST', |
|
521 | 521 | renderer='json_ext', xhr=True) |
|
522 | 522 | def settings_issuetracker_delete(self): |
|
523 | 523 | _ = self.request.translate |
|
524 | 524 | self.load_default_context() |
|
525 | 525 | uid = self.request.POST.get('uid') |
|
526 | 526 | try: |
|
527 | 527 | IssueTrackerSettingsModel().delete_entries(uid) |
|
528 | 528 | except Exception: |
|
529 | 529 | log.exception('Failed to delete issue tracker setting %s', uid) |
|
530 | 530 | raise HTTPNotFound() |
|
531 | 531 | |
|
532 | 532 | SettingsModel().invalidate_settings_cache() |
|
533 | 533 | h.flash(_('Removed issue tracker entry.'), category='success') |
|
534 | 534 | |
|
535 | 535 | return {'deleted': uid} |
|
536 | 536 | |
|
537 | 537 | @LoginRequired() |
|
538 | 538 | @HasPermissionAllDecorator('hg.admin') |
|
539 | 539 | @view_config( |
|
540 | 540 | route_name='admin_settings_email', request_method='GET', |
|
541 | 541 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
542 | 542 | def settings_email(self): |
|
543 | 543 | c = self.load_default_context() |
|
544 | 544 | c.active = 'email' |
|
545 | 545 | c.rhodecode_ini = rhodecode.CONFIG |
|
546 | 546 | |
|
547 | 547 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
548 | 548 | self._get_template_context(c), self.request) |
|
549 | 549 | html = formencode.htmlfill.render( |
|
550 | 550 | data, |
|
551 | 551 | defaults=self._form_defaults(), |
|
552 | 552 | encoding="UTF-8", |
|
553 | 553 | force_defaults=False |
|
554 | 554 | ) |
|
555 | 555 | return Response(html) |
|
556 | 556 | |
|
557 | 557 | @LoginRequired() |
|
558 | 558 | @HasPermissionAllDecorator('hg.admin') |
|
559 | 559 | @CSRFRequired() |
|
560 | 560 | @view_config( |
|
561 | 561 | route_name='admin_settings_email_update', request_method='POST', |
|
562 | 562 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
563 | 563 | def settings_email_update(self): |
|
564 | 564 | _ = self.request.translate |
|
565 | 565 | c = self.load_default_context() |
|
566 | 566 | c.active = 'email' |
|
567 | 567 | |
|
568 | 568 | test_email = self.request.POST.get('test_email') |
|
569 | 569 | |
|
570 | 570 | if not test_email: |
|
571 | 571 | h.flash(_('Please enter email address'), category='error') |
|
572 | 572 | raise HTTPFound(h.route_path('admin_settings_email')) |
|
573 | 573 | |
|
574 | 574 | email_kwargs = { |
|
575 | 575 | 'date': datetime.datetime.now(), |
|
576 |
'user': |
|
|
576 | 'user': self._rhodecode_db_user | |
|
577 | 577 | } |
|
578 | 578 | |
|
579 | 579 | (subject, headers, email_body, |
|
580 | 580 | email_body_plaintext) = EmailNotificationModel().render_email( |
|
581 | 581 | EmailNotificationModel.TYPE_EMAIL_TEST, **email_kwargs) |
|
582 | 582 | |
|
583 | 583 | recipients = [test_email] if test_email else None |
|
584 | 584 | |
|
585 | 585 | run_task(tasks.send_email, recipients, subject, |
|
586 | 586 | email_body_plaintext, email_body) |
|
587 | 587 | |
|
588 | 588 | h.flash(_('Send email task created'), category='success') |
|
589 | 589 | raise HTTPFound(h.route_path('admin_settings_email')) |
|
590 | 590 | |
|
591 | 591 | @LoginRequired() |
|
592 | 592 | @HasPermissionAllDecorator('hg.admin') |
|
593 | 593 | @view_config( |
|
594 | 594 | route_name='admin_settings_hooks', request_method='GET', |
|
595 | 595 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
596 | 596 | def settings_hooks(self): |
|
597 | 597 | c = self.load_default_context() |
|
598 | 598 | c.active = 'hooks' |
|
599 | 599 | |
|
600 | 600 | model = SettingsModel() |
|
601 | 601 | c.hooks = model.get_builtin_hooks() |
|
602 | 602 | c.custom_hooks = model.get_custom_hooks() |
|
603 | 603 | |
|
604 | 604 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
605 | 605 | self._get_template_context(c), self.request) |
|
606 | 606 | html = formencode.htmlfill.render( |
|
607 | 607 | data, |
|
608 | 608 | defaults=self._form_defaults(), |
|
609 | 609 | encoding="UTF-8", |
|
610 | 610 | force_defaults=False |
|
611 | 611 | ) |
|
612 | 612 | return Response(html) |
|
613 | 613 | |
|
614 | 614 | @LoginRequired() |
|
615 | 615 | @HasPermissionAllDecorator('hg.admin') |
|
616 | 616 | @CSRFRequired() |
|
617 | 617 | @view_config( |
|
618 | 618 | route_name='admin_settings_hooks_update', request_method='POST', |
|
619 | 619 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
620 | 620 | @view_config( |
|
621 | 621 | route_name='admin_settings_hooks_delete', request_method='POST', |
|
622 | 622 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
623 | 623 | def settings_hooks_update(self): |
|
624 | 624 | _ = self.request.translate |
|
625 | 625 | c = self.load_default_context() |
|
626 | 626 | c.active = 'hooks' |
|
627 | 627 | if c.visual.allow_custom_hooks_settings: |
|
628 | 628 | ui_key = self.request.POST.get('new_hook_ui_key') |
|
629 | 629 | ui_value = self.request.POST.get('new_hook_ui_value') |
|
630 | 630 | |
|
631 | 631 | hook_id = self.request.POST.get('hook_id') |
|
632 | 632 | new_hook = False |
|
633 | 633 | |
|
634 | 634 | model = SettingsModel() |
|
635 | 635 | try: |
|
636 | 636 | if ui_value and ui_key: |
|
637 | 637 | model.create_or_update_hook(ui_key, ui_value) |
|
638 | 638 | h.flash(_('Added new hook'), category='success') |
|
639 | 639 | new_hook = True |
|
640 | 640 | elif hook_id: |
|
641 | 641 | RhodeCodeUi.delete(hook_id) |
|
642 | 642 | Session().commit() |
|
643 | 643 | |
|
644 | 644 | # check for edits |
|
645 | 645 | update = False |
|
646 | 646 | _d = self.request.POST.dict_of_lists() |
|
647 | 647 | for k, v in zip(_d.get('hook_ui_key', []), |
|
648 | 648 | _d.get('hook_ui_value_new', [])): |
|
649 | 649 | model.create_or_update_hook(k, v) |
|
650 | 650 | update = True |
|
651 | 651 | |
|
652 | 652 | if update and not new_hook: |
|
653 | 653 | h.flash(_('Updated hooks'), category='success') |
|
654 | 654 | Session().commit() |
|
655 | 655 | except Exception: |
|
656 | 656 | log.exception("Exception during hook creation") |
|
657 | 657 | h.flash(_('Error occurred during hook creation'), |
|
658 | 658 | category='error') |
|
659 | 659 | |
|
660 | 660 | raise HTTPFound(h.route_path('admin_settings_hooks')) |
|
661 | 661 | |
|
662 | 662 | @LoginRequired() |
|
663 | 663 | @HasPermissionAllDecorator('hg.admin') |
|
664 | 664 | @view_config( |
|
665 | 665 | route_name='admin_settings_search', request_method='GET', |
|
666 | 666 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
667 | 667 | def settings_search(self): |
|
668 | 668 | c = self.load_default_context() |
|
669 | 669 | c.active = 'search' |
|
670 | 670 | |
|
671 | 671 | c.searcher = searcher_from_config(self.request.registry.settings) |
|
672 | 672 | c.statistics = c.searcher.statistics(self.request.translate) |
|
673 | 673 | |
|
674 | 674 | return self._get_template_context(c) |
|
675 | 675 | |
|
676 | 676 | @LoginRequired() |
|
677 | 677 | @HasPermissionAllDecorator('hg.admin') |
|
678 | 678 | @view_config( |
|
679 | 679 | route_name='admin_settings_automation', request_method='GET', |
|
680 | 680 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
681 | 681 | def settings_automation(self): |
|
682 | 682 | c = self.load_default_context() |
|
683 | 683 | c.active = 'automation' |
|
684 | 684 | |
|
685 | 685 | return self._get_template_context(c) |
|
686 | 686 | |
|
687 | 687 | @LoginRequired() |
|
688 | 688 | @HasPermissionAllDecorator('hg.admin') |
|
689 | 689 | @view_config( |
|
690 | 690 | route_name='admin_settings_labs', request_method='GET', |
|
691 | 691 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
692 | 692 | def settings_labs(self): |
|
693 | 693 | c = self.load_default_context() |
|
694 | 694 | if not c.labs_active: |
|
695 | 695 | raise HTTPFound(h.route_path('admin_settings')) |
|
696 | 696 | |
|
697 | 697 | c.active = 'labs' |
|
698 | 698 | c.lab_settings = _LAB_SETTINGS |
|
699 | 699 | |
|
700 | 700 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
701 | 701 | self._get_template_context(c), self.request) |
|
702 | 702 | html = formencode.htmlfill.render( |
|
703 | 703 | data, |
|
704 | 704 | defaults=self._form_defaults(), |
|
705 | 705 | encoding="UTF-8", |
|
706 | 706 | force_defaults=False |
|
707 | 707 | ) |
|
708 | 708 | return Response(html) |
|
709 | 709 | |
|
710 | 710 | @LoginRequired() |
|
711 | 711 | @HasPermissionAllDecorator('hg.admin') |
|
712 | 712 | @CSRFRequired() |
|
713 | 713 | @view_config( |
|
714 | 714 | route_name='admin_settings_labs_update', request_method='POST', |
|
715 | 715 | renderer='rhodecode:templates/admin/settings/settings.mako') |
|
716 | 716 | def settings_labs_update(self): |
|
717 | 717 | _ = self.request.translate |
|
718 | 718 | c = self.load_default_context() |
|
719 | 719 | c.active = 'labs' |
|
720 | 720 | |
|
721 | 721 | application_form = LabsSettingsForm(self.request.translate)() |
|
722 | 722 | try: |
|
723 | 723 | form_result = application_form.to_python(dict(self.request.POST)) |
|
724 | 724 | except formencode.Invalid as errors: |
|
725 | 725 | h.flash( |
|
726 | 726 | _("Some form inputs contain invalid data."), |
|
727 | 727 | category='error') |
|
728 | 728 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
729 | 729 | self._get_template_context(c), self.request) |
|
730 | 730 | html = formencode.htmlfill.render( |
|
731 | 731 | data, |
|
732 | 732 | defaults=errors.value, |
|
733 | 733 | errors=errors.error_dict or {}, |
|
734 | 734 | prefix_error=False, |
|
735 | 735 | encoding="UTF-8", |
|
736 | 736 | force_defaults=False |
|
737 | 737 | ) |
|
738 | 738 | return Response(html) |
|
739 | 739 | |
|
740 | 740 | try: |
|
741 | 741 | session = Session() |
|
742 | 742 | for setting in _LAB_SETTINGS: |
|
743 | 743 | setting_name = setting.key[len('rhodecode_'):] |
|
744 | 744 | sett = SettingsModel().create_or_update_setting( |
|
745 | 745 | setting_name, form_result[setting.key], setting.type) |
|
746 | 746 | session.add(sett) |
|
747 | 747 | |
|
748 | 748 | except Exception: |
|
749 | 749 | log.exception('Exception while updating lab settings') |
|
750 | 750 | h.flash(_('Error occurred during updating labs settings'), |
|
751 | 751 | category='error') |
|
752 | 752 | else: |
|
753 | 753 | Session().commit() |
|
754 | 754 | SettingsModel().invalidate_settings_cache() |
|
755 | 755 | h.flash(_('Updated Labs settings'), category='success') |
|
756 | 756 | raise HTTPFound(h.route_path('admin_settings_labs')) |
|
757 | 757 | |
|
758 | 758 | data = render('rhodecode:templates/admin/settings/settings.mako', |
|
759 | 759 | self._get_template_context(c), self.request) |
|
760 | 760 | html = formencode.htmlfill.render( |
|
761 | 761 | data, |
|
762 | 762 | defaults=self._form_defaults(), |
|
763 | 763 | encoding="UTF-8", |
|
764 | 764 | force_defaults=False |
|
765 | 765 | ) |
|
766 | 766 | return Response(html) |
|
767 | 767 | |
|
768 | 768 | |
|
769 | 769 | # :param key: name of the setting including the 'rhodecode_' prefix |
|
770 | 770 | # :param type: the RhodeCodeSetting type to use. |
|
771 | 771 | # :param group: the i18ned group in which we should dispaly this setting |
|
772 | 772 | # :param label: the i18ned label we should display for this setting |
|
773 | 773 | # :param help: the i18ned help we should dispaly for this setting |
|
774 | 774 | LabSetting = collections.namedtuple( |
|
775 | 775 | 'LabSetting', ('key', 'type', 'group', 'label', 'help')) |
|
776 | 776 | |
|
777 | 777 | |
|
778 | 778 | # This list has to be kept in sync with the form |
|
779 | 779 | # rhodecode.model.forms.LabsSettingsForm. |
|
780 | 780 | _LAB_SETTINGS = [ |
|
781 | 781 | |
|
782 | 782 | ] |
@@ -1,1333 +1,1336 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | import datetime |
|
23 | 23 | import formencode |
|
24 | 24 | import formencode.htmlfill |
|
25 | 25 | |
|
26 | 26 | from pyramid.httpexceptions import HTTPFound |
|
27 | 27 | from pyramid.view import view_config |
|
28 | 28 | from pyramid.renderers import render |
|
29 | 29 | from pyramid.response import Response |
|
30 | 30 | |
|
31 | 31 | from rhodecode import events |
|
32 | 32 | from rhodecode.apps._base import BaseAppView, DataGridAppView, UserAppView |
|
33 | 33 | from rhodecode.apps.ssh_support import SshKeyFileChangeEvent |
|
34 | 34 | from rhodecode.authentication.base import get_authn_registry, RhodeCodeExternalAuthPlugin |
|
35 | 35 | from rhodecode.authentication.plugins import auth_rhodecode |
|
36 | 36 | from rhodecode.events import trigger |
|
37 | 37 | from rhodecode.model.db import true |
|
38 | 38 | |
|
39 | 39 | from rhodecode.lib import audit_logger, rc_cache |
|
40 | 40 | from rhodecode.lib.exceptions import ( |
|
41 | 41 | UserCreationError, UserOwnsReposException, UserOwnsRepoGroupsException, |
|
42 | 42 | UserOwnsUserGroupsException, DefaultUserException) |
|
43 | 43 | from rhodecode.lib.ext_json import json |
|
44 | 44 | from rhodecode.lib.auth import ( |
|
45 | 45 | LoginRequired, HasPermissionAllDecorator, CSRFRequired) |
|
46 | 46 | from rhodecode.lib import helpers as h |
|
47 | 47 | from rhodecode.lib.helpers import SqlPage |
|
48 | 48 | from rhodecode.lib.utils2 import safe_int, safe_unicode, AttributeDict |
|
49 | 49 | from rhodecode.model.auth_token import AuthTokenModel |
|
50 | 50 | from rhodecode.model.forms import ( |
|
51 | 51 | UserForm, UserIndividualPermissionsForm, UserPermissionsForm, |
|
52 | 52 | UserExtraEmailForm, UserExtraIpForm) |
|
53 | 53 | from rhodecode.model.permission import PermissionModel |
|
54 | 54 | from rhodecode.model.repo_group import RepoGroupModel |
|
55 | 55 | from rhodecode.model.ssh_key import SshKeyModel |
|
56 | 56 | from rhodecode.model.user import UserModel |
|
57 | 57 | from rhodecode.model.user_group import UserGroupModel |
|
58 | 58 | from rhodecode.model.db import ( |
|
59 | 59 | or_, coalesce,IntegrityError, User, UserGroup, UserIpMap, UserEmailMap, |
|
60 | 60 | UserApiKeys, UserSshKeys, RepoGroup) |
|
61 | 61 | from rhodecode.model.meta import Session |
|
62 | 62 | |
|
63 | 63 | log = logging.getLogger(__name__) |
|
64 | 64 | |
|
65 | 65 | |
|
66 | 66 | class AdminUsersView(BaseAppView, DataGridAppView): |
|
67 | 67 | |
|
68 | 68 | def load_default_context(self): |
|
69 | 69 | c = self._get_local_tmpl_context() |
|
70 | 70 | return c |
|
71 | 71 | |
|
72 | 72 | @LoginRequired() |
|
73 | 73 | @HasPermissionAllDecorator('hg.admin') |
|
74 | 74 | @view_config( |
|
75 | 75 | route_name='users', request_method='GET', |
|
76 | 76 | renderer='rhodecode:templates/admin/users/users.mako') |
|
77 | 77 | def users_list(self): |
|
78 | 78 | c = self.load_default_context() |
|
79 | 79 | return self._get_template_context(c) |
|
80 | 80 | |
|
81 | 81 | @LoginRequired() |
|
82 | 82 | @HasPermissionAllDecorator('hg.admin') |
|
83 | 83 | @view_config( |
|
84 | 84 | # renderer defined below |
|
85 | 85 | route_name='users_data', request_method='GET', |
|
86 | 86 | renderer='json_ext', xhr=True) |
|
87 | 87 | def users_list_data(self): |
|
88 | 88 | self.load_default_context() |
|
89 | 89 | column_map = { |
|
90 | 90 | 'first_name': 'name', |
|
91 | 91 | 'last_name': 'lastname', |
|
92 | 92 | } |
|
93 | 93 | draw, start, limit = self._extract_chunk(self.request) |
|
94 | 94 | search_q, order_by, order_dir = self._extract_ordering( |
|
95 | 95 | self.request, column_map=column_map) |
|
96 | 96 | _render = self.request.get_partial_renderer( |
|
97 | 97 | 'rhodecode:templates/data_table/_dt_elements.mako') |
|
98 | 98 | |
|
99 | 99 | def user_actions(user_id, username): |
|
100 | 100 | return _render("user_actions", user_id, username) |
|
101 | 101 | |
|
102 | 102 | users_data_total_count = User.query()\ |
|
103 | 103 | .filter(User.username != User.DEFAULT_USER) \ |
|
104 | 104 | .count() |
|
105 | 105 | |
|
106 | 106 | users_data_total_inactive_count = User.query()\ |
|
107 | 107 | .filter(User.username != User.DEFAULT_USER) \ |
|
108 | 108 | .filter(User.active != true())\ |
|
109 | 109 | .count() |
|
110 | 110 | |
|
111 | 111 | # json generate |
|
112 | 112 | base_q = User.query().filter(User.username != User.DEFAULT_USER) |
|
113 | 113 | base_inactive_q = base_q.filter(User.active != true()) |
|
114 | 114 | |
|
115 | 115 | if search_q: |
|
116 | 116 | like_expression = u'%{}%'.format(safe_unicode(search_q)) |
|
117 | 117 | base_q = base_q.filter(or_( |
|
118 | 118 | User.username.ilike(like_expression), |
|
119 | 119 | User._email.ilike(like_expression), |
|
120 | 120 | User.name.ilike(like_expression), |
|
121 | 121 | User.lastname.ilike(like_expression), |
|
122 | 122 | )) |
|
123 | 123 | base_inactive_q = base_q.filter(User.active != true()) |
|
124 | 124 | |
|
125 | 125 | users_data_total_filtered_count = base_q.count() |
|
126 | 126 | users_data_total_filtered_inactive_count = base_inactive_q.count() |
|
127 | 127 | |
|
128 | 128 | sort_col = getattr(User, order_by, None) |
|
129 | 129 | if sort_col: |
|
130 | 130 | if order_dir == 'asc': |
|
131 | 131 | # handle null values properly to order by NULL last |
|
132 | 132 | if order_by in ['last_activity']: |
|
133 | 133 | sort_col = coalesce(sort_col, datetime.date.max) |
|
134 | 134 | sort_col = sort_col.asc() |
|
135 | 135 | else: |
|
136 | 136 | # handle null values properly to order by NULL last |
|
137 | 137 | if order_by in ['last_activity']: |
|
138 | 138 | sort_col = coalesce(sort_col, datetime.date.min) |
|
139 | 139 | sort_col = sort_col.desc() |
|
140 | 140 | |
|
141 | 141 | base_q = base_q.order_by(sort_col) |
|
142 | 142 | base_q = base_q.offset(start).limit(limit) |
|
143 | 143 | |
|
144 | 144 | users_list = base_q.all() |
|
145 | 145 | |
|
146 | 146 | users_data = [] |
|
147 | 147 | for user in users_list: |
|
148 | 148 | users_data.append({ |
|
149 | 149 | "username": h.gravatar_with_user(self.request, user.username), |
|
150 | 150 | "email": user.email, |
|
151 | 151 | "first_name": user.first_name, |
|
152 | 152 | "last_name": user.last_name, |
|
153 | 153 | "last_login": h.format_date(user.last_login), |
|
154 | 154 | "last_activity": h.format_date(user.last_activity), |
|
155 | 155 | "active": h.bool2icon(user.active), |
|
156 | 156 | "active_raw": user.active, |
|
157 | 157 | "admin": h.bool2icon(user.admin), |
|
158 | 158 | "extern_type": user.extern_type, |
|
159 | 159 | "extern_name": user.extern_name, |
|
160 | 160 | "action": user_actions(user.user_id, user.username), |
|
161 | 161 | }) |
|
162 | 162 | data = ({ |
|
163 | 163 | 'draw': draw, |
|
164 | 164 | 'data': users_data, |
|
165 | 165 | 'recordsTotal': users_data_total_count, |
|
166 | 166 | 'recordsFiltered': users_data_total_filtered_count, |
|
167 | 167 | 'recordsTotalInactive': users_data_total_inactive_count, |
|
168 | 168 | 'recordsFilteredInactive': users_data_total_filtered_inactive_count |
|
169 | 169 | }) |
|
170 | 170 | |
|
171 | 171 | return data |
|
172 | 172 | |
|
173 | 173 | def _set_personal_repo_group_template_vars(self, c_obj): |
|
174 | 174 | DummyUser = AttributeDict({ |
|
175 | 175 | 'username': '${username}', |
|
176 | 176 | 'user_id': '${user_id}', |
|
177 | 177 | }) |
|
178 | 178 | c_obj.default_create_repo_group = RepoGroupModel() \ |
|
179 | 179 | .get_default_create_personal_repo_group() |
|
180 | 180 | c_obj.personal_repo_group_name = RepoGroupModel() \ |
|
181 | 181 | .get_personal_group_name(DummyUser) |
|
182 | 182 | |
|
183 | 183 | @LoginRequired() |
|
184 | 184 | @HasPermissionAllDecorator('hg.admin') |
|
185 | 185 | @view_config( |
|
186 | 186 | route_name='users_new', request_method='GET', |
|
187 | 187 | renderer='rhodecode:templates/admin/users/user_add.mako') |
|
188 | 188 | def users_new(self): |
|
189 | 189 | _ = self.request.translate |
|
190 | 190 | c = self.load_default_context() |
|
191 | 191 | c.default_extern_type = auth_rhodecode.RhodeCodeAuthPlugin.uid |
|
192 | 192 | self._set_personal_repo_group_template_vars(c) |
|
193 | 193 | return self._get_template_context(c) |
|
194 | 194 | |
|
195 | 195 | @LoginRequired() |
|
196 | 196 | @HasPermissionAllDecorator('hg.admin') |
|
197 | 197 | @CSRFRequired() |
|
198 | 198 | @view_config( |
|
199 | 199 | route_name='users_create', request_method='POST', |
|
200 | 200 | renderer='rhodecode:templates/admin/users/user_add.mako') |
|
201 | 201 | def users_create(self): |
|
202 | 202 | _ = self.request.translate |
|
203 | 203 | c = self.load_default_context() |
|
204 | 204 | c.default_extern_type = auth_rhodecode.RhodeCodeAuthPlugin.uid |
|
205 | 205 | user_model = UserModel() |
|
206 | 206 | user_form = UserForm(self.request.translate)() |
|
207 | 207 | try: |
|
208 | 208 | form_result = user_form.to_python(dict(self.request.POST)) |
|
209 | 209 | user = user_model.create(form_result) |
|
210 | 210 | Session().flush() |
|
211 | 211 | creation_data = user.get_api_data() |
|
212 | 212 | username = form_result['username'] |
|
213 | 213 | |
|
214 | 214 | audit_logger.store_web( |
|
215 | 215 | 'user.create', action_data={'data': creation_data}, |
|
216 | 216 | user=c.rhodecode_user) |
|
217 | 217 | |
|
218 | 218 | user_link = h.link_to( |
|
219 | 219 | h.escape(username), |
|
220 | 220 | h.route_path('user_edit', user_id=user.user_id)) |
|
221 | 221 | h.flash(h.literal(_('Created user %(user_link)s') |
|
222 | 222 | % {'user_link': user_link}), category='success') |
|
223 | 223 | Session().commit() |
|
224 | 224 | except formencode.Invalid as errors: |
|
225 | 225 | self._set_personal_repo_group_template_vars(c) |
|
226 | 226 | data = render( |
|
227 | 227 | 'rhodecode:templates/admin/users/user_add.mako', |
|
228 | 228 | self._get_template_context(c), self.request) |
|
229 | 229 | html = formencode.htmlfill.render( |
|
230 | 230 | data, |
|
231 | 231 | defaults=errors.value, |
|
232 | 232 | errors=errors.error_dict or {}, |
|
233 | 233 | prefix_error=False, |
|
234 | 234 | encoding="UTF-8", |
|
235 | 235 | force_defaults=False |
|
236 | 236 | ) |
|
237 | 237 | return Response(html) |
|
238 | 238 | except UserCreationError as e: |
|
239 | 239 | h.flash(e, 'error') |
|
240 | 240 | except Exception: |
|
241 | 241 | log.exception("Exception creation of user") |
|
242 | 242 | h.flash(_('Error occurred during creation of user %s') |
|
243 | 243 | % self.request.POST.get('username'), category='error') |
|
244 | 244 | raise HTTPFound(h.route_path('users')) |
|
245 | 245 | |
|
246 | 246 | |
|
247 | 247 | class UsersView(UserAppView): |
|
248 | 248 | ALLOW_SCOPED_TOKENS = False |
|
249 | 249 | """ |
|
250 | 250 | This view has alternative version inside EE, if modified please take a look |
|
251 | 251 | in there as well. |
|
252 | 252 | """ |
|
253 | 253 | |
|
254 | 254 | def get_auth_plugins(self): |
|
255 | 255 | valid_plugins = [] |
|
256 | 256 | authn_registry = get_authn_registry(self.request.registry) |
|
257 | 257 | for plugin in authn_registry.get_plugins_for_authentication(): |
|
258 | 258 | if isinstance(plugin, RhodeCodeExternalAuthPlugin): |
|
259 | 259 | valid_plugins.append(plugin) |
|
260 | 260 | elif plugin.name == 'rhodecode': |
|
261 | 261 | valid_plugins.append(plugin) |
|
262 | 262 | |
|
263 | 263 | # extend our choices if user has set a bound plugin which isn't enabled at the |
|
264 | 264 | # moment |
|
265 | 265 | extern_type = self.db_user.extern_type |
|
266 | 266 | if extern_type not in [x.uid for x in valid_plugins]: |
|
267 | 267 | try: |
|
268 | 268 | plugin = authn_registry.get_plugin_by_uid(extern_type) |
|
269 | 269 | if plugin: |
|
270 | 270 | valid_plugins.append(plugin) |
|
271 | 271 | |
|
272 | 272 | except Exception: |
|
273 | 273 | log.exception( |
|
274 | 274 | 'Could not extend user plugins with `{}`'.format(extern_type)) |
|
275 | 275 | return valid_plugins |
|
276 | 276 | |
|
277 | 277 | def load_default_context(self): |
|
278 | 278 | req = self.request |
|
279 | 279 | |
|
280 | 280 | c = self._get_local_tmpl_context() |
|
281 | 281 | c.allow_scoped_tokens = self.ALLOW_SCOPED_TOKENS |
|
282 | 282 | c.allowed_languages = [ |
|
283 | 283 | ('en', 'English (en)'), |
|
284 | 284 | ('de', 'German (de)'), |
|
285 | 285 | ('fr', 'French (fr)'), |
|
286 | 286 | ('it', 'Italian (it)'), |
|
287 | 287 | ('ja', 'Japanese (ja)'), |
|
288 | 288 | ('pl', 'Polish (pl)'), |
|
289 | 289 | ('pt', 'Portuguese (pt)'), |
|
290 | 290 | ('ru', 'Russian (ru)'), |
|
291 | 291 | ('zh', 'Chinese (zh)'), |
|
292 | 292 | ] |
|
293 | 293 | |
|
294 | 294 | c.allowed_extern_types = [ |
|
295 | 295 | (x.uid, x.get_display_name()) for x in self.get_auth_plugins() |
|
296 | 296 | ] |
|
297 | 297 | |
|
298 | 298 | c.available_permissions = req.registry.settings['available_permissions'] |
|
299 | 299 | PermissionModel().set_global_permission_choices( |
|
300 | 300 | c, gettext_translator=req.translate) |
|
301 | 301 | |
|
302 | 302 | return c |
|
303 | 303 | |
|
304 | 304 | @LoginRequired() |
|
305 | 305 | @HasPermissionAllDecorator('hg.admin') |
|
306 | 306 | @CSRFRequired() |
|
307 | 307 | @view_config( |
|
308 | 308 | route_name='user_update', request_method='POST', |
|
309 | 309 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
310 | 310 | def user_update(self): |
|
311 | 311 | _ = self.request.translate |
|
312 | 312 | c = self.load_default_context() |
|
313 | 313 | |
|
314 | 314 | user_id = self.db_user_id |
|
315 | 315 | c.user = self.db_user |
|
316 | 316 | |
|
317 | 317 | c.active = 'profile' |
|
318 | 318 | c.extern_type = c.user.extern_type |
|
319 | 319 | c.extern_name = c.user.extern_name |
|
320 | 320 | c.perm_user = c.user.AuthUser(ip_addr=self.request.remote_addr) |
|
321 | 321 | available_languages = [x[0] for x in c.allowed_languages] |
|
322 | 322 | _form = UserForm(self.request.translate, edit=True, |
|
323 | 323 | available_languages=available_languages, |
|
324 | 324 | old_data={'user_id': user_id, |
|
325 | 325 | 'email': c.user.email})() |
|
326 | 326 | form_result = {} |
|
327 | 327 | old_values = c.user.get_api_data() |
|
328 | 328 | try: |
|
329 | 329 | form_result = _form.to_python(dict(self.request.POST)) |
|
330 | 330 | skip_attrs = ['extern_name'] |
|
331 | 331 | # TODO: plugin should define if username can be updated |
|
332 | 332 | if c.extern_type != "rhodecode": |
|
333 | 333 | # forbid updating username for external accounts |
|
334 | 334 | skip_attrs.append('username') |
|
335 | 335 | |
|
336 | 336 | UserModel().update_user( |
|
337 | 337 | user_id, skip_attrs=skip_attrs, **form_result) |
|
338 | 338 | |
|
339 | 339 | audit_logger.store_web( |
|
340 | 340 | 'user.edit', action_data={'old_data': old_values}, |
|
341 | 341 | user=c.rhodecode_user) |
|
342 | 342 | |
|
343 | 343 | Session().commit() |
|
344 | 344 | h.flash(_('User updated successfully'), category='success') |
|
345 | 345 | except formencode.Invalid as errors: |
|
346 | 346 | data = render( |
|
347 | 347 | 'rhodecode:templates/admin/users/user_edit.mako', |
|
348 | 348 | self._get_template_context(c), self.request) |
|
349 | 349 | html = formencode.htmlfill.render( |
|
350 | 350 | data, |
|
351 | 351 | defaults=errors.value, |
|
352 | 352 | errors=errors.error_dict or {}, |
|
353 | 353 | prefix_error=False, |
|
354 | 354 | encoding="UTF-8", |
|
355 | 355 | force_defaults=False |
|
356 | 356 | ) |
|
357 | 357 | return Response(html) |
|
358 | 358 | except UserCreationError as e: |
|
359 | 359 | h.flash(e, 'error') |
|
360 | 360 | except Exception: |
|
361 | 361 | log.exception("Exception updating user") |
|
362 | 362 | h.flash(_('Error occurred during update of user %s') |
|
363 | 363 | % form_result.get('username'), category='error') |
|
364 | 364 | raise HTTPFound(h.route_path('user_edit', user_id=user_id)) |
|
365 | 365 | |
|
366 | 366 | @LoginRequired() |
|
367 | 367 | @HasPermissionAllDecorator('hg.admin') |
|
368 | 368 | @CSRFRequired() |
|
369 | 369 | @view_config( |
|
370 | 370 | route_name='user_delete', request_method='POST', |
|
371 | 371 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
372 | 372 | def user_delete(self): |
|
373 | 373 | _ = self.request.translate |
|
374 | 374 | c = self.load_default_context() |
|
375 | 375 | c.user = self.db_user |
|
376 | 376 | |
|
377 | 377 | _repos = c.user.repositories |
|
378 | 378 | _repo_groups = c.user.repository_groups |
|
379 | 379 | _user_groups = c.user.user_groups |
|
380 | 380 | _artifacts = c.user.artifacts |
|
381 | 381 | |
|
382 | 382 | handle_repos = None |
|
383 | 383 | handle_repo_groups = None |
|
384 | 384 | handle_user_groups = None |
|
385 | 385 | handle_artifacts = None |
|
386 | 386 | |
|
387 | 387 | # calls for flash of handle based on handle case detach or delete |
|
388 | 388 | def set_handle_flash_repos(): |
|
389 | 389 | handle = handle_repos |
|
390 | 390 | if handle == 'detach': |
|
391 | 391 | h.flash(_('Detached %s repositories') % len(_repos), |
|
392 | 392 | category='success') |
|
393 | 393 | elif handle == 'delete': |
|
394 | 394 | h.flash(_('Deleted %s repositories') % len(_repos), |
|
395 | 395 | category='success') |
|
396 | 396 | |
|
397 | 397 | def set_handle_flash_repo_groups(): |
|
398 | 398 | handle = handle_repo_groups |
|
399 | 399 | if handle == 'detach': |
|
400 | 400 | h.flash(_('Detached %s repository groups') % len(_repo_groups), |
|
401 | 401 | category='success') |
|
402 | 402 | elif handle == 'delete': |
|
403 | 403 | h.flash(_('Deleted %s repository groups') % len(_repo_groups), |
|
404 | 404 | category='success') |
|
405 | 405 | |
|
406 | 406 | def set_handle_flash_user_groups(): |
|
407 | 407 | handle = handle_user_groups |
|
408 | 408 | if handle == 'detach': |
|
409 | 409 | h.flash(_('Detached %s user groups') % len(_user_groups), |
|
410 | 410 | category='success') |
|
411 | 411 | elif handle == 'delete': |
|
412 | 412 | h.flash(_('Deleted %s user groups') % len(_user_groups), |
|
413 | 413 | category='success') |
|
414 | 414 | |
|
415 | 415 | def set_handle_flash_artifacts(): |
|
416 | 416 | handle = handle_artifacts |
|
417 | 417 | if handle == 'detach': |
|
418 | 418 | h.flash(_('Detached %s artifacts') % len(_artifacts), |
|
419 | 419 | category='success') |
|
420 | 420 | elif handle == 'delete': |
|
421 | 421 | h.flash(_('Deleted %s artifacts') % len(_artifacts), |
|
422 | 422 | category='success') |
|
423 | 423 | |
|
424 | 424 | if _repos and self.request.POST.get('user_repos'): |
|
425 | 425 | handle_repos = self.request.POST['user_repos'] |
|
426 | 426 | |
|
427 | 427 | if _repo_groups and self.request.POST.get('user_repo_groups'): |
|
428 | 428 | handle_repo_groups = self.request.POST['user_repo_groups'] |
|
429 | 429 | |
|
430 | 430 | if _user_groups and self.request.POST.get('user_user_groups'): |
|
431 | 431 | handle_user_groups = self.request.POST['user_user_groups'] |
|
432 | 432 | |
|
433 | 433 | if _artifacts and self.request.POST.get('user_artifacts'): |
|
434 | 434 | handle_artifacts = self.request.POST['user_artifacts'] |
|
435 | 435 | |
|
436 | 436 | old_values = c.user.get_api_data() |
|
437 | 437 | |
|
438 | 438 | try: |
|
439 | 439 | UserModel().delete(c.user, handle_repos=handle_repos, |
|
440 | 440 | handle_repo_groups=handle_repo_groups, |
|
441 | 441 | handle_user_groups=handle_user_groups, |
|
442 | 442 | handle_artifacts=handle_artifacts) |
|
443 | 443 | |
|
444 | 444 | audit_logger.store_web( |
|
445 | 445 | 'user.delete', action_data={'old_data': old_values}, |
|
446 | 446 | user=c.rhodecode_user) |
|
447 | 447 | |
|
448 | 448 | Session().commit() |
|
449 | 449 | set_handle_flash_repos() |
|
450 | 450 | set_handle_flash_repo_groups() |
|
451 | 451 | set_handle_flash_user_groups() |
|
452 | 452 | set_handle_flash_artifacts() |
|
453 | 453 | username = h.escape(old_values['username']) |
|
454 | 454 | h.flash(_('Successfully deleted user `{}`').format(username), category='success') |
|
455 | 455 | except (UserOwnsReposException, UserOwnsRepoGroupsException, |
|
456 | 456 | UserOwnsUserGroupsException, DefaultUserException) as e: |
|
457 | 457 | h.flash(e, category='warning') |
|
458 | 458 | except Exception: |
|
459 | 459 | log.exception("Exception during deletion of user") |
|
460 | 460 | h.flash(_('An error occurred during deletion of user'), |
|
461 | 461 | category='error') |
|
462 | 462 | raise HTTPFound(h.route_path('users')) |
|
463 | 463 | |
|
464 | 464 | @LoginRequired() |
|
465 | 465 | @HasPermissionAllDecorator('hg.admin') |
|
466 | 466 | @view_config( |
|
467 | 467 | route_name='user_edit', request_method='GET', |
|
468 | 468 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
469 | 469 | def user_edit(self): |
|
470 | 470 | _ = self.request.translate |
|
471 | 471 | c = self.load_default_context() |
|
472 | 472 | c.user = self.db_user |
|
473 | 473 | |
|
474 | 474 | c.active = 'profile' |
|
475 | 475 | c.extern_type = c.user.extern_type |
|
476 | 476 | c.extern_name = c.user.extern_name |
|
477 | 477 | c.perm_user = c.user.AuthUser(ip_addr=self.request.remote_addr) |
|
478 | 478 | |
|
479 | 479 | defaults = c.user.get_dict() |
|
480 | 480 | defaults.update({'language': c.user.user_data.get('language')}) |
|
481 | 481 | |
|
482 | 482 | data = render( |
|
483 | 483 | 'rhodecode:templates/admin/users/user_edit.mako', |
|
484 | 484 | self._get_template_context(c), self.request) |
|
485 | 485 | html = formencode.htmlfill.render( |
|
486 | 486 | data, |
|
487 | 487 | defaults=defaults, |
|
488 | 488 | encoding="UTF-8", |
|
489 | 489 | force_defaults=False |
|
490 | 490 | ) |
|
491 | 491 | return Response(html) |
|
492 | 492 | |
|
493 | 493 | @LoginRequired() |
|
494 | 494 | @HasPermissionAllDecorator('hg.admin') |
|
495 | 495 | @view_config( |
|
496 | 496 | route_name='user_edit_advanced', request_method='GET', |
|
497 | 497 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
498 | 498 | def user_edit_advanced(self): |
|
499 | 499 | _ = self.request.translate |
|
500 | 500 | c = self.load_default_context() |
|
501 | 501 | |
|
502 | 502 | user_id = self.db_user_id |
|
503 | 503 | c.user = self.db_user |
|
504 | 504 | |
|
505 | 505 | c.active = 'advanced' |
|
506 | 506 | c.personal_repo_group = RepoGroup.get_user_personal_repo_group(user_id) |
|
507 | 507 | c.personal_repo_group_name = RepoGroupModel()\ |
|
508 | 508 | .get_personal_group_name(c.user) |
|
509 | 509 | |
|
510 | 510 | c.user_to_review_rules = sorted( |
|
511 | 511 | (x.user for x in c.user.user_review_rules), |
|
512 | 512 | key=lambda u: u.username.lower()) |
|
513 | 513 | |
|
514 | 514 | c.first_admin = User.get_first_super_admin() |
|
515 | 515 | defaults = c.user.get_dict() |
|
516 | 516 | |
|
517 | 517 | # Interim workaround if the user participated on any pull requests as a |
|
518 | 518 | # reviewer. |
|
519 | 519 | has_review = len(c.user.reviewer_pull_requests) |
|
520 | 520 | c.can_delete_user = not has_review |
|
521 | 521 | c.can_delete_user_message = '' |
|
522 | 522 | inactive_link = h.link_to( |
|
523 | 523 | 'inactive', h.route_path('user_edit', user_id=user_id, _anchor='active')) |
|
524 | 524 | if has_review == 1: |
|
525 | 525 | c.can_delete_user_message = h.literal(_( |
|
526 | 526 | 'The user participates as reviewer in {} pull request and ' |
|
527 | 527 | 'cannot be deleted. \nYou can set the user to ' |
|
528 | 528 | '"{}" instead of deleting it.').format( |
|
529 | 529 | has_review, inactive_link)) |
|
530 | 530 | elif has_review: |
|
531 | 531 | c.can_delete_user_message = h.literal(_( |
|
532 | 532 | 'The user participates as reviewer in {} pull requests and ' |
|
533 | 533 | 'cannot be deleted. \nYou can set the user to ' |
|
534 | 534 | '"{}" instead of deleting it.').format( |
|
535 | 535 | has_review, inactive_link)) |
|
536 | 536 | |
|
537 | 537 | data = render( |
|
538 | 538 | 'rhodecode:templates/admin/users/user_edit.mako', |
|
539 | 539 | self._get_template_context(c), self.request) |
|
540 | 540 | html = formencode.htmlfill.render( |
|
541 | 541 | data, |
|
542 | 542 | defaults=defaults, |
|
543 | 543 | encoding="UTF-8", |
|
544 | 544 | force_defaults=False |
|
545 | 545 | ) |
|
546 | 546 | return Response(html) |
|
547 | 547 | |
|
548 | 548 | @LoginRequired() |
|
549 | 549 | @HasPermissionAllDecorator('hg.admin') |
|
550 | 550 | @view_config( |
|
551 | 551 | route_name='user_edit_global_perms', request_method='GET', |
|
552 | 552 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
553 | 553 | def user_edit_global_perms(self): |
|
554 | 554 | _ = self.request.translate |
|
555 | 555 | c = self.load_default_context() |
|
556 | 556 | c.user = self.db_user |
|
557 | 557 | |
|
558 | 558 | c.active = 'global_perms' |
|
559 | 559 | |
|
560 | 560 | c.default_user = User.get_default_user() |
|
561 | 561 | defaults = c.user.get_dict() |
|
562 | 562 | defaults.update(c.default_user.get_default_perms(suffix='_inherited')) |
|
563 | 563 | defaults.update(c.default_user.get_default_perms()) |
|
564 | 564 | defaults.update(c.user.get_default_perms()) |
|
565 | 565 | |
|
566 | 566 | data = render( |
|
567 | 567 | 'rhodecode:templates/admin/users/user_edit.mako', |
|
568 | 568 | self._get_template_context(c), self.request) |
|
569 | 569 | html = formencode.htmlfill.render( |
|
570 | 570 | data, |
|
571 | 571 | defaults=defaults, |
|
572 | 572 | encoding="UTF-8", |
|
573 | 573 | force_defaults=False |
|
574 | 574 | ) |
|
575 | 575 | return Response(html) |
|
576 | 576 | |
|
577 | 577 | @LoginRequired() |
|
578 | 578 | @HasPermissionAllDecorator('hg.admin') |
|
579 | 579 | @CSRFRequired() |
|
580 | 580 | @view_config( |
|
581 | 581 | route_name='user_edit_global_perms_update', request_method='POST', |
|
582 | 582 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
583 | 583 | def user_edit_global_perms_update(self): |
|
584 | 584 | _ = self.request.translate |
|
585 | 585 | c = self.load_default_context() |
|
586 | 586 | |
|
587 | 587 | user_id = self.db_user_id |
|
588 | 588 | c.user = self.db_user |
|
589 | 589 | |
|
590 | 590 | c.active = 'global_perms' |
|
591 | 591 | try: |
|
592 | 592 | # first stage that verifies the checkbox |
|
593 | 593 | _form = UserIndividualPermissionsForm(self.request.translate) |
|
594 | 594 | form_result = _form.to_python(dict(self.request.POST)) |
|
595 | 595 | inherit_perms = form_result['inherit_default_permissions'] |
|
596 | 596 | c.user.inherit_default_permissions = inherit_perms |
|
597 | 597 | Session().add(c.user) |
|
598 | 598 | |
|
599 | 599 | if not inherit_perms: |
|
600 | 600 | # only update the individual ones if we un check the flag |
|
601 | 601 | _form = UserPermissionsForm( |
|
602 | 602 | self.request.translate, |
|
603 | 603 | [x[0] for x in c.repo_create_choices], |
|
604 | 604 | [x[0] for x in c.repo_create_on_write_choices], |
|
605 | 605 | [x[0] for x in c.repo_group_create_choices], |
|
606 | 606 | [x[0] for x in c.user_group_create_choices], |
|
607 | 607 | [x[0] for x in c.fork_choices], |
|
608 | 608 | [x[0] for x in c.inherit_default_permission_choices])() |
|
609 | 609 | |
|
610 | 610 | form_result = _form.to_python(dict(self.request.POST)) |
|
611 | 611 | form_result.update({'perm_user_id': c.user.user_id}) |
|
612 | 612 | |
|
613 | 613 | PermissionModel().update_user_permissions(form_result) |
|
614 | 614 | |
|
615 | 615 | # TODO(marcink): implement global permissions |
|
616 | 616 | # audit_log.store_web('user.edit.permissions') |
|
617 | 617 | |
|
618 | 618 | Session().commit() |
|
619 | 619 | |
|
620 | 620 | h.flash(_('User global permissions updated successfully'), |
|
621 | 621 | category='success') |
|
622 | 622 | |
|
623 | 623 | except formencode.Invalid as errors: |
|
624 | 624 | data = render( |
|
625 | 625 | 'rhodecode:templates/admin/users/user_edit.mako', |
|
626 | 626 | self._get_template_context(c), self.request) |
|
627 | 627 | html = formencode.htmlfill.render( |
|
628 | 628 | data, |
|
629 | 629 | defaults=errors.value, |
|
630 | 630 | errors=errors.error_dict or {}, |
|
631 | 631 | prefix_error=False, |
|
632 | 632 | encoding="UTF-8", |
|
633 | 633 | force_defaults=False |
|
634 | 634 | ) |
|
635 | 635 | return Response(html) |
|
636 | 636 | except Exception: |
|
637 | 637 | log.exception("Exception during permissions saving") |
|
638 | 638 | h.flash(_('An error occurred during permissions saving'), |
|
639 | 639 | category='error') |
|
640 | 640 | |
|
641 | 641 | affected_user_ids = [user_id] |
|
642 | 642 | PermissionModel().trigger_permission_flush(affected_user_ids) |
|
643 | 643 | raise HTTPFound(h.route_path('user_edit_global_perms', user_id=user_id)) |
|
644 | 644 | |
|
645 | 645 | @LoginRequired() |
|
646 | 646 | @HasPermissionAllDecorator('hg.admin') |
|
647 | 647 | @CSRFRequired() |
|
648 | 648 | @view_config( |
|
649 | 649 | route_name='user_enable_force_password_reset', request_method='POST', |
|
650 | 650 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
651 | 651 | def user_enable_force_password_reset(self): |
|
652 | 652 | _ = self.request.translate |
|
653 | 653 | c = self.load_default_context() |
|
654 | 654 | |
|
655 | 655 | user_id = self.db_user_id |
|
656 | 656 | c.user = self.db_user |
|
657 | 657 | |
|
658 | 658 | try: |
|
659 | 659 | c.user.update_userdata(force_password_change=True) |
|
660 | 660 | |
|
661 | 661 | msg = _('Force password change enabled for user') |
|
662 | 662 | audit_logger.store_web('user.edit.password_reset.enabled', |
|
663 | 663 | user=c.rhodecode_user) |
|
664 | 664 | |
|
665 | 665 | Session().commit() |
|
666 | 666 | h.flash(msg, category='success') |
|
667 | 667 | except Exception: |
|
668 | 668 | log.exception("Exception during password reset for user") |
|
669 | 669 | h.flash(_('An error occurred during password reset for user'), |
|
670 | 670 | category='error') |
|
671 | 671 | |
|
672 | 672 | raise HTTPFound(h.route_path('user_edit_advanced', user_id=user_id)) |
|
673 | 673 | |
|
674 | 674 | @LoginRequired() |
|
675 | 675 | @HasPermissionAllDecorator('hg.admin') |
|
676 | 676 | @CSRFRequired() |
|
677 | 677 | @view_config( |
|
678 | 678 | route_name='user_disable_force_password_reset', request_method='POST', |
|
679 | 679 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
680 | 680 | def user_disable_force_password_reset(self): |
|
681 | 681 | _ = self.request.translate |
|
682 | 682 | c = self.load_default_context() |
|
683 | 683 | |
|
684 | 684 | user_id = self.db_user_id |
|
685 | 685 | c.user = self.db_user |
|
686 | 686 | |
|
687 | 687 | try: |
|
688 | 688 | c.user.update_userdata(force_password_change=False) |
|
689 | 689 | |
|
690 | 690 | msg = _('Force password change disabled for user') |
|
691 | 691 | audit_logger.store_web( |
|
692 | 692 | 'user.edit.password_reset.disabled', |
|
693 | 693 | user=c.rhodecode_user) |
|
694 | 694 | |
|
695 | 695 | Session().commit() |
|
696 | 696 | h.flash(msg, category='success') |
|
697 | 697 | except Exception: |
|
698 | 698 | log.exception("Exception during password reset for user") |
|
699 | 699 | h.flash(_('An error occurred during password reset for user'), |
|
700 | 700 | category='error') |
|
701 | 701 | |
|
702 | 702 | raise HTTPFound(h.route_path('user_edit_advanced', user_id=user_id)) |
|
703 | 703 | |
|
704 | 704 | @LoginRequired() |
|
705 | 705 | @HasPermissionAllDecorator('hg.admin') |
|
706 | 706 | @CSRFRequired() |
|
707 | 707 | @view_config( |
|
708 | 708 | route_name='user_create_personal_repo_group', request_method='POST', |
|
709 | 709 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
710 | 710 | def user_create_personal_repo_group(self): |
|
711 | 711 | """ |
|
712 | 712 | Create personal repository group for this user |
|
713 | 713 | """ |
|
714 | 714 | from rhodecode.model.repo_group import RepoGroupModel |
|
715 | 715 | |
|
716 | 716 | _ = self.request.translate |
|
717 | 717 | c = self.load_default_context() |
|
718 | 718 | |
|
719 | 719 | user_id = self.db_user_id |
|
720 | 720 | c.user = self.db_user |
|
721 | 721 | |
|
722 | 722 | personal_repo_group = RepoGroup.get_user_personal_repo_group( |
|
723 | 723 | c.user.user_id) |
|
724 | 724 | if personal_repo_group: |
|
725 | 725 | raise HTTPFound(h.route_path('user_edit_advanced', user_id=user_id)) |
|
726 | 726 | |
|
727 | 727 | personal_repo_group_name = RepoGroupModel().get_personal_group_name(c.user) |
|
728 | 728 | named_personal_group = RepoGroup.get_by_group_name( |
|
729 | 729 | personal_repo_group_name) |
|
730 | 730 | try: |
|
731 | 731 | |
|
732 | 732 | if named_personal_group and named_personal_group.user_id == c.user.user_id: |
|
733 | 733 | # migrate the same named group, and mark it as personal |
|
734 | 734 | named_personal_group.personal = True |
|
735 | 735 | Session().add(named_personal_group) |
|
736 | 736 | Session().commit() |
|
737 | 737 | msg = _('Linked repository group `%s` as personal' % ( |
|
738 | 738 | personal_repo_group_name,)) |
|
739 | 739 | h.flash(msg, category='success') |
|
740 | 740 | elif not named_personal_group: |
|
741 | 741 | RepoGroupModel().create_personal_repo_group(c.user) |
|
742 | 742 | |
|
743 | 743 | msg = _('Created repository group `%s`' % ( |
|
744 | 744 | personal_repo_group_name,)) |
|
745 | 745 | h.flash(msg, category='success') |
|
746 | 746 | else: |
|
747 | 747 | msg = _('Repository group `%s` is already taken' % ( |
|
748 | 748 | personal_repo_group_name,)) |
|
749 | 749 | h.flash(msg, category='warning') |
|
750 | 750 | except Exception: |
|
751 | 751 | log.exception("Exception during repository group creation") |
|
752 | 752 | msg = _( |
|
753 | 753 | 'An error occurred during repository group creation for user') |
|
754 | 754 | h.flash(msg, category='error') |
|
755 | 755 | Session().rollback() |
|
756 | 756 | |
|
757 | 757 | raise HTTPFound(h.route_path('user_edit_advanced', user_id=user_id)) |
|
758 | 758 | |
|
759 | 759 | @LoginRequired() |
|
760 | 760 | @HasPermissionAllDecorator('hg.admin') |
|
761 | 761 | @view_config( |
|
762 | 762 | route_name='edit_user_auth_tokens', request_method='GET', |
|
763 | 763 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
764 | 764 | def auth_tokens(self): |
|
765 | 765 | _ = self.request.translate |
|
766 | 766 | c = self.load_default_context() |
|
767 | 767 | c.user = self.db_user |
|
768 | 768 | |
|
769 | 769 | c.active = 'auth_tokens' |
|
770 | 770 | |
|
771 | 771 | c.lifetime_values = AuthTokenModel.get_lifetime_values(translator=_) |
|
772 | 772 | c.role_values = [ |
|
773 | 773 | (x, AuthTokenModel.cls._get_role_name(x)) |
|
774 | 774 | for x in AuthTokenModel.cls.ROLES] |
|
775 | 775 | c.role_options = [(c.role_values, _("Role"))] |
|
776 | 776 | c.user_auth_tokens = AuthTokenModel().get_auth_tokens( |
|
777 | 777 | c.user.user_id, show_expired=True) |
|
778 | 778 | c.role_vcs = AuthTokenModel.cls.ROLE_VCS |
|
779 | 779 | return self._get_template_context(c) |
|
780 | 780 | |
|
781 | 781 | def maybe_attach_token_scope(self, token): |
|
782 | 782 | # implemented in EE edition |
|
783 | 783 | pass |
|
784 | 784 | |
|
785 | 785 | @LoginRequired() |
|
786 | 786 | @HasPermissionAllDecorator('hg.admin') |
|
787 | 787 | @CSRFRequired() |
|
788 | 788 | @view_config( |
|
789 | 789 | route_name='edit_user_auth_tokens_add', request_method='POST') |
|
790 | 790 | def auth_tokens_add(self): |
|
791 | 791 | _ = self.request.translate |
|
792 | 792 | c = self.load_default_context() |
|
793 | 793 | |
|
794 | 794 | user_id = self.db_user_id |
|
795 | 795 | c.user = self.db_user |
|
796 | 796 | |
|
797 | 797 | user_data = c.user.get_api_data() |
|
798 | 798 | lifetime = safe_int(self.request.POST.get('lifetime'), -1) |
|
799 | 799 | description = self.request.POST.get('description') |
|
800 | 800 | role = self.request.POST.get('role') |
|
801 | 801 | |
|
802 | 802 | token = UserModel().add_auth_token( |
|
803 | 803 | user=c.user.user_id, |
|
804 | 804 | lifetime_minutes=lifetime, role=role, description=description, |
|
805 | 805 | scope_callback=self.maybe_attach_token_scope) |
|
806 | 806 | token_data = token.get_api_data() |
|
807 | 807 | |
|
808 | 808 | audit_logger.store_web( |
|
809 | 809 | 'user.edit.token.add', action_data={ |
|
810 | 810 | 'data': {'token': token_data, 'user': user_data}}, |
|
811 | 811 | user=self._rhodecode_user, ) |
|
812 | 812 | Session().commit() |
|
813 | 813 | |
|
814 | 814 | h.flash(_("Auth token successfully created"), category='success') |
|
815 | 815 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) |
|
816 | 816 | |
|
817 | 817 | @LoginRequired() |
|
818 | 818 | @HasPermissionAllDecorator('hg.admin') |
|
819 | 819 | @CSRFRequired() |
|
820 | 820 | @view_config( |
|
821 | 821 | route_name='edit_user_auth_tokens_delete', request_method='POST') |
|
822 | 822 | def auth_tokens_delete(self): |
|
823 | 823 | _ = self.request.translate |
|
824 | 824 | c = self.load_default_context() |
|
825 | 825 | |
|
826 | 826 | user_id = self.db_user_id |
|
827 | 827 | c.user = self.db_user |
|
828 | 828 | |
|
829 | 829 | user_data = c.user.get_api_data() |
|
830 | 830 | |
|
831 | 831 | del_auth_token = self.request.POST.get('del_auth_token') |
|
832 | 832 | |
|
833 | 833 | if del_auth_token: |
|
834 | 834 | token = UserApiKeys.get_or_404(del_auth_token) |
|
835 | 835 | token_data = token.get_api_data() |
|
836 | 836 | |
|
837 | 837 | AuthTokenModel().delete(del_auth_token, c.user.user_id) |
|
838 | 838 | audit_logger.store_web( |
|
839 | 839 | 'user.edit.token.delete', action_data={ |
|
840 | 840 | 'data': {'token': token_data, 'user': user_data}}, |
|
841 | 841 | user=self._rhodecode_user,) |
|
842 | 842 | Session().commit() |
|
843 | 843 | h.flash(_("Auth token successfully deleted"), category='success') |
|
844 | 844 | |
|
845 | 845 | return HTTPFound(h.route_path('edit_user_auth_tokens', user_id=user_id)) |
|
846 | 846 | |
|
847 | 847 | @LoginRequired() |
|
848 | 848 | @HasPermissionAllDecorator('hg.admin') |
|
849 | 849 | @view_config( |
|
850 | 850 | route_name='edit_user_ssh_keys', request_method='GET', |
|
851 | 851 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
852 | 852 | def ssh_keys(self): |
|
853 | 853 | _ = self.request.translate |
|
854 | 854 | c = self.load_default_context() |
|
855 | 855 | c.user = self.db_user |
|
856 | 856 | |
|
857 | 857 | c.active = 'ssh_keys' |
|
858 | 858 | c.default_key = self.request.GET.get('default_key') |
|
859 | 859 | c.user_ssh_keys = SshKeyModel().get_ssh_keys(c.user.user_id) |
|
860 | 860 | return self._get_template_context(c) |
|
861 | 861 | |
|
862 | 862 | @LoginRequired() |
|
863 | 863 | @HasPermissionAllDecorator('hg.admin') |
|
864 | 864 | @view_config( |
|
865 | 865 | route_name='edit_user_ssh_keys_generate_keypair', request_method='GET', |
|
866 | 866 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
867 | 867 | def ssh_keys_generate_keypair(self): |
|
868 | 868 | _ = self.request.translate |
|
869 | 869 | c = self.load_default_context() |
|
870 | 870 | |
|
871 | 871 | c.user = self.db_user |
|
872 | 872 | |
|
873 | 873 | c.active = 'ssh_keys_generate' |
|
874 | 874 | comment = 'RhodeCode-SSH {}'.format(c.user.email or '') |
|
875 | c.private, c.public = SshKeyModel().generate_keypair(comment=comment) | |
|
875 | private_format = self.request.GET.get('private_format') \ | |
|
876 | or SshKeyModel.DEFAULT_PRIVATE_KEY_FORMAT | |
|
877 | c.private, c.public = SshKeyModel().generate_keypair( | |
|
878 | comment=comment, private_format=private_format) | |
|
876 | 879 | |
|
877 | 880 | return self._get_template_context(c) |
|
878 | 881 | |
|
879 | 882 | @LoginRequired() |
|
880 | 883 | @HasPermissionAllDecorator('hg.admin') |
|
881 | 884 | @CSRFRequired() |
|
882 | 885 | @view_config( |
|
883 | 886 | route_name='edit_user_ssh_keys_add', request_method='POST') |
|
884 | 887 | def ssh_keys_add(self): |
|
885 | 888 | _ = self.request.translate |
|
886 | 889 | c = self.load_default_context() |
|
887 | 890 | |
|
888 | 891 | user_id = self.db_user_id |
|
889 | 892 | c.user = self.db_user |
|
890 | 893 | |
|
891 | 894 | user_data = c.user.get_api_data() |
|
892 | 895 | key_data = self.request.POST.get('key_data') |
|
893 | 896 | description = self.request.POST.get('description') |
|
894 | 897 | |
|
895 | 898 | fingerprint = 'unknown' |
|
896 | 899 | try: |
|
897 | 900 | if not key_data: |
|
898 | 901 | raise ValueError('Please add a valid public key') |
|
899 | 902 | |
|
900 | 903 | key = SshKeyModel().parse_key(key_data.strip()) |
|
901 | 904 | fingerprint = key.hash_md5() |
|
902 | 905 | |
|
903 | 906 | ssh_key = SshKeyModel().create( |
|
904 | 907 | c.user.user_id, fingerprint, key.keydata, description) |
|
905 | 908 | ssh_key_data = ssh_key.get_api_data() |
|
906 | 909 | |
|
907 | 910 | audit_logger.store_web( |
|
908 | 911 | 'user.edit.ssh_key.add', action_data={ |
|
909 | 912 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
910 | 913 | user=self._rhodecode_user, ) |
|
911 | 914 | Session().commit() |
|
912 | 915 | |
|
913 | 916 | # Trigger an event on change of keys. |
|
914 | 917 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
915 | 918 | |
|
916 | 919 | h.flash(_("Ssh Key successfully created"), category='success') |
|
917 | 920 | |
|
918 | 921 | except IntegrityError: |
|
919 | 922 | log.exception("Exception during ssh key saving") |
|
920 | 923 | err = 'Such key with fingerprint `{}` already exists, ' \ |
|
921 | 924 | 'please use a different one'.format(fingerprint) |
|
922 | 925 | h.flash(_('An error occurred during ssh key saving: {}').format(err), |
|
923 | 926 | category='error') |
|
924 | 927 | except Exception as e: |
|
925 | 928 | log.exception("Exception during ssh key saving") |
|
926 | 929 | h.flash(_('An error occurred during ssh key saving: {}').format(e), |
|
927 | 930 | category='error') |
|
928 | 931 | |
|
929 | 932 | return HTTPFound( |
|
930 | 933 | h.route_path('edit_user_ssh_keys', user_id=user_id)) |
|
931 | 934 | |
|
932 | 935 | @LoginRequired() |
|
933 | 936 | @HasPermissionAllDecorator('hg.admin') |
|
934 | 937 | @CSRFRequired() |
|
935 | 938 | @view_config( |
|
936 | 939 | route_name='edit_user_ssh_keys_delete', request_method='POST') |
|
937 | 940 | def ssh_keys_delete(self): |
|
938 | 941 | _ = self.request.translate |
|
939 | 942 | c = self.load_default_context() |
|
940 | 943 | |
|
941 | 944 | user_id = self.db_user_id |
|
942 | 945 | c.user = self.db_user |
|
943 | 946 | |
|
944 | 947 | user_data = c.user.get_api_data() |
|
945 | 948 | |
|
946 | 949 | del_ssh_key = self.request.POST.get('del_ssh_key') |
|
947 | 950 | |
|
948 | 951 | if del_ssh_key: |
|
949 | 952 | ssh_key = UserSshKeys.get_or_404(del_ssh_key) |
|
950 | 953 | ssh_key_data = ssh_key.get_api_data() |
|
951 | 954 | |
|
952 | 955 | SshKeyModel().delete(del_ssh_key, c.user.user_id) |
|
953 | 956 | audit_logger.store_web( |
|
954 | 957 | 'user.edit.ssh_key.delete', action_data={ |
|
955 | 958 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
956 | 959 | user=self._rhodecode_user,) |
|
957 | 960 | Session().commit() |
|
958 | 961 | # Trigger an event on change of keys. |
|
959 | 962 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
960 | 963 | h.flash(_("Ssh key successfully deleted"), category='success') |
|
961 | 964 | |
|
962 | 965 | return HTTPFound(h.route_path('edit_user_ssh_keys', user_id=user_id)) |
|
963 | 966 | |
|
964 | 967 | @LoginRequired() |
|
965 | 968 | @HasPermissionAllDecorator('hg.admin') |
|
966 | 969 | @view_config( |
|
967 | 970 | route_name='edit_user_emails', request_method='GET', |
|
968 | 971 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
969 | 972 | def emails(self): |
|
970 | 973 | _ = self.request.translate |
|
971 | 974 | c = self.load_default_context() |
|
972 | 975 | c.user = self.db_user |
|
973 | 976 | |
|
974 | 977 | c.active = 'emails' |
|
975 | 978 | c.user_email_map = UserEmailMap.query() \ |
|
976 | 979 | .filter(UserEmailMap.user == c.user).all() |
|
977 | 980 | |
|
978 | 981 | return self._get_template_context(c) |
|
979 | 982 | |
|
980 | 983 | @LoginRequired() |
|
981 | 984 | @HasPermissionAllDecorator('hg.admin') |
|
982 | 985 | @CSRFRequired() |
|
983 | 986 | @view_config( |
|
984 | 987 | route_name='edit_user_emails_add', request_method='POST') |
|
985 | 988 | def emails_add(self): |
|
986 | 989 | _ = self.request.translate |
|
987 | 990 | c = self.load_default_context() |
|
988 | 991 | |
|
989 | 992 | user_id = self.db_user_id |
|
990 | 993 | c.user = self.db_user |
|
991 | 994 | |
|
992 | 995 | email = self.request.POST.get('new_email') |
|
993 | 996 | user_data = c.user.get_api_data() |
|
994 | 997 | try: |
|
995 | 998 | |
|
996 | 999 | form = UserExtraEmailForm(self.request.translate)() |
|
997 | 1000 | data = form.to_python({'email': email}) |
|
998 | 1001 | email = data['email'] |
|
999 | 1002 | |
|
1000 | 1003 | UserModel().add_extra_email(c.user.user_id, email) |
|
1001 | 1004 | audit_logger.store_web( |
|
1002 | 1005 | 'user.edit.email.add', |
|
1003 | 1006 | action_data={'email': email, 'user': user_data}, |
|
1004 | 1007 | user=self._rhodecode_user) |
|
1005 | 1008 | Session().commit() |
|
1006 | 1009 | h.flash(_("Added new email address `%s` for user account") % email, |
|
1007 | 1010 | category='success') |
|
1008 | 1011 | except formencode.Invalid as error: |
|
1009 | 1012 | h.flash(h.escape(error.error_dict['email']), category='error') |
|
1010 | 1013 | except IntegrityError: |
|
1011 | 1014 | log.warning("Email %s already exists", email) |
|
1012 | 1015 | h.flash(_('Email `{}` is already registered for another user.').format(email), |
|
1013 | 1016 | category='error') |
|
1014 | 1017 | except Exception: |
|
1015 | 1018 | log.exception("Exception during email saving") |
|
1016 | 1019 | h.flash(_('An error occurred during email saving'), |
|
1017 | 1020 | category='error') |
|
1018 | 1021 | raise HTTPFound(h.route_path('edit_user_emails', user_id=user_id)) |
|
1019 | 1022 | |
|
1020 | 1023 | @LoginRequired() |
|
1021 | 1024 | @HasPermissionAllDecorator('hg.admin') |
|
1022 | 1025 | @CSRFRequired() |
|
1023 | 1026 | @view_config( |
|
1024 | 1027 | route_name='edit_user_emails_delete', request_method='POST') |
|
1025 | 1028 | def emails_delete(self): |
|
1026 | 1029 | _ = self.request.translate |
|
1027 | 1030 | c = self.load_default_context() |
|
1028 | 1031 | |
|
1029 | 1032 | user_id = self.db_user_id |
|
1030 | 1033 | c.user = self.db_user |
|
1031 | 1034 | |
|
1032 | 1035 | email_id = self.request.POST.get('del_email_id') |
|
1033 | 1036 | user_model = UserModel() |
|
1034 | 1037 | |
|
1035 | 1038 | email = UserEmailMap.query().get(email_id).email |
|
1036 | 1039 | user_data = c.user.get_api_data() |
|
1037 | 1040 | user_model.delete_extra_email(c.user.user_id, email_id) |
|
1038 | 1041 | audit_logger.store_web( |
|
1039 | 1042 | 'user.edit.email.delete', |
|
1040 | 1043 | action_data={'email': email, 'user': user_data}, |
|
1041 | 1044 | user=self._rhodecode_user) |
|
1042 | 1045 | Session().commit() |
|
1043 | 1046 | h.flash(_("Removed email address from user account"), |
|
1044 | 1047 | category='success') |
|
1045 | 1048 | raise HTTPFound(h.route_path('edit_user_emails', user_id=user_id)) |
|
1046 | 1049 | |
|
1047 | 1050 | @LoginRequired() |
|
1048 | 1051 | @HasPermissionAllDecorator('hg.admin') |
|
1049 | 1052 | @view_config( |
|
1050 | 1053 | route_name='edit_user_ips', request_method='GET', |
|
1051 | 1054 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
1052 | 1055 | def ips(self): |
|
1053 | 1056 | _ = self.request.translate |
|
1054 | 1057 | c = self.load_default_context() |
|
1055 | 1058 | c.user = self.db_user |
|
1056 | 1059 | |
|
1057 | 1060 | c.active = 'ips' |
|
1058 | 1061 | c.user_ip_map = UserIpMap.query() \ |
|
1059 | 1062 | .filter(UserIpMap.user == c.user).all() |
|
1060 | 1063 | |
|
1061 | 1064 | c.inherit_default_ips = c.user.inherit_default_permissions |
|
1062 | 1065 | c.default_user_ip_map = UserIpMap.query() \ |
|
1063 | 1066 | .filter(UserIpMap.user == User.get_default_user()).all() |
|
1064 | 1067 | |
|
1065 | 1068 | return self._get_template_context(c) |
|
1066 | 1069 | |
|
1067 | 1070 | @LoginRequired() |
|
1068 | 1071 | @HasPermissionAllDecorator('hg.admin') |
|
1069 | 1072 | @CSRFRequired() |
|
1070 | 1073 | @view_config( |
|
1071 | 1074 | route_name='edit_user_ips_add', request_method='POST') |
|
1072 | 1075 | # NOTE(marcink): this view is allowed for default users, as we can |
|
1073 | 1076 | # edit their IP white list |
|
1074 | 1077 | def ips_add(self): |
|
1075 | 1078 | _ = self.request.translate |
|
1076 | 1079 | c = self.load_default_context() |
|
1077 | 1080 | |
|
1078 | 1081 | user_id = self.db_user_id |
|
1079 | 1082 | c.user = self.db_user |
|
1080 | 1083 | |
|
1081 | 1084 | user_model = UserModel() |
|
1082 | 1085 | desc = self.request.POST.get('description') |
|
1083 | 1086 | try: |
|
1084 | 1087 | ip_list = user_model.parse_ip_range( |
|
1085 | 1088 | self.request.POST.get('new_ip')) |
|
1086 | 1089 | except Exception as e: |
|
1087 | 1090 | ip_list = [] |
|
1088 | 1091 | log.exception("Exception during ip saving") |
|
1089 | 1092 | h.flash(_('An error occurred during ip saving:%s' % (e,)), |
|
1090 | 1093 | category='error') |
|
1091 | 1094 | added = [] |
|
1092 | 1095 | user_data = c.user.get_api_data() |
|
1093 | 1096 | for ip in ip_list: |
|
1094 | 1097 | try: |
|
1095 | 1098 | form = UserExtraIpForm(self.request.translate)() |
|
1096 | 1099 | data = form.to_python({'ip': ip}) |
|
1097 | 1100 | ip = data['ip'] |
|
1098 | 1101 | |
|
1099 | 1102 | user_model.add_extra_ip(c.user.user_id, ip, desc) |
|
1100 | 1103 | audit_logger.store_web( |
|
1101 | 1104 | 'user.edit.ip.add', |
|
1102 | 1105 | action_data={'ip': ip, 'user': user_data}, |
|
1103 | 1106 | user=self._rhodecode_user) |
|
1104 | 1107 | Session().commit() |
|
1105 | 1108 | added.append(ip) |
|
1106 | 1109 | except formencode.Invalid as error: |
|
1107 | 1110 | msg = error.error_dict['ip'] |
|
1108 | 1111 | h.flash(msg, category='error') |
|
1109 | 1112 | except Exception: |
|
1110 | 1113 | log.exception("Exception during ip saving") |
|
1111 | 1114 | h.flash(_('An error occurred during ip saving'), |
|
1112 | 1115 | category='error') |
|
1113 | 1116 | if added: |
|
1114 | 1117 | h.flash( |
|
1115 | 1118 | _("Added ips %s to user whitelist") % (', '.join(ip_list), ), |
|
1116 | 1119 | category='success') |
|
1117 | 1120 | if 'default_user' in self.request.POST: |
|
1118 | 1121 | # case for editing global IP list we do it for 'DEFAULT' user |
|
1119 | 1122 | raise HTTPFound(h.route_path('admin_permissions_ips')) |
|
1120 | 1123 | raise HTTPFound(h.route_path('edit_user_ips', user_id=user_id)) |
|
1121 | 1124 | |
|
1122 | 1125 | @LoginRequired() |
|
1123 | 1126 | @HasPermissionAllDecorator('hg.admin') |
|
1124 | 1127 | @CSRFRequired() |
|
1125 | 1128 | @view_config( |
|
1126 | 1129 | route_name='edit_user_ips_delete', request_method='POST') |
|
1127 | 1130 | # NOTE(marcink): this view is allowed for default users, as we can |
|
1128 | 1131 | # edit their IP white list |
|
1129 | 1132 | def ips_delete(self): |
|
1130 | 1133 | _ = self.request.translate |
|
1131 | 1134 | c = self.load_default_context() |
|
1132 | 1135 | |
|
1133 | 1136 | user_id = self.db_user_id |
|
1134 | 1137 | c.user = self.db_user |
|
1135 | 1138 | |
|
1136 | 1139 | ip_id = self.request.POST.get('del_ip_id') |
|
1137 | 1140 | user_model = UserModel() |
|
1138 | 1141 | user_data = c.user.get_api_data() |
|
1139 | 1142 | ip = UserIpMap.query().get(ip_id).ip_addr |
|
1140 | 1143 | user_model.delete_extra_ip(c.user.user_id, ip_id) |
|
1141 | 1144 | audit_logger.store_web( |
|
1142 | 1145 | 'user.edit.ip.delete', action_data={'ip': ip, 'user': user_data}, |
|
1143 | 1146 | user=self._rhodecode_user) |
|
1144 | 1147 | Session().commit() |
|
1145 | 1148 | h.flash(_("Removed ip address from user whitelist"), category='success') |
|
1146 | 1149 | |
|
1147 | 1150 | if 'default_user' in self.request.POST: |
|
1148 | 1151 | # case for editing global IP list we do it for 'DEFAULT' user |
|
1149 | 1152 | raise HTTPFound(h.route_path('admin_permissions_ips')) |
|
1150 | 1153 | raise HTTPFound(h.route_path('edit_user_ips', user_id=user_id)) |
|
1151 | 1154 | |
|
1152 | 1155 | @LoginRequired() |
|
1153 | 1156 | @HasPermissionAllDecorator('hg.admin') |
|
1154 | 1157 | @view_config( |
|
1155 | 1158 | route_name='edit_user_groups_management', request_method='GET', |
|
1156 | 1159 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
1157 | 1160 | def groups_management(self): |
|
1158 | 1161 | c = self.load_default_context() |
|
1159 | 1162 | c.user = self.db_user |
|
1160 | 1163 | c.data = c.user.group_member |
|
1161 | 1164 | |
|
1162 | 1165 | groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) |
|
1163 | 1166 | for group in c.user.group_member] |
|
1164 | 1167 | c.groups = json.dumps(groups) |
|
1165 | 1168 | c.active = 'groups' |
|
1166 | 1169 | |
|
1167 | 1170 | return self._get_template_context(c) |
|
1168 | 1171 | |
|
1169 | 1172 | @LoginRequired() |
|
1170 | 1173 | @HasPermissionAllDecorator('hg.admin') |
|
1171 | 1174 | @CSRFRequired() |
|
1172 | 1175 | @view_config( |
|
1173 | 1176 | route_name='edit_user_groups_management_updates', request_method='POST') |
|
1174 | 1177 | def groups_management_updates(self): |
|
1175 | 1178 | _ = self.request.translate |
|
1176 | 1179 | c = self.load_default_context() |
|
1177 | 1180 | |
|
1178 | 1181 | user_id = self.db_user_id |
|
1179 | 1182 | c.user = self.db_user |
|
1180 | 1183 | |
|
1181 | 1184 | user_groups = set(self.request.POST.getall('users_group_id')) |
|
1182 | 1185 | user_groups_objects = [] |
|
1183 | 1186 | |
|
1184 | 1187 | for ugid in user_groups: |
|
1185 | 1188 | user_groups_objects.append( |
|
1186 | 1189 | UserGroupModel().get_group(safe_int(ugid))) |
|
1187 | 1190 | user_group_model = UserGroupModel() |
|
1188 | 1191 | added_to_groups, removed_from_groups = \ |
|
1189 | 1192 | user_group_model.change_groups(c.user, user_groups_objects) |
|
1190 | 1193 | |
|
1191 | 1194 | user_data = c.user.get_api_data() |
|
1192 | 1195 | for user_group_id in added_to_groups: |
|
1193 | 1196 | user_group = UserGroup.get(user_group_id) |
|
1194 | 1197 | old_values = user_group.get_api_data() |
|
1195 | 1198 | audit_logger.store_web( |
|
1196 | 1199 | 'user_group.edit.member.add', |
|
1197 | 1200 | action_data={'user': user_data, 'old_data': old_values}, |
|
1198 | 1201 | user=self._rhodecode_user) |
|
1199 | 1202 | |
|
1200 | 1203 | for user_group_id in removed_from_groups: |
|
1201 | 1204 | user_group = UserGroup.get(user_group_id) |
|
1202 | 1205 | old_values = user_group.get_api_data() |
|
1203 | 1206 | audit_logger.store_web( |
|
1204 | 1207 | 'user_group.edit.member.delete', |
|
1205 | 1208 | action_data={'user': user_data, 'old_data': old_values}, |
|
1206 | 1209 | user=self._rhodecode_user) |
|
1207 | 1210 | |
|
1208 | 1211 | Session().commit() |
|
1209 | 1212 | c.active = 'user_groups_management' |
|
1210 | 1213 | h.flash(_("Groups successfully changed"), category='success') |
|
1211 | 1214 | |
|
1212 | 1215 | return HTTPFound(h.route_path( |
|
1213 | 1216 | 'edit_user_groups_management', user_id=user_id)) |
|
1214 | 1217 | |
|
1215 | 1218 | @LoginRequired() |
|
1216 | 1219 | @HasPermissionAllDecorator('hg.admin') |
|
1217 | 1220 | @view_config( |
|
1218 | 1221 | route_name='edit_user_audit_logs', request_method='GET', |
|
1219 | 1222 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
1220 | 1223 | def user_audit_logs(self): |
|
1221 | 1224 | _ = self.request.translate |
|
1222 | 1225 | c = self.load_default_context() |
|
1223 | 1226 | c.user = self.db_user |
|
1224 | 1227 | |
|
1225 | 1228 | c.active = 'audit' |
|
1226 | 1229 | |
|
1227 | 1230 | p = safe_int(self.request.GET.get('page', 1), 1) |
|
1228 | 1231 | |
|
1229 | 1232 | filter_term = self.request.GET.get('filter') |
|
1230 | 1233 | user_log = UserModel().get_user_log(c.user, filter_term) |
|
1231 | 1234 | |
|
1232 | 1235 | def url_generator(page_num): |
|
1233 | 1236 | query_params = { |
|
1234 | 1237 | 'page': page_num |
|
1235 | 1238 | } |
|
1236 | 1239 | if filter_term: |
|
1237 | 1240 | query_params['filter'] = filter_term |
|
1238 | 1241 | return self.request.current_route_path(_query=query_params) |
|
1239 | 1242 | |
|
1240 | 1243 | c.audit_logs = SqlPage( |
|
1241 | 1244 | user_log, page=p, items_per_page=10, url_maker=url_generator) |
|
1242 | 1245 | c.filter_term = filter_term |
|
1243 | 1246 | return self._get_template_context(c) |
|
1244 | 1247 | |
|
1245 | 1248 | @LoginRequired() |
|
1246 | 1249 | @HasPermissionAllDecorator('hg.admin') |
|
1247 | 1250 | @view_config( |
|
1248 | 1251 | route_name='edit_user_audit_logs_download', request_method='GET', |
|
1249 | 1252 | renderer='string') |
|
1250 | 1253 | def user_audit_logs_download(self): |
|
1251 | 1254 | _ = self.request.translate |
|
1252 | 1255 | c = self.load_default_context() |
|
1253 | 1256 | c.user = self.db_user |
|
1254 | 1257 | |
|
1255 | 1258 | user_log = UserModel().get_user_log(c.user, filter_term=None) |
|
1256 | 1259 | |
|
1257 | 1260 | audit_log_data = {} |
|
1258 | 1261 | for entry in user_log: |
|
1259 | 1262 | audit_log_data[entry.user_log_id] = entry.get_dict() |
|
1260 | 1263 | |
|
1261 | 1264 | response = Response(json.dumps(audit_log_data, indent=4)) |
|
1262 | 1265 | response.content_disposition = str( |
|
1263 | 1266 | 'attachment; filename=%s' % 'user_{}_audit_logs.json'.format(c.user.user_id)) |
|
1264 | 1267 | response.content_type = 'application/json' |
|
1265 | 1268 | |
|
1266 | 1269 | return response |
|
1267 | 1270 | |
|
1268 | 1271 | @LoginRequired() |
|
1269 | 1272 | @HasPermissionAllDecorator('hg.admin') |
|
1270 | 1273 | @view_config( |
|
1271 | 1274 | route_name='edit_user_perms_summary', request_method='GET', |
|
1272 | 1275 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
1273 | 1276 | def user_perms_summary(self): |
|
1274 | 1277 | _ = self.request.translate |
|
1275 | 1278 | c = self.load_default_context() |
|
1276 | 1279 | c.user = self.db_user |
|
1277 | 1280 | |
|
1278 | 1281 | c.active = 'perms_summary' |
|
1279 | 1282 | c.perm_user = c.user.AuthUser(ip_addr=self.request.remote_addr) |
|
1280 | 1283 | |
|
1281 | 1284 | return self._get_template_context(c) |
|
1282 | 1285 | |
|
1283 | 1286 | @LoginRequired() |
|
1284 | 1287 | @HasPermissionAllDecorator('hg.admin') |
|
1285 | 1288 | @view_config( |
|
1286 | 1289 | route_name='edit_user_perms_summary_json', request_method='GET', |
|
1287 | 1290 | renderer='json_ext') |
|
1288 | 1291 | def user_perms_summary_json(self): |
|
1289 | 1292 | self.load_default_context() |
|
1290 | 1293 | perm_user = self.db_user.AuthUser(ip_addr=self.request.remote_addr) |
|
1291 | 1294 | |
|
1292 | 1295 | return perm_user.permissions |
|
1293 | 1296 | |
|
1294 | 1297 | @LoginRequired() |
|
1295 | 1298 | @HasPermissionAllDecorator('hg.admin') |
|
1296 | 1299 | @view_config( |
|
1297 | 1300 | route_name='edit_user_caches', request_method='GET', |
|
1298 | 1301 | renderer='rhodecode:templates/admin/users/user_edit.mako') |
|
1299 | 1302 | def user_caches(self): |
|
1300 | 1303 | _ = self.request.translate |
|
1301 | 1304 | c = self.load_default_context() |
|
1302 | 1305 | c.user = self.db_user |
|
1303 | 1306 | |
|
1304 | 1307 | c.active = 'caches' |
|
1305 | 1308 | c.perm_user = c.user.AuthUser(ip_addr=self.request.remote_addr) |
|
1306 | 1309 | |
|
1307 | 1310 | cache_namespace_uid = 'cache_user_auth.{}'.format(self.db_user.user_id) |
|
1308 | 1311 | c.region = rc_cache.get_or_create_region('cache_perms', cache_namespace_uid) |
|
1309 | 1312 | c.backend = c.region.backend |
|
1310 | 1313 | c.user_keys = sorted(c.region.backend.list_keys(prefix=cache_namespace_uid)) |
|
1311 | 1314 | |
|
1312 | 1315 | return self._get_template_context(c) |
|
1313 | 1316 | |
|
1314 | 1317 | @LoginRequired() |
|
1315 | 1318 | @HasPermissionAllDecorator('hg.admin') |
|
1316 | 1319 | @CSRFRequired() |
|
1317 | 1320 | @view_config( |
|
1318 | 1321 | route_name='edit_user_caches_update', request_method='POST') |
|
1319 | 1322 | def user_caches_update(self): |
|
1320 | 1323 | _ = self.request.translate |
|
1321 | 1324 | c = self.load_default_context() |
|
1322 | 1325 | c.user = self.db_user |
|
1323 | 1326 | |
|
1324 | 1327 | c.active = 'caches' |
|
1325 | 1328 | c.perm_user = c.user.AuthUser(ip_addr=self.request.remote_addr) |
|
1326 | 1329 | |
|
1327 | 1330 | cache_namespace_uid = 'cache_user_auth.{}'.format(self.db_user.user_id) |
|
1328 | 1331 | del_keys = rc_cache.clear_cache_namespace('cache_perms', cache_namespace_uid) |
|
1329 | 1332 | |
|
1330 | 1333 | h.flash(_("Deleted {} cache keys").format(del_keys), category='success') |
|
1331 | 1334 | |
|
1332 | 1335 | return HTTPFound(h.route_path( |
|
1333 | 1336 | 'edit_user_caches', user_id=c.user.user_id)) |
@@ -1,174 +1,195 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | import logging |
|
21 | 21 | |
|
22 | 22 | from pyramid.view import view_config |
|
23 | 23 | from pyramid.response import FileResponse |
|
24 | 24 | from pyramid.httpexceptions import HTTPFound, HTTPNotFound |
|
25 | 25 | |
|
26 | 26 | from rhodecode.apps._base import BaseAppView |
|
27 | 27 | from rhodecode.apps.file_store import utils |
|
28 | 28 | from rhodecode.apps.file_store.exceptions import ( |
|
29 | 29 | FileNotAllowedException, FileOverSizeException) |
|
30 | 30 | |
|
31 | 31 | from rhodecode.lib import helpers as h |
|
32 | 32 | from rhodecode.lib import audit_logger |
|
33 | 33 | from rhodecode.lib.auth import ( |
|
34 | 34 | CSRFRequired, NotAnonymous, HasRepoPermissionAny, HasRepoGroupPermissionAny, |
|
35 | 35 | LoginRequired) |
|
36 | from rhodecode.lib.vcs.conf.mtypes import get_mimetypes_db | |
|
36 | 37 | from rhodecode.model.db import Session, FileStore, UserApiKeys |
|
37 | 38 | |
|
38 | 39 | log = logging.getLogger(__name__) |
|
39 | 40 | |
|
40 | 41 | |
|
41 | 42 | class FileStoreView(BaseAppView): |
|
42 | 43 | upload_key = 'store_file' |
|
43 | 44 | |
|
44 | 45 | def load_default_context(self): |
|
45 | 46 | c = self._get_local_tmpl_context() |
|
46 | 47 | self.storage = utils.get_file_storage(self.request.registry.settings) |
|
47 | 48 | return c |
|
48 | 49 | |
|
50 | def _guess_type(self, file_name): | |
|
51 | """ | |
|
52 | Our own type guesser for mimetypes using the rich DB | |
|
53 | """ | |
|
54 | if not hasattr(self, 'db'): | |
|
55 | self.db = get_mimetypes_db() | |
|
56 | _content_type, _encoding = self.db.guess_type(file_name, strict=False) | |
|
57 | return _content_type, _encoding | |
|
58 | ||
|
49 | 59 | def _serve_file(self, file_uid): |
|
50 | 60 | |
|
51 | 61 | if not self.storage.exists(file_uid): |
|
52 | 62 | store_path = self.storage.store_path(file_uid) |
|
53 | 63 | log.debug('File with FID:%s not found in the store under `%s`', |
|
54 | 64 | file_uid, store_path) |
|
55 | 65 | raise HTTPNotFound() |
|
56 | 66 | |
|
57 | 67 | db_obj = FileStore().query().filter(FileStore.file_uid == file_uid).scalar() |
|
58 | 68 | if not db_obj: |
|
59 | 69 | raise HTTPNotFound() |
|
60 | 70 | |
|
61 | 71 | # private upload for user |
|
62 | 72 | if db_obj.check_acl and db_obj.scope_user_id: |
|
63 | 73 | log.debug('Artifact: checking scope access for bound artifact user: `%s`', |
|
64 | 74 | db_obj.scope_user_id) |
|
65 | 75 | user = db_obj.user |
|
66 | 76 | if self._rhodecode_db_user.user_id != user.user_id: |
|
67 | 77 | log.warning('Access to file store object forbidden') |
|
68 | 78 | raise HTTPNotFound() |
|
69 | 79 | |
|
70 | 80 | # scoped to repository permissions |
|
71 | 81 | if db_obj.check_acl and db_obj.scope_repo_id: |
|
72 | 82 | log.debug('Artifact: checking scope access for bound artifact repo: `%s`', |
|
73 | 83 | db_obj.scope_repo_id) |
|
74 | 84 | repo = db_obj.repo |
|
75 | 85 | perm_set = ['repository.read', 'repository.write', 'repository.admin'] |
|
76 | 86 | has_perm = HasRepoPermissionAny(*perm_set)(repo.repo_name, 'FileStore check') |
|
77 | 87 | if not has_perm: |
|
78 | 88 | log.warning('Access to file store object `%s` forbidden', file_uid) |
|
79 | 89 | raise HTTPNotFound() |
|
80 | 90 | |
|
81 | 91 | # scoped to repository group permissions |
|
82 | 92 | if db_obj.check_acl and db_obj.scope_repo_group_id: |
|
83 | 93 | log.debug('Artifact: checking scope access for bound artifact repo group: `%s`', |
|
84 | 94 | db_obj.scope_repo_group_id) |
|
85 | 95 | repo_group = db_obj.repo_group |
|
86 | 96 | perm_set = ['group.read', 'group.write', 'group.admin'] |
|
87 | 97 | has_perm = HasRepoGroupPermissionAny(*perm_set)(repo_group.group_name, 'FileStore check') |
|
88 | 98 | if not has_perm: |
|
89 | 99 | log.warning('Access to file store object `%s` forbidden', file_uid) |
|
90 | 100 | raise HTTPNotFound() |
|
91 | 101 | |
|
92 | 102 | FileStore.bump_access_counter(file_uid) |
|
93 | 103 | |
|
94 | 104 | file_path = self.storage.store_path(file_uid) |
|
95 | return FileResponse(file_path) | |
|
105 | content_type = 'application/octet-stream' | |
|
106 | content_encoding = None | |
|
107 | ||
|
108 | _content_type, _encoding = self._guess_type(file_path) | |
|
109 | if _content_type: | |
|
110 | content_type = _content_type | |
|
111 | ||
|
112 | # For file store we don't submit any session data, this logic tells the | |
|
113 | # Session lib to skip it | |
|
114 | setattr(self.request, '_file_response', True) | |
|
115 | return FileResponse(file_path, request=self.request, | |
|
116 | content_type=content_type, content_encoding=content_encoding) | |
|
96 | 117 | |
|
97 | 118 | @LoginRequired() |
|
98 | 119 | @NotAnonymous() |
|
99 | 120 | @CSRFRequired() |
|
100 | 121 | @view_config(route_name='upload_file', request_method='POST', renderer='json_ext') |
|
101 | 122 | def upload_file(self): |
|
102 | 123 | self.load_default_context() |
|
103 | 124 | file_obj = self.request.POST.get(self.upload_key) |
|
104 | 125 | |
|
105 | 126 | if file_obj is None: |
|
106 | 127 | return {'store_fid': None, |
|
107 | 128 | 'access_path': None, |
|
108 | 129 | 'error': '{} data field is missing'.format(self.upload_key)} |
|
109 | 130 | |
|
110 | 131 | if not hasattr(file_obj, 'filename'): |
|
111 | 132 | return {'store_fid': None, |
|
112 | 133 | 'access_path': None, |
|
113 | 134 | 'error': 'filename cannot be read from the data field'} |
|
114 | 135 | |
|
115 | 136 | filename = file_obj.filename |
|
116 | 137 | |
|
117 | 138 | metadata = { |
|
118 | 139 | 'user_uploaded': {'username': self._rhodecode_user.username, |
|
119 | 140 | 'user_id': self._rhodecode_user.user_id, |
|
120 | 141 | 'ip': self._rhodecode_user.ip_addr}} |
|
121 | 142 | try: |
|
122 | 143 | store_uid, metadata = self.storage.save_file( |
|
123 | 144 | file_obj.file, filename, extra_metadata=metadata) |
|
124 | 145 | except FileNotAllowedException: |
|
125 | 146 | return {'store_fid': None, |
|
126 | 147 | 'access_path': None, |
|
127 | 148 | 'error': 'File {} is not allowed.'.format(filename)} |
|
128 | 149 | |
|
129 | 150 | except FileOverSizeException: |
|
130 | 151 | return {'store_fid': None, |
|
131 | 152 | 'access_path': None, |
|
132 | 153 | 'error': 'File {} is exceeding allowed limit.'.format(filename)} |
|
133 | 154 | |
|
134 | 155 | try: |
|
135 | 156 | entry = FileStore.create( |
|
136 | 157 | file_uid=store_uid, filename=metadata["filename"], |
|
137 | 158 | file_hash=metadata["sha256"], file_size=metadata["size"], |
|
138 | 159 | file_description=u'upload attachment', |
|
139 | 160 | check_acl=False, user_id=self._rhodecode_user.user_id |
|
140 | 161 | ) |
|
141 | 162 | Session().add(entry) |
|
142 | 163 | Session().commit() |
|
143 | 164 | log.debug('Stored upload in DB as %s', entry) |
|
144 | 165 | except Exception: |
|
145 | 166 | log.exception('Failed to store file %s', filename) |
|
146 | 167 | return {'store_fid': None, |
|
147 | 168 | 'access_path': None, |
|
148 | 169 | 'error': 'File {} failed to store in DB.'.format(filename)} |
|
149 | 170 | |
|
150 | 171 | return {'store_fid': store_uid, |
|
151 | 172 | 'access_path': h.route_path('download_file', fid=store_uid)} |
|
152 | 173 | |
|
153 | 174 | # ACL is checked by scopes, if no scope the file is accessible to all |
|
154 | 175 | @view_config(route_name='download_file') |
|
155 | 176 | def download_file(self): |
|
156 | 177 | self.load_default_context() |
|
157 | 178 | file_uid = self.request.matchdict['fid'] |
|
158 | 179 | log.debug('Requesting FID:%s from store %s', file_uid, self.storage) |
|
159 | 180 | return self._serve_file(file_uid) |
|
160 | 181 | |
|
161 | 182 | # in addition to @LoginRequired ACL is checked by scopes |
|
162 | 183 | @LoginRequired(auth_token_access=[UserApiKeys.ROLE_ARTIFACT_DOWNLOAD]) |
|
163 | 184 | @NotAnonymous() |
|
164 | 185 | @view_config(route_name='download_file_by_token') |
|
165 | 186 | def download_file_by_token(self): |
|
166 | 187 | """ |
|
167 | 188 | Special view that allows to access the download file by special URL that |
|
168 | 189 | is stored inside the URL. |
|
169 | 190 | |
|
170 | 191 | http://example.com/_file_store/token-download/TOKEN/FILE_UID |
|
171 | 192 | """ |
|
172 | 193 | self.load_default_context() |
|
173 | 194 | file_uid = self.request.matchdict['fid'] |
|
174 | 195 | return self._serve_file(file_uid) |
@@ -1,391 +1,391 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import mock |
|
22 | 22 | import pytest |
|
23 | 23 | |
|
24 | 24 | from rhodecode.lib import helpers as h |
|
25 | 25 | from rhodecode.model.db import User, Gist |
|
26 | 26 | from rhodecode.model.gist import GistModel |
|
27 | 27 | from rhodecode.model.meta import Session |
|
28 | 28 | from rhodecode.tests import ( |
|
29 | 29 | TEST_USER_ADMIN_LOGIN, TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS, |
|
30 | 30 | TestController, assert_session_flash) |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | def route_path(name, params=None, **kwargs): |
|
34 | 34 | import urllib |
|
35 | 35 | from rhodecode.apps._base import ADMIN_PREFIX |
|
36 | 36 | |
|
37 | 37 | base_url = { |
|
38 | 38 | 'gists_show': ADMIN_PREFIX + '/gists', |
|
39 | 39 | 'gists_new': ADMIN_PREFIX + '/gists/new', |
|
40 | 40 | 'gists_create': ADMIN_PREFIX + '/gists/create', |
|
41 | 41 | 'gist_show': ADMIN_PREFIX + '/gists/{gist_id}', |
|
42 | 42 | 'gist_delete': ADMIN_PREFIX + '/gists/{gist_id}/delete', |
|
43 | 43 | 'gist_edit': ADMIN_PREFIX + '/gists/{gist_id}/edit', |
|
44 | 44 | 'gist_edit_check_revision': ADMIN_PREFIX + '/gists/{gist_id}/edit/check_revision', |
|
45 | 45 | 'gist_update': ADMIN_PREFIX + '/gists/{gist_id}/update', |
|
46 | 46 | 'gist_show_rev': ADMIN_PREFIX + '/gists/{gist_id}/{revision}', |
|
47 | 47 | 'gist_show_formatted': ADMIN_PREFIX + '/gists/{gist_id}/{revision}/{format}', |
|
48 | 48 | 'gist_show_formatted_path': ADMIN_PREFIX + '/gists/{gist_id}/{revision}/{format}/{f_path}', |
|
49 | 49 | |
|
50 | 50 | }[name].format(**kwargs) |
|
51 | 51 | |
|
52 | 52 | if params: |
|
53 | 53 | base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) |
|
54 | 54 | return base_url |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | class GistUtility(object): |
|
58 | 58 | |
|
59 | 59 | def __init__(self): |
|
60 | 60 | self._gist_ids = [] |
|
61 | 61 | |
|
62 | 62 | def __call__( |
|
63 | 63 | self, f_name, content='some gist', lifetime=-1, |
|
64 | 64 | description='gist-desc', gist_type='public', |
|
65 | 65 | acl_level=Gist.GIST_PUBLIC, owner=TEST_USER_ADMIN_LOGIN): |
|
66 | 66 | gist_mapping = { |
|
67 | 67 | f_name: {'content': content} |
|
68 | 68 | } |
|
69 | 69 | user = User.get_by_username(owner) |
|
70 | 70 | gist = GistModel().create( |
|
71 | 71 | description, owner=user, gist_mapping=gist_mapping, |
|
72 | 72 | gist_type=gist_type, lifetime=lifetime, gist_acl_level=acl_level) |
|
73 | 73 | Session().commit() |
|
74 | 74 | self._gist_ids.append(gist.gist_id) |
|
75 | 75 | return gist |
|
76 | 76 | |
|
77 | 77 | def cleanup(self): |
|
78 | 78 | for gist_id in self._gist_ids: |
|
79 | 79 | gist = Gist.get(gist_id) |
|
80 | 80 | if gist: |
|
81 | 81 | Session().delete(gist) |
|
82 | 82 | |
|
83 | 83 | Session().commit() |
|
84 | 84 | |
|
85 | 85 | |
|
86 | 86 | @pytest.fixture() |
|
87 | 87 | def create_gist(request): |
|
88 | 88 | gist_utility = GistUtility() |
|
89 | 89 | request.addfinalizer(gist_utility.cleanup) |
|
90 | 90 | return gist_utility |
|
91 | 91 | |
|
92 | 92 | |
|
93 | 93 | class TestGistsController(TestController): |
|
94 | 94 | |
|
95 | 95 | def test_index_empty(self, create_gist): |
|
96 | 96 | self.log_user() |
|
97 | 97 | response = self.app.get(route_path('gists_show')) |
|
98 | 98 | response.mustcontain('data: [],') |
|
99 | 99 | |
|
100 | 100 | def test_index(self, create_gist): |
|
101 | 101 | self.log_user() |
|
102 | 102 | g1 = create_gist('gist1') |
|
103 | 103 | g2 = create_gist('gist2', lifetime=1400) |
|
104 | 104 | g3 = create_gist('gist3', description='gist3-desc') |
|
105 | 105 | g4 = create_gist('gist4', gist_type='private').gist_access_id |
|
106 | 106 | response = self.app.get(route_path('gists_show')) |
|
107 | 107 | |
|
108 |
response.mustcontain( |
|
|
109 |
response.mustcontain( |
|
|
110 |
response.mustcontain( |
|
|
108 | response.mustcontain(g1.gist_access_id) | |
|
109 | response.mustcontain(g2.gist_access_id) | |
|
110 | response.mustcontain(g3.gist_access_id) | |
|
111 | 111 | response.mustcontain('gist3-desc') |
|
112 |
response.mustcontain(no=[ |
|
|
112 | response.mustcontain(no=[g4]) | |
|
113 | 113 | |
|
114 | 114 | # Expiration information should be visible |
|
115 | 115 | expires_tag = '%s' % h.age_component( |
|
116 | 116 | h.time_to_utcdatetime(g2.gist_expires)) |
|
117 | 117 | response.mustcontain(expires_tag.replace('"', '\\"')) |
|
118 | 118 | |
|
119 | 119 | def test_index_private_gists(self, create_gist): |
|
120 | 120 | self.log_user() |
|
121 | 121 | gist = create_gist('gist5', gist_type='private') |
|
122 | 122 | response = self.app.get(route_path('gists_show', params=dict(private=1))) |
|
123 | 123 | |
|
124 | 124 | # and privates |
|
125 |
response.mustcontain( |
|
|
125 | response.mustcontain(gist.gist_access_id) | |
|
126 | 126 | |
|
127 | 127 | def test_index_show_all(self, create_gist): |
|
128 | 128 | self.log_user() |
|
129 | 129 | create_gist('gist1') |
|
130 | 130 | create_gist('gist2', lifetime=1400) |
|
131 | 131 | create_gist('gist3', description='gist3-desc') |
|
132 | 132 | create_gist('gist4', gist_type='private') |
|
133 | 133 | |
|
134 | 134 | response = self.app.get(route_path('gists_show', params=dict(all=1))) |
|
135 | 135 | |
|
136 | 136 | assert len(GistModel.get_all()) == 4 |
|
137 | 137 | # and privates |
|
138 | 138 | for gist in GistModel.get_all(): |
|
139 |
response.mustcontain( |
|
|
139 | response.mustcontain(gist.gist_access_id) | |
|
140 | 140 | |
|
141 | 141 | def test_index_show_all_hidden_from_regular(self, create_gist): |
|
142 | 142 | self.log_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS) |
|
143 | 143 | create_gist('gist2', gist_type='private') |
|
144 | 144 | create_gist('gist3', gist_type='private') |
|
145 | 145 | create_gist('gist4', gist_type='private') |
|
146 | 146 | |
|
147 | 147 | response = self.app.get(route_path('gists_show', params=dict(all=1))) |
|
148 | 148 | |
|
149 | 149 | assert len(GistModel.get_all()) == 3 |
|
150 | 150 | # since we don't have access to private in this view, we |
|
151 | 151 | # should see nothing |
|
152 | 152 | for gist in GistModel.get_all(): |
|
153 |
response.mustcontain(no=[ |
|
|
153 | response.mustcontain(no=[gist.gist_access_id]) | |
|
154 | 154 | |
|
155 | 155 | def test_create(self): |
|
156 | 156 | self.log_user() |
|
157 | 157 | response = self.app.post( |
|
158 | 158 | route_path('gists_create'), |
|
159 | 159 | params={'lifetime': -1, |
|
160 | 160 | 'content': 'gist test', |
|
161 | 161 | 'filename': 'foo', |
|
162 | 162 | 'gist_type': 'public', |
|
163 | 163 | 'gist_acl_level': Gist.ACL_LEVEL_PUBLIC, |
|
164 | 164 | 'csrf_token': self.csrf_token}, |
|
165 | 165 | status=302) |
|
166 | 166 | response = response.follow() |
|
167 | 167 | response.mustcontain('added file: foo') |
|
168 | 168 | response.mustcontain('gist test') |
|
169 | 169 | |
|
170 | 170 | def test_create_with_path_with_dirs(self): |
|
171 | 171 | self.log_user() |
|
172 | 172 | response = self.app.post( |
|
173 | 173 | route_path('gists_create'), |
|
174 | 174 | params={'lifetime': -1, |
|
175 | 175 | 'content': 'gist test', |
|
176 | 176 | 'filename': '/home/foo', |
|
177 | 177 | 'gist_type': 'public', |
|
178 | 178 | 'gist_acl_level': Gist.ACL_LEVEL_PUBLIC, |
|
179 | 179 | 'csrf_token': self.csrf_token}, |
|
180 | 180 | status=200) |
|
181 | 181 | response.mustcontain('Filename /home/foo cannot be inside a directory') |
|
182 | 182 | |
|
183 | 183 | def test_access_expired_gist(self, create_gist): |
|
184 | 184 | self.log_user() |
|
185 | 185 | gist = create_gist('never-see-me') |
|
186 | 186 | gist.gist_expires = 0 # 1970 |
|
187 | 187 | Session().add(gist) |
|
188 | 188 | Session().commit() |
|
189 | 189 | |
|
190 | 190 | self.app.get(route_path('gist_show', gist_id=gist.gist_access_id), |
|
191 | 191 | status=404) |
|
192 | 192 | |
|
193 | 193 | def test_create_private(self): |
|
194 | 194 | self.log_user() |
|
195 | 195 | response = self.app.post( |
|
196 | 196 | route_path('gists_create'), |
|
197 | 197 | params={'lifetime': -1, |
|
198 | 198 | 'content': 'private gist test', |
|
199 | 199 | 'filename': 'private-foo', |
|
200 | 200 | 'gist_type': 'private', |
|
201 | 201 | 'gist_acl_level': Gist.ACL_LEVEL_PUBLIC, |
|
202 | 202 | 'csrf_token': self.csrf_token}, |
|
203 | 203 | status=302) |
|
204 | 204 | response = response.follow() |
|
205 | 205 | response.mustcontain('added file: private-foo<') |
|
206 | 206 | response.mustcontain('private gist test') |
|
207 | 207 | response.mustcontain('Private Gist') |
|
208 | 208 | # Make sure private gists are not indexed by robots |
|
209 | 209 | response.mustcontain( |
|
210 | 210 | '<meta name="robots" content="noindex, nofollow">') |
|
211 | 211 | |
|
212 | 212 | def test_create_private_acl_private(self): |
|
213 | 213 | self.log_user() |
|
214 | 214 | response = self.app.post( |
|
215 | 215 | route_path('gists_create'), |
|
216 | 216 | params={'lifetime': -1, |
|
217 | 217 | 'content': 'private gist test', |
|
218 | 218 | 'filename': 'private-foo', |
|
219 | 219 | 'gist_type': 'private', |
|
220 | 220 | 'gist_acl_level': Gist.ACL_LEVEL_PRIVATE, |
|
221 | 221 | 'csrf_token': self.csrf_token}, |
|
222 | 222 | status=302) |
|
223 | 223 | response = response.follow() |
|
224 | 224 | response.mustcontain('added file: private-foo<') |
|
225 | 225 | response.mustcontain('private gist test') |
|
226 | 226 | response.mustcontain('Private Gist') |
|
227 | 227 | # Make sure private gists are not indexed by robots |
|
228 | 228 | response.mustcontain( |
|
229 | 229 | '<meta name="robots" content="noindex, nofollow">') |
|
230 | 230 | |
|
231 | 231 | def test_create_with_description(self): |
|
232 | 232 | self.log_user() |
|
233 | 233 | response = self.app.post( |
|
234 | 234 | route_path('gists_create'), |
|
235 | 235 | params={'lifetime': -1, |
|
236 | 236 | 'content': 'gist test', |
|
237 | 237 | 'filename': 'foo-desc', |
|
238 | 238 | 'description': 'gist-desc', |
|
239 | 239 | 'gist_type': 'public', |
|
240 | 240 | 'gist_acl_level': Gist.ACL_LEVEL_PUBLIC, |
|
241 | 241 | 'csrf_token': self.csrf_token}, |
|
242 | 242 | status=302) |
|
243 | 243 | response = response.follow() |
|
244 | 244 | response.mustcontain('added file: foo-desc') |
|
245 | 245 | response.mustcontain('gist test') |
|
246 | 246 | response.mustcontain('gist-desc') |
|
247 | 247 | |
|
248 | 248 | def test_create_public_with_anonymous_access(self): |
|
249 | 249 | self.log_user() |
|
250 | 250 | params = { |
|
251 | 251 | 'lifetime': -1, |
|
252 | 252 | 'content': 'gist test', |
|
253 | 253 | 'filename': 'foo-desc', |
|
254 | 254 | 'description': 'gist-desc', |
|
255 | 255 | 'gist_type': 'public', |
|
256 | 256 | 'gist_acl_level': Gist.ACL_LEVEL_PUBLIC, |
|
257 | 257 | 'csrf_token': self.csrf_token |
|
258 | 258 | } |
|
259 | 259 | response = self.app.post( |
|
260 | 260 | route_path('gists_create'), params=params, status=302) |
|
261 | 261 | self.logout_user() |
|
262 | 262 | response = response.follow() |
|
263 | 263 | response.mustcontain('added file: foo-desc') |
|
264 | 264 | response.mustcontain('gist test') |
|
265 | 265 | response.mustcontain('gist-desc') |
|
266 | 266 | |
|
267 | 267 | def test_new(self): |
|
268 | 268 | self.log_user() |
|
269 | 269 | self.app.get(route_path('gists_new')) |
|
270 | 270 | |
|
271 | 271 | def test_delete(self, create_gist): |
|
272 | 272 | self.log_user() |
|
273 | 273 | gist = create_gist('delete-me') |
|
274 | 274 | response = self.app.post( |
|
275 | 275 | route_path('gist_delete', gist_id=gist.gist_id), |
|
276 | 276 | params={'csrf_token': self.csrf_token}) |
|
277 | 277 | assert_session_flash(response, 'Deleted gist %s' % gist.gist_id) |
|
278 | 278 | |
|
279 | 279 | def test_delete_normal_user_his_gist(self, create_gist): |
|
280 | 280 | self.log_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS) |
|
281 | 281 | gist = create_gist('delete-me', owner=TEST_USER_REGULAR_LOGIN) |
|
282 | 282 | |
|
283 | 283 | response = self.app.post( |
|
284 | 284 | route_path('gist_delete', gist_id=gist.gist_id), |
|
285 | 285 | params={'csrf_token': self.csrf_token}) |
|
286 | 286 | assert_session_flash(response, 'Deleted gist %s' % gist.gist_id) |
|
287 | 287 | |
|
288 | 288 | def test_delete_normal_user_not_his_own_gist(self, create_gist): |
|
289 | 289 | self.log_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS) |
|
290 | 290 | gist = create_gist('delete-me-2') |
|
291 | 291 | |
|
292 | 292 | self.app.post( |
|
293 | 293 | route_path('gist_delete', gist_id=gist.gist_id), |
|
294 | 294 | params={'csrf_token': self.csrf_token}, status=404) |
|
295 | 295 | |
|
296 | 296 | def test_show(self, create_gist): |
|
297 | 297 | gist = create_gist('gist-show-me') |
|
298 | 298 | response = self.app.get(route_path('gist_show', gist_id=gist.gist_access_id)) |
|
299 | 299 | |
|
300 | 300 | response.mustcontain('added file: gist-show-me<') |
|
301 | 301 | |
|
302 | 302 | assert_response = response.assert_response() |
|
303 | 303 | assert_response.element_equals_to( |
|
304 | 304 | 'div.rc-user span.user', |
|
305 | 305 | '<a href="/_profiles/test_admin">test_admin</a></span>') |
|
306 | 306 | |
|
307 | 307 | response.mustcontain('gist-desc') |
|
308 | 308 | |
|
309 | 309 | def test_show_without_hg(self, create_gist): |
|
310 | 310 | with mock.patch( |
|
311 | 311 | 'rhodecode.lib.vcs.settings.ALIASES', ['git']): |
|
312 | 312 | gist = create_gist('gist-show-me-again') |
|
313 | 313 | self.app.get( |
|
314 | 314 | route_path('gist_show', gist_id=gist.gist_access_id), status=200) |
|
315 | 315 | |
|
316 | 316 | def test_show_acl_private(self, create_gist): |
|
317 | 317 | gist = create_gist('gist-show-me-only-when-im-logged-in', |
|
318 | 318 | acl_level=Gist.ACL_LEVEL_PRIVATE) |
|
319 | 319 | self.app.get( |
|
320 | 320 | route_path('gist_show', gist_id=gist.gist_access_id), status=404) |
|
321 | 321 | |
|
322 | 322 | # now we log-in we should see thi gist |
|
323 | 323 | self.log_user() |
|
324 | 324 | response = self.app.get( |
|
325 | 325 | route_path('gist_show', gist_id=gist.gist_access_id)) |
|
326 | 326 | response.mustcontain('added file: gist-show-me-only-when-im-logged-in') |
|
327 | 327 | |
|
328 | 328 | assert_response = response.assert_response() |
|
329 | 329 | assert_response.element_equals_to( |
|
330 | 330 | 'div.rc-user span.user', |
|
331 | 331 | '<a href="/_profiles/test_admin">test_admin</a></span>') |
|
332 | 332 | response.mustcontain('gist-desc') |
|
333 | 333 | |
|
334 | 334 | def test_show_as_raw(self, create_gist): |
|
335 | 335 | gist = create_gist('gist-show-me', content='GIST CONTENT') |
|
336 | 336 | response = self.app.get( |
|
337 | 337 | route_path('gist_show_formatted', |
|
338 | 338 | gist_id=gist.gist_access_id, revision='tip', |
|
339 | 339 | format='raw')) |
|
340 | 340 | assert response.body == 'GIST CONTENT' |
|
341 | 341 | |
|
342 | 342 | def test_show_as_raw_individual_file(self, create_gist): |
|
343 | 343 | gist = create_gist('gist-show-me-raw', content='GIST BODY') |
|
344 | 344 | response = self.app.get( |
|
345 | 345 | route_path('gist_show_formatted_path', |
|
346 | 346 | gist_id=gist.gist_access_id, format='raw', |
|
347 | 347 | revision='tip', f_path='gist-show-me-raw')) |
|
348 | 348 | assert response.body == 'GIST BODY' |
|
349 | 349 | |
|
350 | 350 | def test_edit_page(self, create_gist): |
|
351 | 351 | self.log_user() |
|
352 | 352 | gist = create_gist('gist-for-edit', content='GIST EDIT BODY') |
|
353 | 353 | response = self.app.get(route_path('gist_edit', gist_id=gist.gist_access_id)) |
|
354 | 354 | response.mustcontain('GIST EDIT BODY') |
|
355 | 355 | |
|
356 | 356 | def test_edit_page_non_logged_user(self, create_gist): |
|
357 | 357 | gist = create_gist('gist-for-edit', content='GIST EDIT BODY') |
|
358 | 358 | self.app.get(route_path('gist_edit', gist_id=gist.gist_access_id), |
|
359 | 359 | status=302) |
|
360 | 360 | |
|
361 | 361 | def test_edit_normal_user_his_gist(self, create_gist): |
|
362 | 362 | self.log_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS) |
|
363 | 363 | gist = create_gist('gist-for-edit', owner=TEST_USER_REGULAR_LOGIN) |
|
364 | 364 | self.app.get(route_path('gist_edit', gist_id=gist.gist_access_id, |
|
365 | 365 | status=200)) |
|
366 | 366 | |
|
367 | 367 | def test_edit_normal_user_not_his_own_gist(self, create_gist): |
|
368 | 368 | self.log_user(TEST_USER_REGULAR_LOGIN, TEST_USER_REGULAR_PASS) |
|
369 | 369 | gist = create_gist('delete-me') |
|
370 | 370 | self.app.get(route_path('gist_edit', gist_id=gist.gist_access_id), |
|
371 | 371 | status=404) |
|
372 | 372 | |
|
373 | 373 | def test_user_first_name_is_escaped(self, user_util, create_gist): |
|
374 | 374 | xss_atack_string = '"><script>alert(\'First Name\')</script>' |
|
375 | 375 | xss_escaped_string = h.html_escape(h.escape(xss_atack_string)) |
|
376 | 376 | password = 'test' |
|
377 | 377 | user = user_util.create_user( |
|
378 | 378 | firstname=xss_atack_string, password=password) |
|
379 | 379 | create_gist('gist', gist_type='public', owner=user.username) |
|
380 | 380 | response = self.app.get(route_path('gists_show')) |
|
381 | 381 | response.mustcontain(xss_escaped_string) |
|
382 | 382 | |
|
383 | 383 | def test_user_last_name_is_escaped(self, user_util, create_gist): |
|
384 | 384 | xss_atack_string = '"><script>alert(\'Last Name\')</script>' |
|
385 | 385 | xss_escaped_string = h.html_escape(h.escape(xss_atack_string)) |
|
386 | 386 | password = 'test' |
|
387 | 387 | user = user_util.create_user( |
|
388 | 388 | lastname=xss_atack_string, password=password) |
|
389 | 389 | create_gist('gist', gist_type='public', owner=user.username) |
|
390 | 390 | response = self.app.get(route_path('gists_show')) |
|
391 | 391 | response.mustcontain(xss_escaped_string) |
@@ -1,69 +1,69 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | def assert_and_get_main_filter_content(result): |
|
23 | 23 | repos = [] |
|
24 | 24 | groups = [] |
|
25 | 25 | commits = [] |
|
26 | 26 | users = [] |
|
27 | 27 | for data_item in result: |
|
28 | 28 | assert data_item['id'] |
|
29 | 29 | assert data_item['value'] |
|
30 | 30 | assert data_item['value_display'] |
|
31 | 31 | assert data_item['url'] |
|
32 | 32 | |
|
33 | 33 | if data_item['type'] == 'search': |
|
34 | 34 | display_val = data_item['value_display'] |
|
35 | 35 | if data_item['id'] == -1: |
|
36 | assert 'File search for:' in display_val, display_val | |
|
36 | assert 'File content search for:' in display_val, display_val | |
|
37 | 37 | elif data_item['id'] == -2: |
|
38 | 38 | assert 'Commit search for:' in display_val, display_val |
|
39 | 39 | else: |
|
40 | 40 | assert False, 'No Proper ID returned {}'.format(data_item['id']) |
|
41 | 41 | |
|
42 | 42 | elif data_item['type'] == 'repo': |
|
43 | 43 | repos.append(data_item) |
|
44 | 44 | elif data_item['type'] == 'repo_group': |
|
45 | 45 | groups.append(data_item) |
|
46 | 46 | elif data_item['type'] == 'user': |
|
47 | 47 | users.append(data_item) |
|
48 | 48 | elif data_item['type'] == 'commit': |
|
49 | 49 | commits.append(data_item) |
|
50 | 50 | else: |
|
51 | 51 | raise Exception('invalid type `%s`' % data_item['type']) |
|
52 | 52 | |
|
53 | 53 | return repos, groups, users, commits |
|
54 | 54 | |
|
55 | 55 | |
|
56 | 56 | def assert_and_get_repo_list_content(result): |
|
57 | 57 | repos = [] |
|
58 | 58 | for data in result: |
|
59 | 59 | for data_item in data['children']: |
|
60 | 60 | assert data_item['id'] |
|
61 | 61 | assert data_item['text'] |
|
62 | 62 | assert data_item['url'] |
|
63 | 63 | |
|
64 | 64 | if data_item['type'] == 'repo': |
|
65 | 65 | repos.append(data_item) |
|
66 | 66 | else: |
|
67 | 67 | raise Exception('invalid type %s' % data_item['type']) |
|
68 | 68 | |
|
69 | 69 | return repos |
@@ -1,830 +1,830 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import re |
|
22 | 22 | import logging |
|
23 | 23 | import collections |
|
24 | 24 | |
|
25 | 25 | from pyramid.httpexceptions import HTTPNotFound |
|
26 | 26 | from pyramid.view import view_config |
|
27 | 27 | |
|
28 | 28 | from rhodecode.apps._base import BaseAppView, DataGridAppView |
|
29 | 29 | from rhodecode.lib import helpers as h |
|
30 | 30 | from rhodecode.lib.auth import ( |
|
31 | 31 | LoginRequired, NotAnonymous, HasRepoGroupPermissionAnyDecorator, CSRFRequired, |
|
32 | 32 | HasRepoGroupPermissionAny, AuthUser) |
|
33 | 33 | from rhodecode.lib.codeblocks import filenode_as_lines_tokens |
|
34 | 34 | from rhodecode.lib.index import searcher_from_config |
|
35 | 35 | from rhodecode.lib.utils2 import safe_unicode, str2bool, safe_int |
|
36 | 36 | from rhodecode.lib.vcs.nodes import FileNode |
|
37 | 37 | from rhodecode.model.db import ( |
|
38 | 38 | func, true, or_, case, in_filter_generator, Session, |
|
39 | 39 | Repository, RepoGroup, User, UserGroup) |
|
40 | 40 | from rhodecode.model.repo import RepoModel |
|
41 | 41 | from rhodecode.model.repo_group import RepoGroupModel |
|
42 | 42 | from rhodecode.model.user import UserModel |
|
43 | 43 | from rhodecode.model.user_group import UserGroupModel |
|
44 | 44 | |
|
45 | 45 | log = logging.getLogger(__name__) |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | class HomeView(BaseAppView, DataGridAppView): |
|
49 | 49 | |
|
50 | 50 | def load_default_context(self): |
|
51 | 51 | c = self._get_local_tmpl_context() |
|
52 | 52 | c.user = c.auth_user.get_instance() |
|
53 | 53 | |
|
54 | 54 | return c |
|
55 | 55 | |
|
56 | 56 | @LoginRequired() |
|
57 | 57 | @view_config( |
|
58 | 58 | route_name='user_autocomplete_data', request_method='GET', |
|
59 | 59 | renderer='json_ext', xhr=True) |
|
60 | 60 | def user_autocomplete_data(self): |
|
61 | 61 | self.load_default_context() |
|
62 | 62 | query = self.request.GET.get('query') |
|
63 | 63 | active = str2bool(self.request.GET.get('active') or True) |
|
64 | 64 | include_groups = str2bool(self.request.GET.get('user_groups')) |
|
65 | 65 | expand_groups = str2bool(self.request.GET.get('user_groups_expand')) |
|
66 | 66 | skip_default_user = str2bool(self.request.GET.get('skip_default_user')) |
|
67 | 67 | |
|
68 | 68 | log.debug('generating user list, query:%s, active:%s, with_groups:%s', |
|
69 | 69 | query, active, include_groups) |
|
70 | 70 | |
|
71 | 71 | _users = UserModel().get_users( |
|
72 | 72 | name_contains=query, only_active=active) |
|
73 | 73 | |
|
74 | 74 | def maybe_skip_default_user(usr): |
|
75 | 75 | if skip_default_user and usr['username'] == UserModel.cls.DEFAULT_USER: |
|
76 | 76 | return False |
|
77 | 77 | return True |
|
78 | 78 | _users = filter(maybe_skip_default_user, _users) |
|
79 | 79 | |
|
80 | 80 | if include_groups: |
|
81 | 81 | # extend with user groups |
|
82 | 82 | _user_groups = UserGroupModel().get_user_groups( |
|
83 | 83 | name_contains=query, only_active=active, |
|
84 | 84 | expand_groups=expand_groups) |
|
85 | 85 | _users = _users + _user_groups |
|
86 | 86 | |
|
87 | 87 | return {'suggestions': _users} |
|
88 | 88 | |
|
89 | 89 | @LoginRequired() |
|
90 | 90 | @NotAnonymous() |
|
91 | 91 | @view_config( |
|
92 | 92 | route_name='user_group_autocomplete_data', request_method='GET', |
|
93 | 93 | renderer='json_ext', xhr=True) |
|
94 | 94 | def user_group_autocomplete_data(self): |
|
95 | 95 | self.load_default_context() |
|
96 | 96 | query = self.request.GET.get('query') |
|
97 | 97 | active = str2bool(self.request.GET.get('active') or True) |
|
98 | 98 | expand_groups = str2bool(self.request.GET.get('user_groups_expand')) |
|
99 | 99 | |
|
100 | 100 | log.debug('generating user group list, query:%s, active:%s', |
|
101 | 101 | query, active) |
|
102 | 102 | |
|
103 | 103 | _user_groups = UserGroupModel().get_user_groups( |
|
104 | 104 | name_contains=query, only_active=active, |
|
105 | 105 | expand_groups=expand_groups) |
|
106 | 106 | _user_groups = _user_groups |
|
107 | 107 | |
|
108 | 108 | return {'suggestions': _user_groups} |
|
109 | 109 | |
|
110 | 110 | def _get_repo_list(self, name_contains=None, repo_type=None, repo_group_name='', limit=20): |
|
111 | 111 | org_query = name_contains |
|
112 | 112 | allowed_ids = self._rhodecode_user.repo_acl_ids( |
|
113 | 113 | ['repository.read', 'repository.write', 'repository.admin'], |
|
114 | 114 | cache=False, name_filter=name_contains) or [-1] |
|
115 | 115 | |
|
116 | 116 | query = Session().query( |
|
117 | 117 | Repository.repo_name, |
|
118 | 118 | Repository.repo_id, |
|
119 | 119 | Repository.repo_type, |
|
120 | 120 | Repository.private, |
|
121 | 121 | )\ |
|
122 | 122 | .filter(Repository.archived.isnot(true()))\ |
|
123 | 123 | .filter(or_( |
|
124 | 124 | # generate multiple IN to fix limitation problems |
|
125 | 125 | *in_filter_generator(Repository.repo_id, allowed_ids) |
|
126 | 126 | )) |
|
127 | 127 | |
|
128 | 128 | query = query.order_by(case( |
|
129 | 129 | [ |
|
130 | 130 | (Repository.repo_name.startswith(repo_group_name), repo_group_name+'/'), |
|
131 | 131 | ], |
|
132 | 132 | )) |
|
133 | 133 | query = query.order_by(func.length(Repository.repo_name)) |
|
134 | 134 | query = query.order_by(Repository.repo_name) |
|
135 | 135 | |
|
136 | 136 | if repo_type: |
|
137 | 137 | query = query.filter(Repository.repo_type == repo_type) |
|
138 | 138 | |
|
139 | 139 | if name_contains: |
|
140 | 140 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
141 | 141 | query = query.filter( |
|
142 | 142 | Repository.repo_name.ilike(ilike_expression)) |
|
143 | 143 | query = query.limit(limit) |
|
144 | 144 | |
|
145 | 145 | acl_iter = query |
|
146 | 146 | |
|
147 | 147 | return [ |
|
148 | 148 | { |
|
149 | 149 | 'id': obj.repo_name, |
|
150 | 150 | 'value': org_query, |
|
151 | 151 | 'value_display': obj.repo_name, |
|
152 | 152 | 'text': obj.repo_name, |
|
153 | 153 | 'type': 'repo', |
|
154 | 154 | 'repo_id': obj.repo_id, |
|
155 | 155 | 'repo_type': obj.repo_type, |
|
156 | 156 | 'private': obj.private, |
|
157 | 157 | 'url': h.route_path('repo_summary', repo_name=obj.repo_name) |
|
158 | 158 | } |
|
159 | 159 | for obj in acl_iter] |
|
160 | 160 | |
|
161 | 161 | def _get_repo_group_list(self, name_contains=None, repo_group_name='', limit=20): |
|
162 | 162 | org_query = name_contains |
|
163 | 163 | allowed_ids = self._rhodecode_user.repo_group_acl_ids( |
|
164 | 164 | ['group.read', 'group.write', 'group.admin'], |
|
165 | 165 | cache=False, name_filter=name_contains) or [-1] |
|
166 | 166 | |
|
167 | 167 | query = Session().query( |
|
168 | 168 | RepoGroup.group_id, |
|
169 | 169 | RepoGroup.group_name, |
|
170 | 170 | )\ |
|
171 | 171 | .filter(or_( |
|
172 | 172 | # generate multiple IN to fix limitation problems |
|
173 | 173 | *in_filter_generator(RepoGroup.group_id, allowed_ids) |
|
174 | 174 | )) |
|
175 | 175 | |
|
176 | 176 | query = query.order_by(case( |
|
177 | 177 | [ |
|
178 | 178 | (RepoGroup.group_name.startswith(repo_group_name), repo_group_name+'/'), |
|
179 | 179 | ], |
|
180 | 180 | )) |
|
181 | 181 | query = query.order_by(func.length(RepoGroup.group_name)) |
|
182 | 182 | query = query.order_by(RepoGroup.group_name) |
|
183 | 183 | |
|
184 | 184 | if name_contains: |
|
185 | 185 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
186 | 186 | query = query.filter( |
|
187 | 187 | RepoGroup.group_name.ilike(ilike_expression)) |
|
188 | 188 | query = query.limit(limit) |
|
189 | 189 | |
|
190 | 190 | acl_iter = query |
|
191 | 191 | |
|
192 | 192 | return [ |
|
193 | 193 | { |
|
194 | 194 | 'id': obj.group_name, |
|
195 | 195 | 'value': org_query, |
|
196 | 196 | 'value_display': obj.group_name, |
|
197 | 197 | 'text': obj.group_name, |
|
198 | 198 | 'type': 'repo_group', |
|
199 | 199 | 'repo_group_id': obj.group_id, |
|
200 | 200 | 'url': h.route_path( |
|
201 | 201 | 'repo_group_home', repo_group_name=obj.group_name) |
|
202 | 202 | } |
|
203 | 203 | for obj in acl_iter] |
|
204 | 204 | |
|
205 | 205 | def _get_user_list(self, name_contains=None, limit=20): |
|
206 | 206 | org_query = name_contains |
|
207 | 207 | if not name_contains: |
|
208 | 208 | return [], False |
|
209 | 209 | |
|
210 | 210 | # TODO(marcink): should all logged in users be allowed to search others? |
|
211 | 211 | allowed_user_search = self._rhodecode_user.username != User.DEFAULT_USER |
|
212 | 212 | if not allowed_user_search: |
|
213 | 213 | return [], False |
|
214 | 214 | |
|
215 | 215 | name_contains = re.compile('(?:user:[ ]?)(.+)').findall(name_contains) |
|
216 | 216 | if len(name_contains) != 1: |
|
217 | 217 | return [], False |
|
218 | 218 | |
|
219 | 219 | name_contains = name_contains[0] |
|
220 | 220 | |
|
221 | 221 | query = User.query()\ |
|
222 | 222 | .order_by(func.length(User.username))\ |
|
223 | 223 | .order_by(User.username) \ |
|
224 | 224 | .filter(User.username != User.DEFAULT_USER) |
|
225 | 225 | |
|
226 | 226 | if name_contains: |
|
227 | 227 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
228 | 228 | query = query.filter( |
|
229 | 229 | User.username.ilike(ilike_expression)) |
|
230 | 230 | query = query.limit(limit) |
|
231 | 231 | |
|
232 | 232 | acl_iter = query |
|
233 | 233 | |
|
234 | 234 | return [ |
|
235 | 235 | { |
|
236 | 236 | 'id': obj.user_id, |
|
237 | 237 | 'value': org_query, |
|
238 | 238 | 'value_display': 'user: `{}`'.format(obj.username), |
|
239 | 239 | 'type': 'user', |
|
240 | 240 | 'icon_link': h.gravatar_url(obj.email, 30), |
|
241 | 241 | 'url': h.route_path( |
|
242 | 242 | 'user_profile', username=obj.username) |
|
243 | 243 | } |
|
244 | 244 | for obj in acl_iter], True |
|
245 | 245 | |
|
246 | 246 | def _get_user_groups_list(self, name_contains=None, limit=20): |
|
247 | 247 | org_query = name_contains |
|
248 | 248 | if not name_contains: |
|
249 | 249 | return [], False |
|
250 | 250 | |
|
251 | 251 | # TODO(marcink): should all logged in users be allowed to search others? |
|
252 | 252 | allowed_user_search = self._rhodecode_user.username != User.DEFAULT_USER |
|
253 | 253 | if not allowed_user_search: |
|
254 | 254 | return [], False |
|
255 | 255 | |
|
256 | 256 | name_contains = re.compile('(?:user_group:[ ]?)(.+)').findall(name_contains) |
|
257 | 257 | if len(name_contains) != 1: |
|
258 | 258 | return [], False |
|
259 | 259 | |
|
260 | 260 | name_contains = name_contains[0] |
|
261 | 261 | |
|
262 | 262 | query = UserGroup.query()\ |
|
263 | 263 | .order_by(func.length(UserGroup.users_group_name))\ |
|
264 | 264 | .order_by(UserGroup.users_group_name) |
|
265 | 265 | |
|
266 | 266 | if name_contains: |
|
267 | 267 | ilike_expression = u'%{}%'.format(safe_unicode(name_contains)) |
|
268 | 268 | query = query.filter( |
|
269 | 269 | UserGroup.users_group_name.ilike(ilike_expression)) |
|
270 | 270 | query = query.limit(limit) |
|
271 | 271 | |
|
272 | 272 | acl_iter = query |
|
273 | 273 | |
|
274 | 274 | return [ |
|
275 | 275 | { |
|
276 | 276 | 'id': obj.users_group_id, |
|
277 | 277 | 'value': org_query, |
|
278 | 278 | 'value_display': 'user_group: `{}`'.format(obj.users_group_name), |
|
279 | 279 | 'type': 'user_group', |
|
280 | 280 | 'url': h.route_path( |
|
281 | 281 | 'user_group_profile', user_group_name=obj.users_group_name) |
|
282 | 282 | } |
|
283 | 283 | for obj in acl_iter], True |
|
284 | 284 | |
|
285 | 285 | def _get_hash_commit_list(self, auth_user, searcher, query, repo=None, repo_group=None): |
|
286 | 286 | repo_name = repo_group_name = None |
|
287 | 287 | if repo: |
|
288 | 288 | repo_name = repo.repo_name |
|
289 | 289 | if repo_group: |
|
290 | 290 | repo_group_name = repo_group.group_name |
|
291 | 291 | |
|
292 | 292 | org_query = query |
|
293 | 293 | if not query or len(query) < 3 or not searcher: |
|
294 | 294 | return [], False |
|
295 | 295 | |
|
296 | 296 | commit_hashes = re.compile('(?:commit:[ ]?)([0-9a-f]{2,40})').findall(query) |
|
297 | 297 | |
|
298 | 298 | if len(commit_hashes) != 1: |
|
299 | 299 | return [], False |
|
300 | 300 | |
|
301 | 301 | commit_hash = commit_hashes[0] |
|
302 | 302 | |
|
303 | 303 | result = searcher.search( |
|
304 | 304 | 'commit_id:{}*'.format(commit_hash), 'commit', auth_user, |
|
305 | 305 | repo_name, repo_group_name, raise_on_exc=False) |
|
306 | 306 | |
|
307 | 307 | commits = [] |
|
308 | 308 | for entry in result['results']: |
|
309 | 309 | repo_data = { |
|
310 | 310 | 'repository_id': entry.get('repository_id'), |
|
311 | 311 | 'repository_type': entry.get('repo_type'), |
|
312 | 312 | 'repository_name': entry.get('repository'), |
|
313 | 313 | } |
|
314 | 314 | |
|
315 | 315 | commit_entry = { |
|
316 | 316 | 'id': entry['commit_id'], |
|
317 | 317 | 'value': org_query, |
|
318 | 318 | 'value_display': '`{}` commit: {}'.format( |
|
319 | 319 | entry['repository'], entry['commit_id']), |
|
320 | 320 | 'type': 'commit', |
|
321 | 321 | 'repo': entry['repository'], |
|
322 | 322 | 'repo_data': repo_data, |
|
323 | 323 | |
|
324 | 324 | 'url': h.route_path( |
|
325 | 325 | 'repo_commit', |
|
326 | 326 | repo_name=entry['repository'], commit_id=entry['commit_id']) |
|
327 | 327 | } |
|
328 | 328 | |
|
329 | 329 | commits.append(commit_entry) |
|
330 | 330 | return commits, True |
|
331 | 331 | |
|
332 | 332 | def _get_path_list(self, auth_user, searcher, query, repo=None, repo_group=None): |
|
333 | 333 | repo_name = repo_group_name = None |
|
334 | 334 | if repo: |
|
335 | 335 | repo_name = repo.repo_name |
|
336 | 336 | if repo_group: |
|
337 | 337 | repo_group_name = repo_group.group_name |
|
338 | 338 | |
|
339 | 339 | org_query = query |
|
340 | 340 | if not query or len(query) < 3 or not searcher: |
|
341 | 341 | return [], False |
|
342 | 342 | |
|
343 | 343 | paths_re = re.compile('(?:file:[ ]?)(.+)').findall(query) |
|
344 | 344 | if len(paths_re) != 1: |
|
345 | 345 | return [], False |
|
346 | 346 | |
|
347 | 347 | file_path = paths_re[0] |
|
348 | 348 | |
|
349 | 349 | search_path = searcher.escape_specials(file_path) |
|
350 | 350 | result = searcher.search( |
|
351 | 351 | 'file.raw:*{}*'.format(search_path), 'path', auth_user, |
|
352 | 352 | repo_name, repo_group_name, raise_on_exc=False) |
|
353 | 353 | |
|
354 | 354 | files = [] |
|
355 | 355 | for entry in result['results']: |
|
356 | 356 | repo_data = { |
|
357 | 357 | 'repository_id': entry.get('repository_id'), |
|
358 | 358 | 'repository_type': entry.get('repo_type'), |
|
359 | 359 | 'repository_name': entry.get('repository'), |
|
360 | 360 | } |
|
361 | 361 | |
|
362 | 362 | file_entry = { |
|
363 | 363 | 'id': entry['commit_id'], |
|
364 | 364 | 'value': org_query, |
|
365 | 365 | 'value_display': '`{}` file: {}'.format( |
|
366 | 366 | entry['repository'], entry['file']), |
|
367 | 367 | 'type': 'file', |
|
368 | 368 | 'repo': entry['repository'], |
|
369 | 369 | 'repo_data': repo_data, |
|
370 | 370 | |
|
371 | 371 | 'url': h.route_path( |
|
372 | 372 | 'repo_files', |
|
373 | 373 | repo_name=entry['repository'], commit_id=entry['commit_id'], |
|
374 | 374 | f_path=entry['file']) |
|
375 | 375 | } |
|
376 | 376 | |
|
377 | 377 | files.append(file_entry) |
|
378 | 378 | return files, True |
|
379 | 379 | |
|
380 | 380 | @LoginRequired() |
|
381 | 381 | @view_config( |
|
382 | 382 | route_name='repo_list_data', request_method='GET', |
|
383 | 383 | renderer='json_ext', xhr=True) |
|
384 | 384 | def repo_list_data(self): |
|
385 | 385 | _ = self.request.translate |
|
386 | 386 | self.load_default_context() |
|
387 | 387 | |
|
388 | 388 | query = self.request.GET.get('query') |
|
389 | 389 | repo_type = self.request.GET.get('repo_type') |
|
390 | 390 | log.debug('generating repo list, query:%s, repo_type:%s', |
|
391 | 391 | query, repo_type) |
|
392 | 392 | |
|
393 | 393 | res = [] |
|
394 | 394 | repos = self._get_repo_list(query, repo_type=repo_type) |
|
395 | 395 | if repos: |
|
396 | 396 | res.append({ |
|
397 | 397 | 'text': _('Repositories'), |
|
398 | 398 | 'children': repos |
|
399 | 399 | }) |
|
400 | 400 | |
|
401 | 401 | data = { |
|
402 | 402 | 'more': False, |
|
403 | 403 | 'results': res |
|
404 | 404 | } |
|
405 | 405 | return data |
|
406 | 406 | |
|
407 | 407 | @LoginRequired() |
|
408 | 408 | @view_config( |
|
409 | 409 | route_name='repo_group_list_data', request_method='GET', |
|
410 | 410 | renderer='json_ext', xhr=True) |
|
411 | 411 | def repo_group_list_data(self): |
|
412 | 412 | _ = self.request.translate |
|
413 | 413 | self.load_default_context() |
|
414 | 414 | |
|
415 | 415 | query = self.request.GET.get('query') |
|
416 | 416 | |
|
417 | 417 | log.debug('generating repo group list, query:%s', |
|
418 | 418 | query) |
|
419 | 419 | |
|
420 | 420 | res = [] |
|
421 | 421 | repo_groups = self._get_repo_group_list(query) |
|
422 | 422 | if repo_groups: |
|
423 | 423 | res.append({ |
|
424 | 424 | 'text': _('Repository Groups'), |
|
425 | 425 | 'children': repo_groups |
|
426 | 426 | }) |
|
427 | 427 | |
|
428 | 428 | data = { |
|
429 | 429 | 'more': False, |
|
430 | 430 | 'results': res |
|
431 | 431 | } |
|
432 | 432 | return data |
|
433 | 433 | |
|
434 | 434 | def _get_default_search_queries(self, search_context, searcher, query): |
|
435 | 435 | if not searcher: |
|
436 | 436 | return [] |
|
437 | 437 | |
|
438 | 438 | is_es_6 = searcher.is_es_6 |
|
439 | 439 | |
|
440 | 440 | queries = [] |
|
441 | 441 | repo_group_name, repo_name, repo_context = None, None, None |
|
442 | 442 | |
|
443 | 443 | # repo group context |
|
444 | 444 | if search_context.get('search_context[repo_group_name]'): |
|
445 | 445 | repo_group_name = search_context.get('search_context[repo_group_name]') |
|
446 | 446 | if search_context.get('search_context[repo_name]'): |
|
447 | 447 | repo_name = search_context.get('search_context[repo_name]') |
|
448 | 448 | repo_context = search_context.get('search_context[repo_view_type]') |
|
449 | 449 | |
|
450 | 450 | if is_es_6 and repo_name: |
|
451 | 451 | # files |
|
452 | 452 | def query_modifier(): |
|
453 | 453 | qry = query |
|
454 | 454 | return {'q': qry, 'type': 'content'} |
|
455 | 455 | |
|
456 | label = u'File search for `{}`'.format(h.escape(query)) | |
|
456 | label = u'File content search for `{}`'.format(h.escape(query)) | |
|
457 | 457 | file_qry = { |
|
458 | 458 | 'id': -10, |
|
459 | 459 | 'value': query, |
|
460 | 460 | 'value_display': label, |
|
461 | 461 | 'value_icon': '<i class="icon-code"></i>', |
|
462 | 462 | 'type': 'search', |
|
463 | 463 | 'subtype': 'repo', |
|
464 | 464 | 'url': h.route_path('search_repo', |
|
465 | 465 | repo_name=repo_name, |
|
466 | 466 | _query=query_modifier()) |
|
467 | 467 | } |
|
468 | 468 | |
|
469 | 469 | # commits |
|
470 | 470 | def query_modifier(): |
|
471 | 471 | qry = query |
|
472 | 472 | return {'q': qry, 'type': 'commit'} |
|
473 | 473 | |
|
474 | 474 | label = u'Commit search for `{}`'.format(h.escape(query)) |
|
475 | 475 | commit_qry = { |
|
476 | 476 | 'id': -20, |
|
477 | 477 | 'value': query, |
|
478 | 478 | 'value_display': label, |
|
479 | 479 | 'value_icon': '<i class="icon-history"></i>', |
|
480 | 480 | 'type': 'search', |
|
481 | 481 | 'subtype': 'repo', |
|
482 | 482 | 'url': h.route_path('search_repo', |
|
483 | 483 | repo_name=repo_name, |
|
484 | 484 | _query=query_modifier()) |
|
485 | 485 | } |
|
486 | 486 | |
|
487 | 487 | if repo_context in ['commit', 'commits']: |
|
488 | 488 | queries.extend([commit_qry, file_qry]) |
|
489 | 489 | elif repo_context in ['files', 'summary']: |
|
490 | 490 | queries.extend([file_qry, commit_qry]) |
|
491 | 491 | else: |
|
492 | 492 | queries.extend([commit_qry, file_qry]) |
|
493 | 493 | |
|
494 | 494 | elif is_es_6 and repo_group_name: |
|
495 | 495 | # files |
|
496 | 496 | def query_modifier(): |
|
497 | 497 | qry = query |
|
498 | 498 | return {'q': qry, 'type': 'content'} |
|
499 | 499 | |
|
500 | label = u'File search for `{}`'.format(query) | |
|
500 | label = u'File content search for `{}`'.format(query) | |
|
501 | 501 | file_qry = { |
|
502 | 502 | 'id': -30, |
|
503 | 503 | 'value': query, |
|
504 | 504 | 'value_display': label, |
|
505 | 505 | 'value_icon': '<i class="icon-code"></i>', |
|
506 | 506 | 'type': 'search', |
|
507 | 507 | 'subtype': 'repo_group', |
|
508 | 508 | 'url': h.route_path('search_repo_group', |
|
509 | 509 | repo_group_name=repo_group_name, |
|
510 | 510 | _query=query_modifier()) |
|
511 | 511 | } |
|
512 | 512 | |
|
513 | 513 | # commits |
|
514 | 514 | def query_modifier(): |
|
515 | 515 | qry = query |
|
516 | 516 | return {'q': qry, 'type': 'commit'} |
|
517 | 517 | |
|
518 | 518 | label = u'Commit search for `{}`'.format(query) |
|
519 | 519 | commit_qry = { |
|
520 | 520 | 'id': -40, |
|
521 | 521 | 'value': query, |
|
522 | 522 | 'value_display': label, |
|
523 | 523 | 'value_icon': '<i class="icon-history"></i>', |
|
524 | 524 | 'type': 'search', |
|
525 | 525 | 'subtype': 'repo_group', |
|
526 | 526 | 'url': h.route_path('search_repo_group', |
|
527 | 527 | repo_group_name=repo_group_name, |
|
528 | 528 | _query=query_modifier()) |
|
529 | 529 | } |
|
530 | 530 | |
|
531 | 531 | if repo_context in ['commit', 'commits']: |
|
532 | 532 | queries.extend([commit_qry, file_qry]) |
|
533 | 533 | elif repo_context in ['files', 'summary']: |
|
534 | 534 | queries.extend([file_qry, commit_qry]) |
|
535 | 535 | else: |
|
536 | 536 | queries.extend([commit_qry, file_qry]) |
|
537 | 537 | |
|
538 | 538 | # Global, not scoped |
|
539 | 539 | if not queries: |
|
540 | 540 | queries.append( |
|
541 | 541 | { |
|
542 | 542 | 'id': -1, |
|
543 | 543 | 'value': query, |
|
544 | 'value_display': u'File search for: `{}`'.format(query), | |
|
544 | 'value_display': u'File content search for: `{}`'.format(query), | |
|
545 | 545 | 'value_icon': '<i class="icon-code"></i>', |
|
546 | 546 | 'type': 'search', |
|
547 | 547 | 'subtype': 'global', |
|
548 | 548 | 'url': h.route_path('search', |
|
549 | 549 | _query={'q': query, 'type': 'content'}) |
|
550 | 550 | }) |
|
551 | 551 | queries.append( |
|
552 | 552 | { |
|
553 | 553 | 'id': -2, |
|
554 | 554 | 'value': query, |
|
555 | 555 | 'value_display': u'Commit search for: `{}`'.format(query), |
|
556 | 556 | 'value_icon': '<i class="icon-history"></i>', |
|
557 | 557 | 'type': 'search', |
|
558 | 558 | 'subtype': 'global', |
|
559 | 559 | 'url': h.route_path('search', |
|
560 | 560 | _query={'q': query, 'type': 'commit'}) |
|
561 | 561 | }) |
|
562 | 562 | |
|
563 | 563 | return queries |
|
564 | 564 | |
|
565 | 565 | @LoginRequired() |
|
566 | 566 | @view_config( |
|
567 | 567 | route_name='goto_switcher_data', request_method='GET', |
|
568 | 568 | renderer='json_ext', xhr=True) |
|
569 | 569 | def goto_switcher_data(self): |
|
570 | 570 | c = self.load_default_context() |
|
571 | 571 | |
|
572 | 572 | _ = self.request.translate |
|
573 | 573 | |
|
574 | 574 | query = self.request.GET.get('query') |
|
575 | 575 | log.debug('generating main filter data, query %s', query) |
|
576 | 576 | |
|
577 | 577 | res = [] |
|
578 | 578 | if not query: |
|
579 | 579 | return {'suggestions': res} |
|
580 | 580 | |
|
581 | 581 | def no_match(name): |
|
582 | 582 | return { |
|
583 | 583 | 'id': -1, |
|
584 | 584 | 'value': "", |
|
585 | 585 | 'value_display': name, |
|
586 | 586 | 'type': 'text', |
|
587 | 587 | 'url': "" |
|
588 | 588 | } |
|
589 | 589 | searcher = searcher_from_config(self.request.registry.settings) |
|
590 | 590 | has_specialized_search = False |
|
591 | 591 | |
|
592 | 592 | # set repo context |
|
593 | 593 | repo = None |
|
594 | 594 | repo_id = safe_int(self.request.GET.get('search_context[repo_id]')) |
|
595 | 595 | if repo_id: |
|
596 | 596 | repo = Repository.get(repo_id) |
|
597 | 597 | |
|
598 | 598 | # set group context |
|
599 | 599 | repo_group = None |
|
600 | 600 | repo_group_id = safe_int(self.request.GET.get('search_context[repo_group_id]')) |
|
601 | 601 | if repo_group_id: |
|
602 | 602 | repo_group = RepoGroup.get(repo_group_id) |
|
603 | 603 | prefix_match = False |
|
604 | 604 | |
|
605 | 605 | # user: type search |
|
606 | 606 | if not prefix_match: |
|
607 | 607 | users, prefix_match = self._get_user_list(query) |
|
608 | 608 | if users: |
|
609 | 609 | has_specialized_search = True |
|
610 | 610 | for serialized_user in users: |
|
611 | 611 | res.append(serialized_user) |
|
612 | 612 | elif prefix_match: |
|
613 | 613 | has_specialized_search = True |
|
614 | 614 | res.append(no_match('No matching users found')) |
|
615 | 615 | |
|
616 | 616 | # user_group: type search |
|
617 | 617 | if not prefix_match: |
|
618 | 618 | user_groups, prefix_match = self._get_user_groups_list(query) |
|
619 | 619 | if user_groups: |
|
620 | 620 | has_specialized_search = True |
|
621 | 621 | for serialized_user_group in user_groups: |
|
622 | 622 | res.append(serialized_user_group) |
|
623 | 623 | elif prefix_match: |
|
624 | 624 | has_specialized_search = True |
|
625 | 625 | res.append(no_match('No matching user groups found')) |
|
626 | 626 | |
|
627 | 627 | # FTS commit: type search |
|
628 | 628 | if not prefix_match: |
|
629 | 629 | commits, prefix_match = self._get_hash_commit_list( |
|
630 | 630 | c.auth_user, searcher, query, repo, repo_group) |
|
631 | 631 | if commits: |
|
632 | 632 | has_specialized_search = True |
|
633 | 633 | unique_repos = collections.OrderedDict() |
|
634 | 634 | for commit in commits: |
|
635 | 635 | repo_name = commit['repo'] |
|
636 | 636 | unique_repos.setdefault(repo_name, []).append(commit) |
|
637 | 637 | |
|
638 | 638 | for _repo, commits in unique_repos.items(): |
|
639 | 639 | for commit in commits: |
|
640 | 640 | res.append(commit) |
|
641 | 641 | elif prefix_match: |
|
642 | 642 | has_specialized_search = True |
|
643 | 643 | res.append(no_match('No matching commits found')) |
|
644 | 644 | |
|
645 | 645 | # FTS file: type search |
|
646 | 646 | if not prefix_match: |
|
647 | 647 | paths, prefix_match = self._get_path_list( |
|
648 | 648 | c.auth_user, searcher, query, repo, repo_group) |
|
649 | 649 | if paths: |
|
650 | 650 | has_specialized_search = True |
|
651 | 651 | unique_repos = collections.OrderedDict() |
|
652 | 652 | for path in paths: |
|
653 | 653 | repo_name = path['repo'] |
|
654 | 654 | unique_repos.setdefault(repo_name, []).append(path) |
|
655 | 655 | |
|
656 | 656 | for repo, paths in unique_repos.items(): |
|
657 | 657 | for path in paths: |
|
658 | 658 | res.append(path) |
|
659 | 659 | elif prefix_match: |
|
660 | 660 | has_specialized_search = True |
|
661 | 661 | res.append(no_match('No matching files found')) |
|
662 | 662 | |
|
663 | 663 | # main suggestions |
|
664 | 664 | if not has_specialized_search: |
|
665 | 665 | repo_group_name = '' |
|
666 | 666 | if repo_group: |
|
667 | 667 | repo_group_name = repo_group.group_name |
|
668 | 668 | |
|
669 | 669 | for _q in self._get_default_search_queries(self.request.GET, searcher, query): |
|
670 | 670 | res.append(_q) |
|
671 | 671 | |
|
672 | 672 | repo_groups = self._get_repo_group_list(query, repo_group_name=repo_group_name) |
|
673 | 673 | for serialized_repo_group in repo_groups: |
|
674 | 674 | res.append(serialized_repo_group) |
|
675 | 675 | |
|
676 | 676 | repos = self._get_repo_list(query, repo_group_name=repo_group_name) |
|
677 | 677 | for serialized_repo in repos: |
|
678 | 678 | res.append(serialized_repo) |
|
679 | 679 | |
|
680 | 680 | if not repos and not repo_groups: |
|
681 | 681 | res.append(no_match('No matches found')) |
|
682 | 682 | |
|
683 | 683 | return {'suggestions': res} |
|
684 | 684 | |
|
685 | 685 | @LoginRequired() |
|
686 | 686 | @view_config( |
|
687 | 687 | route_name='home', request_method='GET', |
|
688 | 688 | renderer='rhodecode:templates/index.mako') |
|
689 | 689 | def main_page(self): |
|
690 | 690 | c = self.load_default_context() |
|
691 | 691 | c.repo_group = None |
|
692 | 692 | return self._get_template_context(c) |
|
693 | 693 | |
|
694 | 694 | def _main_page_repo_groups_data(self, repo_group_id): |
|
695 | 695 | column_map = { |
|
696 | 696 | 'name': 'group_name_hash', |
|
697 | 697 | 'desc': 'group_description', |
|
698 | 698 | 'last_change': 'updated_on', |
|
699 | 699 | 'owner': 'user_username', |
|
700 | 700 | } |
|
701 | 701 | draw, start, limit = self._extract_chunk(self.request) |
|
702 | 702 | search_q, order_by, order_dir = self._extract_ordering( |
|
703 | 703 | self.request, column_map=column_map) |
|
704 | 704 | return RepoGroupModel().get_repo_groups_data_table( |
|
705 | 705 | draw, start, limit, |
|
706 | 706 | search_q, order_by, order_dir, |
|
707 | 707 | self._rhodecode_user, repo_group_id) |
|
708 | 708 | |
|
709 | 709 | def _main_page_repos_data(self, repo_group_id): |
|
710 | 710 | column_map = { |
|
711 | 711 | 'name': 'repo_name', |
|
712 | 712 | 'desc': 'description', |
|
713 | 713 | 'last_change': 'updated_on', |
|
714 | 714 | 'owner': 'user_username', |
|
715 | 715 | } |
|
716 | 716 | draw, start, limit = self._extract_chunk(self.request) |
|
717 | 717 | search_q, order_by, order_dir = self._extract_ordering( |
|
718 | 718 | self.request, column_map=column_map) |
|
719 | 719 | return RepoModel().get_repos_data_table( |
|
720 | 720 | draw, start, limit, |
|
721 | 721 | search_q, order_by, order_dir, |
|
722 | 722 | self._rhodecode_user, repo_group_id) |
|
723 | 723 | |
|
724 | 724 | @LoginRequired() |
|
725 | 725 | @view_config( |
|
726 | 726 | route_name='main_page_repo_groups_data', |
|
727 | 727 | request_method='GET', renderer='json_ext', xhr=True) |
|
728 | 728 | def main_page_repo_groups_data(self): |
|
729 | 729 | self.load_default_context() |
|
730 | 730 | repo_group_id = safe_int(self.request.GET.get('repo_group_id')) |
|
731 | 731 | |
|
732 | 732 | if repo_group_id: |
|
733 | 733 | group = RepoGroup.get_or_404(repo_group_id) |
|
734 | 734 | _perms = AuthUser.repo_group_read_perms |
|
735 | 735 | if not HasRepoGroupPermissionAny(*_perms)( |
|
736 | 736 | group.group_name, 'user is allowed to list repo group children'): |
|
737 | 737 | raise HTTPNotFound() |
|
738 | 738 | |
|
739 | 739 | return self._main_page_repo_groups_data(repo_group_id) |
|
740 | 740 | |
|
741 | 741 | @LoginRequired() |
|
742 | 742 | @view_config( |
|
743 | 743 | route_name='main_page_repos_data', |
|
744 | 744 | request_method='GET', renderer='json_ext', xhr=True) |
|
745 | 745 | def main_page_repos_data(self): |
|
746 | 746 | self.load_default_context() |
|
747 | 747 | repo_group_id = safe_int(self.request.GET.get('repo_group_id')) |
|
748 | 748 | |
|
749 | 749 | if repo_group_id: |
|
750 | 750 | group = RepoGroup.get_or_404(repo_group_id) |
|
751 | 751 | _perms = AuthUser.repo_group_read_perms |
|
752 | 752 | if not HasRepoGroupPermissionAny(*_perms)( |
|
753 | 753 | group.group_name, 'user is allowed to list repo group children'): |
|
754 | 754 | raise HTTPNotFound() |
|
755 | 755 | |
|
756 | 756 | return self._main_page_repos_data(repo_group_id) |
|
757 | 757 | |
|
758 | 758 | @LoginRequired() |
|
759 | 759 | @HasRepoGroupPermissionAnyDecorator(*AuthUser.repo_group_read_perms) |
|
760 | 760 | @view_config( |
|
761 | 761 | route_name='repo_group_home', request_method='GET', |
|
762 | 762 | renderer='rhodecode:templates/index_repo_group.mako') |
|
763 | 763 | @view_config( |
|
764 | 764 | route_name='repo_group_home_slash', request_method='GET', |
|
765 | 765 | renderer='rhodecode:templates/index_repo_group.mako') |
|
766 | 766 | def repo_group_main_page(self): |
|
767 | 767 | c = self.load_default_context() |
|
768 | 768 | c.repo_group = self.request.db_repo_group |
|
769 | 769 | return self._get_template_context(c) |
|
770 | 770 | |
|
771 | 771 | @LoginRequired() |
|
772 | 772 | @CSRFRequired() |
|
773 | 773 | @view_config( |
|
774 | 774 | route_name='markup_preview', request_method='POST', |
|
775 | 775 | renderer='string', xhr=True) |
|
776 | 776 | def markup_preview(self): |
|
777 | 777 | # Technically a CSRF token is not needed as no state changes with this |
|
778 | 778 | # call. However, as this is a POST is better to have it, so automated |
|
779 | 779 | # tools don't flag it as potential CSRF. |
|
780 | 780 | # Post is required because the payload could be bigger than the maximum |
|
781 | 781 | # allowed by GET. |
|
782 | 782 | |
|
783 | 783 | text = self.request.POST.get('text') |
|
784 | 784 | renderer = self.request.POST.get('renderer') or 'rst' |
|
785 | 785 | if text: |
|
786 | 786 | return h.render(text, renderer=renderer, mentions=True) |
|
787 | 787 | return '' |
|
788 | 788 | |
|
789 | 789 | @LoginRequired() |
|
790 | 790 | @CSRFRequired() |
|
791 | 791 | @view_config( |
|
792 | 792 | route_name='file_preview', request_method='POST', |
|
793 | 793 | renderer='string', xhr=True) |
|
794 | 794 | def file_preview(self): |
|
795 | 795 | # Technically a CSRF token is not needed as no state changes with this |
|
796 | 796 | # call. However, as this is a POST is better to have it, so automated |
|
797 | 797 | # tools don't flag it as potential CSRF. |
|
798 | 798 | # Post is required because the payload could be bigger than the maximum |
|
799 | 799 | # allowed by GET. |
|
800 | 800 | |
|
801 | 801 | text = self.request.POST.get('text') |
|
802 | 802 | file_path = self.request.POST.get('file_path') |
|
803 | 803 | |
|
804 | 804 | renderer = h.renderer_from_filename(file_path) |
|
805 | 805 | |
|
806 | 806 | if renderer: |
|
807 | 807 | return h.render(text, renderer=renderer, mentions=True) |
|
808 | 808 | else: |
|
809 | 809 | self.load_default_context() |
|
810 | 810 | _render = self.request.get_partial_renderer( |
|
811 | 811 | 'rhodecode:templates/files/file_content.mako') |
|
812 | 812 | |
|
813 | 813 | lines = filenode_as_lines_tokens(FileNode(file_path, text)) |
|
814 | 814 | |
|
815 | 815 | return _render('render_lines', lines) |
|
816 | 816 | |
|
817 | 817 | @LoginRequired() |
|
818 | 818 | @CSRFRequired() |
|
819 | 819 | @view_config( |
|
820 | 820 | route_name='store_user_session_value', request_method='POST', |
|
821 | 821 | renderer='string', xhr=True) |
|
822 | 822 | def store_user_session_attr(self): |
|
823 | 823 | key = self.request.POST.get('key') |
|
824 | 824 | val = self.request.POST.get('val') |
|
825 | 825 | |
|
826 | 826 | existing_value = self.request.session.get(key) |
|
827 | 827 | if existing_value != val: |
|
828 | 828 | self.request.session[key] = val |
|
829 | 829 | |
|
830 | 830 | return 'stored:{}:{}'.format(key, val) |
@@ -1,41 +1,45 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2018-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | def includeme(config): |
|
23 | 23 | |
|
24 | 24 | config.add_route( |
|
25 | 25 | name='hovercard_user', |
|
26 | 26 | pattern='/_hovercard/user/{user_id}') |
|
27 | 27 | |
|
28 | 28 | config.add_route( |
|
29 | name='hovercard_username', | |
|
30 | pattern='/_hovercard/username/{username}') | |
|
31 | ||
|
32 | config.add_route( | |
|
29 | 33 | name='hovercard_user_group', |
|
30 | 34 | pattern='/_hovercard/user_group/{user_group_id}') |
|
31 | 35 | |
|
32 | 36 | config.add_route( |
|
33 | 37 | name='hovercard_pull_request', |
|
34 | 38 | pattern='/_hovercard/pull_request/{pull_request_id}') |
|
35 | 39 | |
|
36 | 40 | config.add_route( |
|
37 | 41 | name='hovercard_repo_commit', |
|
38 | 42 | pattern='/_hovercard/commit/{repo_name:.*?[^/]}/{commit_id}', repo_route=True) |
|
39 | 43 | |
|
40 | 44 | # Scan module for configuration decorators. |
|
41 | 45 | config.scan('.views', ignore='.tests') |
@@ -1,110 +1,123 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import re |
|
22 | 22 | import logging |
|
23 | 23 | import collections |
|
24 | 24 | |
|
25 | 25 | from pyramid.httpexceptions import HTTPNotFound |
|
26 | 26 | from pyramid.view import view_config |
|
27 | 27 | |
|
28 | 28 | from rhodecode.apps._base import BaseAppView, RepoAppView |
|
29 | 29 | from rhodecode.lib import helpers as h |
|
30 | 30 | from rhodecode.lib.auth import ( |
|
31 | 31 | LoginRequired, NotAnonymous, HasRepoGroupPermissionAnyDecorator, CSRFRequired, |
|
32 | 32 | HasRepoPermissionAnyDecorator) |
|
33 | 33 | from rhodecode.lib.codeblocks import filenode_as_lines_tokens |
|
34 | 34 | from rhodecode.lib.index import searcher_from_config |
|
35 | 35 | from rhodecode.lib.utils2 import safe_unicode, str2bool, safe_int |
|
36 | 36 | from rhodecode.lib.ext_json import json |
|
37 | 37 | from rhodecode.lib.vcs.exceptions import CommitDoesNotExistError, EmptyRepositoryError |
|
38 | 38 | from rhodecode.lib.vcs.nodes import FileNode |
|
39 | 39 | from rhodecode.model.db import ( |
|
40 | 40 | func, true, or_, case, in_filter_generator, Repository, RepoGroup, User, UserGroup, PullRequest) |
|
41 | 41 | from rhodecode.model.repo import RepoModel |
|
42 | 42 | from rhodecode.model.repo_group import RepoGroupModel |
|
43 | 43 | from rhodecode.model.scm import RepoGroupList, RepoList |
|
44 | 44 | from rhodecode.model.user import UserModel |
|
45 | 45 | from rhodecode.model.user_group import UserGroupModel |
|
46 | 46 | |
|
47 | 47 | log = logging.getLogger(__name__) |
|
48 | 48 | |
|
49 | 49 | |
|
50 | 50 | class HoverCardsView(BaseAppView): |
|
51 | 51 | |
|
52 | 52 | def load_default_context(self): |
|
53 | 53 | c = self._get_local_tmpl_context() |
|
54 | 54 | return c |
|
55 | 55 | |
|
56 | 56 | @LoginRequired() |
|
57 | 57 | @view_config( |
|
58 | 58 | route_name='hovercard_user', request_method='GET', xhr=True, |
|
59 | 59 | renderer='rhodecode:templates/hovercards/hovercard_user.mako') |
|
60 | 60 | def hovercard_user(self): |
|
61 | 61 | c = self.load_default_context() |
|
62 | 62 | user_id = self.request.matchdict['user_id'] |
|
63 | 63 | c.user = User.get_or_404(user_id) |
|
64 | 64 | return self._get_template_context(c) |
|
65 | 65 | |
|
66 | 66 | @LoginRequired() |
|
67 | 67 | @view_config( |
|
68 | route_name='hovercard_username', request_method='GET', xhr=True, | |
|
69 | renderer='rhodecode:templates/hovercards/hovercard_user.mako') | |
|
70 | def hovercard_username(self): | |
|
71 | c = self.load_default_context() | |
|
72 | username = self.request.matchdict['username'] | |
|
73 | c.user = User.get_by_username(username) | |
|
74 | if not c.user: | |
|
75 | raise HTTPNotFound() | |
|
76 | ||
|
77 | return self._get_template_context(c) | |
|
78 | ||
|
79 | @LoginRequired() | |
|
80 | @view_config( | |
|
68 | 81 | route_name='hovercard_user_group', request_method='GET', xhr=True, |
|
69 | 82 | renderer='rhodecode:templates/hovercards/hovercard_user_group.mako') |
|
70 | 83 | def hovercard_user_group(self): |
|
71 | 84 | c = self.load_default_context() |
|
72 | 85 | user_group_id = self.request.matchdict['user_group_id'] |
|
73 | 86 | c.user_group = UserGroup.get_or_404(user_group_id) |
|
74 | 87 | return self._get_template_context(c) |
|
75 | 88 | |
|
76 | 89 | @LoginRequired() |
|
77 | 90 | @view_config( |
|
78 | 91 | route_name='hovercard_pull_request', request_method='GET', xhr=True, |
|
79 | 92 | renderer='rhodecode:templates/hovercards/hovercard_pull_request.mako') |
|
80 | 93 | def hovercard_pull_request(self): |
|
81 | 94 | c = self.load_default_context() |
|
82 | 95 | c.pull_request = PullRequest.get_or_404( |
|
83 | 96 | self.request.matchdict['pull_request_id']) |
|
84 | 97 | perms = ['repository.read', 'repository.write', 'repository.admin'] |
|
85 | 98 | c.can_view_pr = h.HasRepoPermissionAny(*perms)( |
|
86 | 99 | c.pull_request.target_repo.repo_name) |
|
87 | 100 | return self._get_template_context(c) |
|
88 | 101 | |
|
89 | 102 | |
|
90 | 103 | class HoverCardsRepoView(RepoAppView): |
|
91 | 104 | def load_default_context(self): |
|
92 | 105 | c = self._get_local_tmpl_context() |
|
93 | 106 | return c |
|
94 | 107 | |
|
95 | 108 | @LoginRequired() |
|
96 | 109 | @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', 'repository.admin') |
|
97 | 110 | @view_config( |
|
98 | 111 | route_name='hovercard_repo_commit', request_method='GET', xhr=True, |
|
99 | 112 | renderer='rhodecode:templates/hovercards/hovercard_repo_commit.mako') |
|
100 | 113 | def hovercard_repo_commit(self): |
|
101 | 114 | c = self.load_default_context() |
|
102 | 115 | commit_id = self.request.matchdict['commit_id'] |
|
103 | 116 | pre_load = ['author', 'branch', 'date', 'message'] |
|
104 | 117 | try: |
|
105 | 118 | c.commit = self.rhodecode_vcs_repo.get_commit( |
|
106 | 119 | commit_id=commit_id, pre_load=pre_load) |
|
107 | 120 | except (CommitDoesNotExistError, EmptyRepositoryError): |
|
108 | 121 | raise HTTPNotFound() |
|
109 | 122 | |
|
110 | 123 | return self._get_template_context(c) |
@@ -1,578 +1,580 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import urlparse |
|
22 | 22 | |
|
23 | 23 | import mock |
|
24 | 24 | import pytest |
|
25 | 25 | |
|
26 | 26 | from rhodecode.tests import ( |
|
27 | 27 | assert_session_flash, HG_REPO, TEST_USER_ADMIN_LOGIN, |
|
28 | 28 | no_newline_id_generator) |
|
29 | 29 | from rhodecode.tests.fixture import Fixture |
|
30 | 30 | from rhodecode.lib.auth import check_password |
|
31 | 31 | from rhodecode.lib import helpers as h |
|
32 | 32 | from rhodecode.model.auth_token import AuthTokenModel |
|
33 | 33 | from rhodecode.model.db import User, Notification, UserApiKeys |
|
34 | 34 | from rhodecode.model.meta import Session |
|
35 | 35 | |
|
36 | 36 | fixture = Fixture() |
|
37 | 37 | |
|
38 | 38 | whitelist_view = ['RepoCommitsView:repo_commit_raw'] |
|
39 | 39 | |
|
40 | 40 | |
|
41 | 41 | def route_path(name, params=None, **kwargs): |
|
42 | 42 | import urllib |
|
43 | 43 | from rhodecode.apps._base import ADMIN_PREFIX |
|
44 | 44 | |
|
45 | 45 | base_url = { |
|
46 | 46 | 'login': ADMIN_PREFIX + '/login', |
|
47 | 47 | 'logout': ADMIN_PREFIX + '/logout', |
|
48 | 48 | 'register': ADMIN_PREFIX + '/register', |
|
49 | 49 | 'reset_password': |
|
50 | 50 | ADMIN_PREFIX + '/password_reset', |
|
51 | 51 | 'reset_password_confirmation': |
|
52 | 52 | ADMIN_PREFIX + '/password_reset_confirmation', |
|
53 | 53 | |
|
54 | 54 | 'admin_permissions_application': |
|
55 | 55 | ADMIN_PREFIX + '/permissions/application', |
|
56 | 56 | 'admin_permissions_application_update': |
|
57 | 57 | ADMIN_PREFIX + '/permissions/application/update', |
|
58 | 58 | |
|
59 | 59 | 'repo_commit_raw': '/{repo_name}/raw-changeset/{commit_id}' |
|
60 | 60 | |
|
61 | 61 | }[name].format(**kwargs) |
|
62 | 62 | |
|
63 | 63 | if params: |
|
64 | 64 | base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) |
|
65 | 65 | return base_url |
|
66 | 66 | |
|
67 | 67 | |
|
68 | 68 | @pytest.mark.usefixtures('app') |
|
69 | 69 | class TestLoginController(object): |
|
70 | 70 | destroy_users = set() |
|
71 | 71 | |
|
72 | 72 | @classmethod |
|
73 | 73 | def teardown_class(cls): |
|
74 | 74 | fixture.destroy_users(cls.destroy_users) |
|
75 | 75 | |
|
76 | 76 | def teardown_method(self, method): |
|
77 | 77 | for n in Notification.query().all(): |
|
78 | 78 | Session().delete(n) |
|
79 | 79 | |
|
80 | 80 | Session().commit() |
|
81 | 81 | assert Notification.query().all() == [] |
|
82 | 82 | |
|
83 | 83 | def test_index(self): |
|
84 | 84 | response = self.app.get(route_path('login')) |
|
85 | 85 | assert response.status == '200 OK' |
|
86 | 86 | # Test response... |
|
87 | 87 | |
|
88 | 88 | def test_login_admin_ok(self): |
|
89 | 89 | response = self.app.post(route_path('login'), |
|
90 | 90 | {'username': 'test_admin', |
|
91 | 91 | 'password': 'test12'}, status=302) |
|
92 | 92 | response = response.follow() |
|
93 | 93 | session = response.get_session_from_response() |
|
94 | 94 | username = session['rhodecode_user'].get('username') |
|
95 | 95 | assert username == 'test_admin' |
|
96 | 96 | response.mustcontain('logout') |
|
97 | 97 | |
|
98 | 98 | def test_login_regular_ok(self): |
|
99 | 99 | response = self.app.post(route_path('login'), |
|
100 | 100 | {'username': 'test_regular', |
|
101 | 101 | 'password': 'test12'}, status=302) |
|
102 | 102 | |
|
103 | 103 | response = response.follow() |
|
104 | 104 | session = response.get_session_from_response() |
|
105 | 105 | username = session['rhodecode_user'].get('username') |
|
106 | 106 | assert username == 'test_regular' |
|
107 | 107 | response.mustcontain('logout') |
|
108 | 108 | |
|
109 | 109 | def test_login_regular_forbidden_when_super_admin_restriction(self): |
|
110 | 110 | from rhodecode.authentication.plugins.auth_rhodecode import RhodeCodeAuthPlugin |
|
111 | with fixture.auth_restriction(RhodeCodeAuthPlugin.AUTH_RESTRICTION_SUPER_ADMIN): | |
|
111 | with fixture.auth_restriction(self.app._pyramid_registry, | |
|
112 | RhodeCodeAuthPlugin.AUTH_RESTRICTION_SUPER_ADMIN): | |
|
112 | 113 | response = self.app.post(route_path('login'), |
|
113 | 114 | {'username': 'test_regular', |
|
114 | 115 | 'password': 'test12'}) |
|
115 | 116 | |
|
116 | 117 | response.mustcontain('invalid user name') |
|
117 | 118 | response.mustcontain('invalid password') |
|
118 | 119 | |
|
119 | 120 | def test_login_regular_forbidden_when_scope_restriction(self): |
|
120 | 121 | from rhodecode.authentication.plugins.auth_rhodecode import RhodeCodeAuthPlugin |
|
121 | with fixture.scope_restriction(RhodeCodeAuthPlugin.AUTH_RESTRICTION_SCOPE_VCS): | |
|
122 | with fixture.scope_restriction(self.app._pyramid_registry, | |
|
123 | RhodeCodeAuthPlugin.AUTH_RESTRICTION_SCOPE_VCS): | |
|
122 | 124 | response = self.app.post(route_path('login'), |
|
123 | 125 | {'username': 'test_regular', |
|
124 | 126 | 'password': 'test12'}) |
|
125 | 127 | |
|
126 | 128 | response.mustcontain('invalid user name') |
|
127 | 129 | response.mustcontain('invalid password') |
|
128 | 130 | |
|
129 | 131 | def test_login_ok_came_from(self): |
|
130 | 132 | test_came_from = '/_admin/users?branch=stable' |
|
131 | 133 | _url = '{}?came_from={}'.format(route_path('login'), test_came_from) |
|
132 | 134 | response = self.app.post( |
|
133 | 135 | _url, {'username': 'test_admin', 'password': 'test12'}, status=302) |
|
134 | 136 | |
|
135 | 137 | assert 'branch=stable' in response.location |
|
136 | 138 | response = response.follow() |
|
137 | 139 | |
|
138 | 140 | assert response.status == '200 OK' |
|
139 | 141 | response.mustcontain('Users administration') |
|
140 | 142 | |
|
141 | 143 | def test_redirect_to_login_with_get_args(self): |
|
142 | 144 | with fixture.anon_access(False): |
|
143 | 145 | kwargs = {'branch': 'stable'} |
|
144 | 146 | response = self.app.get( |
|
145 | 147 | h.route_path('repo_summary', repo_name=HG_REPO, _query=kwargs), |
|
146 | 148 | status=302) |
|
147 | 149 | |
|
148 | 150 | response_query = urlparse.parse_qsl(response.location) |
|
149 | 151 | assert 'branch=stable' in response_query[0][1] |
|
150 | 152 | |
|
151 | 153 | def test_login_form_with_get_args(self): |
|
152 | 154 | _url = '{}?came_from=/_admin/users,branch=stable'.format(route_path('login')) |
|
153 | 155 | response = self.app.get(_url) |
|
154 | 156 | assert 'branch%3Dstable' in response.form.action |
|
155 | 157 | |
|
156 | 158 | @pytest.mark.parametrize("url_came_from", [ |
|
157 | 159 | 'data:text/html,<script>window.alert("xss")</script>', |
|
158 | 160 | 'mailto:test@rhodecode.org', |
|
159 | 161 | 'file:///etc/passwd', |
|
160 | 162 | 'ftp://some.ftp.server', |
|
161 | 163 | 'http://other.domain', |
|
162 | 164 | '/\r\nX-Forwarded-Host: http://example.org', |
|
163 | 165 | ], ids=no_newline_id_generator) |
|
164 | 166 | def test_login_bad_came_froms(self, url_came_from): |
|
165 | 167 | _url = '{}?came_from={}'.format(route_path('login'), url_came_from) |
|
166 | 168 | response = self.app.post( |
|
167 | 169 | _url, |
|
168 | 170 | {'username': 'test_admin', 'password': 'test12'}) |
|
169 | 171 | assert response.status == '302 Found' |
|
170 | 172 | response = response.follow() |
|
171 | 173 | assert response.status == '200 OK' |
|
172 | 174 | assert response.request.path == '/' |
|
173 | 175 | |
|
174 | 176 | def test_login_short_password(self): |
|
175 | 177 | response = self.app.post(route_path('login'), |
|
176 | 178 | {'username': 'test_admin', |
|
177 | 179 | 'password': 'as'}) |
|
178 | 180 | assert response.status == '200 OK' |
|
179 | 181 | |
|
180 | 182 | response.mustcontain('Enter 3 characters or more') |
|
181 | 183 | |
|
182 | 184 | def test_login_wrong_non_ascii_password(self, user_regular): |
|
183 | 185 | response = self.app.post( |
|
184 | 186 | route_path('login'), |
|
185 | 187 | {'username': user_regular.username, |
|
186 | 188 | 'password': u'invalid-non-asci\xe4'.encode('utf8')}) |
|
187 | 189 | |
|
188 | 190 | response.mustcontain('invalid user name') |
|
189 | 191 | response.mustcontain('invalid password') |
|
190 | 192 | |
|
191 | 193 | def test_login_with_non_ascii_password(self, user_util): |
|
192 | 194 | password = u'valid-non-ascii\xe4' |
|
193 | 195 | user = user_util.create_user(password=password) |
|
194 | 196 | response = self.app.post( |
|
195 | 197 | route_path('login'), |
|
196 | 198 | {'username': user.username, |
|
197 | 199 | 'password': password.encode('utf-8')}) |
|
198 | 200 | assert response.status_code == 302 |
|
199 | 201 | |
|
200 | 202 | def test_login_wrong_username_password(self): |
|
201 | 203 | response = self.app.post(route_path('login'), |
|
202 | 204 | {'username': 'error', |
|
203 | 205 | 'password': 'test12'}) |
|
204 | 206 | |
|
205 | 207 | response.mustcontain('invalid user name') |
|
206 | 208 | response.mustcontain('invalid password') |
|
207 | 209 | |
|
208 | 210 | def test_login_admin_ok_password_migration(self, real_crypto_backend): |
|
209 | 211 | from rhodecode.lib import auth |
|
210 | 212 | |
|
211 | 213 | # create new user, with sha256 password |
|
212 | 214 | temp_user = 'test_admin_sha256' |
|
213 | 215 | user = fixture.create_user(temp_user) |
|
214 | 216 | user.password = auth._RhodeCodeCryptoSha256().hash_create( |
|
215 | 217 | b'test123') |
|
216 | 218 | Session().add(user) |
|
217 | 219 | Session().commit() |
|
218 | 220 | self.destroy_users.add(temp_user) |
|
219 | 221 | response = self.app.post(route_path('login'), |
|
220 | 222 | {'username': temp_user, |
|
221 | 223 | 'password': 'test123'}, status=302) |
|
222 | 224 | |
|
223 | 225 | response = response.follow() |
|
224 | 226 | session = response.get_session_from_response() |
|
225 | 227 | username = session['rhodecode_user'].get('username') |
|
226 | 228 | assert username == temp_user |
|
227 | 229 | response.mustcontain('logout') |
|
228 | 230 | |
|
229 | 231 | # new password should be bcrypted, after log-in and transfer |
|
230 | 232 | user = User.get_by_username(temp_user) |
|
231 | 233 | assert user.password.startswith('$') |
|
232 | 234 | |
|
233 | 235 | # REGISTRATIONS |
|
234 | 236 | def test_register(self): |
|
235 | 237 | response = self.app.get(route_path('register')) |
|
236 | 238 | response.mustcontain('Create an Account') |
|
237 | 239 | |
|
238 | 240 | def test_register_err_same_username(self): |
|
239 | 241 | uname = 'test_admin' |
|
240 | 242 | response = self.app.post( |
|
241 | 243 | route_path('register'), |
|
242 | 244 | { |
|
243 | 245 | 'username': uname, |
|
244 | 246 | 'password': 'test12', |
|
245 | 247 | 'password_confirmation': 'test12', |
|
246 | 248 | 'email': 'goodmail@domain.com', |
|
247 | 249 | 'firstname': 'test', |
|
248 | 250 | 'lastname': 'test' |
|
249 | 251 | } |
|
250 | 252 | ) |
|
251 | 253 | |
|
252 | 254 | assertr = response.assert_response() |
|
253 | 255 | msg = 'Username "%(username)s" already exists' |
|
254 | 256 | msg = msg % {'username': uname} |
|
255 | 257 | assertr.element_contains('#username+.error-message', msg) |
|
256 | 258 | |
|
257 | 259 | def test_register_err_same_email(self): |
|
258 | 260 | response = self.app.post( |
|
259 | 261 | route_path('register'), |
|
260 | 262 | { |
|
261 | 263 | 'username': 'test_admin_0', |
|
262 | 264 | 'password': 'test12', |
|
263 | 265 | 'password_confirmation': 'test12', |
|
264 | 266 | 'email': 'test_admin@mail.com', |
|
265 | 267 | 'firstname': 'test', |
|
266 | 268 | 'lastname': 'test' |
|
267 | 269 | } |
|
268 | 270 | ) |
|
269 | 271 | |
|
270 | 272 | assertr = response.assert_response() |
|
271 | 273 | msg = u'This e-mail address is already taken' |
|
272 | 274 | assertr.element_contains('#email+.error-message', msg) |
|
273 | 275 | |
|
274 | 276 | def test_register_err_same_email_case_sensitive(self): |
|
275 | 277 | response = self.app.post( |
|
276 | 278 | route_path('register'), |
|
277 | 279 | { |
|
278 | 280 | 'username': 'test_admin_1', |
|
279 | 281 | 'password': 'test12', |
|
280 | 282 | 'password_confirmation': 'test12', |
|
281 | 283 | 'email': 'TesT_Admin@mail.COM', |
|
282 | 284 | 'firstname': 'test', |
|
283 | 285 | 'lastname': 'test' |
|
284 | 286 | } |
|
285 | 287 | ) |
|
286 | 288 | assertr = response.assert_response() |
|
287 | 289 | msg = u'This e-mail address is already taken' |
|
288 | 290 | assertr.element_contains('#email+.error-message', msg) |
|
289 | 291 | |
|
290 | 292 | def test_register_err_wrong_data(self): |
|
291 | 293 | response = self.app.post( |
|
292 | 294 | route_path('register'), |
|
293 | 295 | { |
|
294 | 296 | 'username': 'xs', |
|
295 | 297 | 'password': 'test', |
|
296 | 298 | 'password_confirmation': 'test', |
|
297 | 299 | 'email': 'goodmailm', |
|
298 | 300 | 'firstname': 'test', |
|
299 | 301 | 'lastname': 'test' |
|
300 | 302 | } |
|
301 | 303 | ) |
|
302 | 304 | assert response.status == '200 OK' |
|
303 | 305 | response.mustcontain('An email address must contain a single @') |
|
304 | 306 | response.mustcontain('Enter a value 6 characters long or more') |
|
305 | 307 | |
|
306 | 308 | def test_register_err_username(self): |
|
307 | 309 | response = self.app.post( |
|
308 | 310 | route_path('register'), |
|
309 | 311 | { |
|
310 | 312 | 'username': 'error user', |
|
311 | 313 | 'password': 'test12', |
|
312 | 314 | 'password_confirmation': 'test12', |
|
313 | 315 | 'email': 'goodmailm', |
|
314 | 316 | 'firstname': 'test', |
|
315 | 317 | 'lastname': 'test' |
|
316 | 318 | } |
|
317 | 319 | ) |
|
318 | 320 | |
|
319 | 321 | response.mustcontain('An email address must contain a single @') |
|
320 | 322 | response.mustcontain( |
|
321 | 323 | 'Username may only contain ' |
|
322 | 324 | 'alphanumeric characters underscores, ' |
|
323 | 325 | 'periods or dashes and must begin with ' |
|
324 | 326 | 'alphanumeric character') |
|
325 | 327 | |
|
326 | 328 | def test_register_err_case_sensitive(self): |
|
327 | 329 | usr = 'Test_Admin' |
|
328 | 330 | response = self.app.post( |
|
329 | 331 | route_path('register'), |
|
330 | 332 | { |
|
331 | 333 | 'username': usr, |
|
332 | 334 | 'password': 'test12', |
|
333 | 335 | 'password_confirmation': 'test12', |
|
334 | 336 | 'email': 'goodmailm', |
|
335 | 337 | 'firstname': 'test', |
|
336 | 338 | 'lastname': 'test' |
|
337 | 339 | } |
|
338 | 340 | ) |
|
339 | 341 | |
|
340 | 342 | assertr = response.assert_response() |
|
341 | 343 | msg = u'Username "%(username)s" already exists' |
|
342 | 344 | msg = msg % {'username': usr} |
|
343 | 345 | assertr.element_contains('#username+.error-message', msg) |
|
344 | 346 | |
|
345 | 347 | def test_register_special_chars(self): |
|
346 | 348 | response = self.app.post( |
|
347 | 349 | route_path('register'), |
|
348 | 350 | { |
|
349 | 351 | 'username': 'xxxaxn', |
|
350 | 352 | 'password': 'ąćźżąśśśś', |
|
351 | 353 | 'password_confirmation': 'ąćźżąśśśś', |
|
352 | 354 | 'email': 'goodmailm@test.plx', |
|
353 | 355 | 'firstname': 'test', |
|
354 | 356 | 'lastname': 'test' |
|
355 | 357 | } |
|
356 | 358 | ) |
|
357 | 359 | |
|
358 | 360 | msg = u'Invalid characters (non-ascii) in password' |
|
359 | 361 | response.mustcontain(msg) |
|
360 | 362 | |
|
361 | 363 | def test_register_password_mismatch(self): |
|
362 | 364 | response = self.app.post( |
|
363 | 365 | route_path('register'), |
|
364 | 366 | { |
|
365 | 367 | 'username': 'xs', |
|
366 | 368 | 'password': '123qwe', |
|
367 | 369 | 'password_confirmation': 'qwe123', |
|
368 | 370 | 'email': 'goodmailm@test.plxa', |
|
369 | 371 | 'firstname': 'test', |
|
370 | 372 | 'lastname': 'test' |
|
371 | 373 | } |
|
372 | 374 | ) |
|
373 | 375 | msg = u'Passwords do not match' |
|
374 | 376 | response.mustcontain(msg) |
|
375 | 377 | |
|
376 | 378 | def test_register_ok(self): |
|
377 | 379 | username = 'test_regular4' |
|
378 | 380 | password = 'qweqwe' |
|
379 | 381 | email = 'marcin@test.com' |
|
380 | 382 | name = 'testname' |
|
381 | 383 | lastname = 'testlastname' |
|
382 | 384 | |
|
383 | 385 | # this initializes a session |
|
384 | 386 | response = self.app.get(route_path('register')) |
|
385 | 387 | response.mustcontain('Create an Account') |
|
386 | 388 | |
|
387 | 389 | |
|
388 | 390 | response = self.app.post( |
|
389 | 391 | route_path('register'), |
|
390 | 392 | { |
|
391 | 393 | 'username': username, |
|
392 | 394 | 'password': password, |
|
393 | 395 | 'password_confirmation': password, |
|
394 | 396 | 'email': email, |
|
395 | 397 | 'firstname': name, |
|
396 | 398 | 'lastname': lastname, |
|
397 | 399 | 'admin': True |
|
398 | 400 | }, |
|
399 | 401 | status=302 |
|
400 | 402 | ) # This should be overridden |
|
401 | 403 | |
|
402 | 404 | assert_session_flash( |
|
403 | 405 | response, 'You have successfully registered with RhodeCode. You can log-in now.') |
|
404 | 406 | |
|
405 | 407 | ret = Session().query(User).filter( |
|
406 | 408 | User.username == 'test_regular4').one() |
|
407 | 409 | assert ret.username == username |
|
408 | 410 | assert check_password(password, ret.password) |
|
409 | 411 | assert ret.email == email |
|
410 | 412 | assert ret.name == name |
|
411 | 413 | assert ret.lastname == lastname |
|
412 | 414 | assert ret.auth_tokens is not None |
|
413 | 415 | assert not ret.admin |
|
414 | 416 | |
|
415 | 417 | def test_forgot_password_wrong_mail(self): |
|
416 | 418 | bad_email = 'marcin@wrongmail.org' |
|
417 | 419 | # this initializes a session |
|
418 | 420 | self.app.get(route_path('reset_password')) |
|
419 | 421 | |
|
420 | 422 | response = self.app.post( |
|
421 | 423 | route_path('reset_password'), {'email': bad_email, } |
|
422 | 424 | ) |
|
423 | 425 | assert_session_flash(response, |
|
424 | 426 | 'If such email exists, a password reset link was sent to it.') |
|
425 | 427 | |
|
426 | 428 | def test_forgot_password(self, user_util): |
|
427 | 429 | # this initializes a session |
|
428 | 430 | self.app.get(route_path('reset_password')) |
|
429 | 431 | |
|
430 | 432 | user = user_util.create_user() |
|
431 | 433 | user_id = user.user_id |
|
432 | 434 | email = user.email |
|
433 | 435 | |
|
434 | 436 | response = self.app.post(route_path('reset_password'), {'email': email, }) |
|
435 | 437 | |
|
436 | 438 | assert_session_flash(response, |
|
437 | 439 | 'If such email exists, a password reset link was sent to it.') |
|
438 | 440 | |
|
439 | 441 | # BAD KEY |
|
440 | 442 | confirm_url = '{}?key={}'.format(route_path('reset_password_confirmation'), 'badkey') |
|
441 | 443 | response = self.app.get(confirm_url, status=302) |
|
442 | 444 | assert response.location.endswith(route_path('reset_password')) |
|
443 | 445 | assert_session_flash(response, 'Given reset token is invalid') |
|
444 | 446 | |
|
445 | 447 | response.follow() # cleanup flash |
|
446 | 448 | |
|
447 | 449 | # GOOD KEY |
|
448 | 450 | key = UserApiKeys.query()\ |
|
449 | 451 | .filter(UserApiKeys.user_id == user_id)\ |
|
450 | 452 | .filter(UserApiKeys.role == UserApiKeys.ROLE_PASSWORD_RESET)\ |
|
451 | 453 | .first() |
|
452 | 454 | |
|
453 | 455 | assert key |
|
454 | 456 | |
|
455 | 457 | confirm_url = '{}?key={}'.format(route_path('reset_password_confirmation'), key.api_key) |
|
456 | 458 | response = self.app.get(confirm_url) |
|
457 | 459 | assert response.status == '302 Found' |
|
458 | 460 | assert response.location.endswith(route_path('login')) |
|
459 | 461 | |
|
460 | 462 | assert_session_flash( |
|
461 | 463 | response, |
|
462 | 464 | 'Your password reset was successful, ' |
|
463 | 465 | 'a new password has been sent to your email') |
|
464 | 466 | |
|
465 | 467 | response.follow() |
|
466 | 468 | |
|
467 | 469 | def _get_api_whitelist(self, values=None): |
|
468 | 470 | config = {'api_access_controllers_whitelist': values or []} |
|
469 | 471 | return config |
|
470 | 472 | |
|
471 | 473 | @pytest.mark.parametrize("test_name, auth_token", [ |
|
472 | 474 | ('none', None), |
|
473 | 475 | ('empty_string', ''), |
|
474 | 476 | ('fake_number', '123456'), |
|
475 | 477 | ('proper_auth_token', None) |
|
476 | 478 | ]) |
|
477 | 479 | def test_access_not_whitelisted_page_via_auth_token( |
|
478 | 480 | self, test_name, auth_token, user_admin): |
|
479 | 481 | |
|
480 | 482 | whitelist = self._get_api_whitelist([]) |
|
481 | 483 | with mock.patch.dict('rhodecode.CONFIG', whitelist): |
|
482 | 484 | assert [] == whitelist['api_access_controllers_whitelist'] |
|
483 | 485 | if test_name == 'proper_auth_token': |
|
484 | 486 | # use builtin if api_key is None |
|
485 | 487 | auth_token = user_admin.api_key |
|
486 | 488 | |
|
487 | 489 | with fixture.anon_access(False): |
|
488 | 490 | self.app.get( |
|
489 | 491 | route_path('repo_commit_raw', |
|
490 | 492 | repo_name=HG_REPO, commit_id='tip', |
|
491 | 493 | params=dict(api_key=auth_token)), |
|
492 | 494 | status=302) |
|
493 | 495 | |
|
494 | 496 | @pytest.mark.parametrize("test_name, auth_token, code", [ |
|
495 | 497 | ('none', None, 302), |
|
496 | 498 | ('empty_string', '', 302), |
|
497 | 499 | ('fake_number', '123456', 302), |
|
498 | 500 | ('proper_auth_token', None, 200) |
|
499 | 501 | ]) |
|
500 | 502 | def test_access_whitelisted_page_via_auth_token( |
|
501 | 503 | self, test_name, auth_token, code, user_admin): |
|
502 | 504 | |
|
503 | 505 | whitelist = self._get_api_whitelist(whitelist_view) |
|
504 | 506 | |
|
505 | 507 | with mock.patch.dict('rhodecode.CONFIG', whitelist): |
|
506 | 508 | assert whitelist_view == whitelist['api_access_controllers_whitelist'] |
|
507 | 509 | |
|
508 | 510 | if test_name == 'proper_auth_token': |
|
509 | 511 | auth_token = user_admin.api_key |
|
510 | 512 | assert auth_token |
|
511 | 513 | |
|
512 | 514 | with fixture.anon_access(False): |
|
513 | 515 | self.app.get( |
|
514 | 516 | route_path('repo_commit_raw', |
|
515 | 517 | repo_name=HG_REPO, commit_id='tip', |
|
516 | 518 | params=dict(api_key=auth_token)), |
|
517 | 519 | status=code) |
|
518 | 520 | |
|
519 | 521 | @pytest.mark.parametrize("test_name, auth_token, code", [ |
|
520 | 522 | ('proper_auth_token', None, 200), |
|
521 | 523 | ('wrong_auth_token', '123456', 302), |
|
522 | 524 | ]) |
|
523 | 525 | def test_access_whitelisted_page_via_auth_token_bound_to_token( |
|
524 | 526 | self, test_name, auth_token, code, user_admin): |
|
525 | 527 | |
|
526 | 528 | expected_token = auth_token |
|
527 | 529 | if test_name == 'proper_auth_token': |
|
528 | 530 | auth_token = user_admin.api_key |
|
529 | 531 | expected_token = auth_token |
|
530 | 532 | assert auth_token |
|
531 | 533 | |
|
532 | 534 | whitelist = self._get_api_whitelist([ |
|
533 | 535 | 'RepoCommitsView:repo_commit_raw@{}'.format(expected_token)]) |
|
534 | 536 | |
|
535 | 537 | with mock.patch.dict('rhodecode.CONFIG', whitelist): |
|
536 | 538 | |
|
537 | 539 | with fixture.anon_access(False): |
|
538 | 540 | self.app.get( |
|
539 | 541 | route_path('repo_commit_raw', |
|
540 | 542 | repo_name=HG_REPO, commit_id='tip', |
|
541 | 543 | params=dict(api_key=auth_token)), |
|
542 | 544 | status=code) |
|
543 | 545 | |
|
544 | 546 | def test_access_page_via_extra_auth_token(self): |
|
545 | 547 | whitelist = self._get_api_whitelist(whitelist_view) |
|
546 | 548 | with mock.patch.dict('rhodecode.CONFIG', whitelist): |
|
547 | 549 | assert whitelist_view == \ |
|
548 | 550 | whitelist['api_access_controllers_whitelist'] |
|
549 | 551 | |
|
550 | 552 | new_auth_token = AuthTokenModel().create( |
|
551 | 553 | TEST_USER_ADMIN_LOGIN, 'test') |
|
552 | 554 | Session().commit() |
|
553 | 555 | with fixture.anon_access(False): |
|
554 | 556 | self.app.get( |
|
555 | 557 | route_path('repo_commit_raw', |
|
556 | 558 | repo_name=HG_REPO, commit_id='tip', |
|
557 | 559 | params=dict(api_key=new_auth_token.api_key)), |
|
558 | 560 | status=200) |
|
559 | 561 | |
|
560 | 562 | def test_access_page_via_expired_auth_token(self): |
|
561 | 563 | whitelist = self._get_api_whitelist(whitelist_view) |
|
562 | 564 | with mock.patch.dict('rhodecode.CONFIG', whitelist): |
|
563 | 565 | assert whitelist_view == \ |
|
564 | 566 | whitelist['api_access_controllers_whitelist'] |
|
565 | 567 | |
|
566 | 568 | new_auth_token = AuthTokenModel().create( |
|
567 | 569 | TEST_USER_ADMIN_LOGIN, 'test') |
|
568 | 570 | Session().commit() |
|
569 | 571 | # patch the api key and make it expired |
|
570 | 572 | new_auth_token.expires = 0 |
|
571 | 573 | Session().add(new_auth_token) |
|
572 | 574 | Session().commit() |
|
573 | 575 | with fixture.anon_access(False): |
|
574 | 576 | self.app.get( |
|
575 | 577 | route_path('repo_commit_raw', |
|
576 | 578 | repo_name=HG_REPO, commit_id='tip', |
|
577 | 579 | params=dict(api_key=new_auth_token.api_key)), |
|
578 | 580 | status=302) |
@@ -1,800 +1,801 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | import datetime |
|
23 | 23 | import string |
|
24 | 24 | |
|
25 | 25 | import formencode |
|
26 | 26 | import formencode.htmlfill |
|
27 | 27 | import peppercorn |
|
28 | 28 | from pyramid.httpexceptions import HTTPFound |
|
29 | 29 | from pyramid.view import view_config |
|
30 | 30 | |
|
31 | 31 | from rhodecode.apps._base import BaseAppView, DataGridAppView |
|
32 | 32 | from rhodecode import forms |
|
33 | 33 | from rhodecode.lib import helpers as h |
|
34 | 34 | from rhodecode.lib import audit_logger |
|
35 | 35 | from rhodecode.lib.ext_json import json |
|
36 | 36 | from rhodecode.lib.auth import ( |
|
37 | 37 | LoginRequired, NotAnonymous, CSRFRequired, |
|
38 | 38 | HasRepoPermissionAny, HasRepoGroupPermissionAny, AuthUser) |
|
39 | 39 | from rhodecode.lib.channelstream import ( |
|
40 | 40 | channelstream_request, ChannelstreamException) |
|
41 | 41 | from rhodecode.lib.utils2 import safe_int, md5, str2bool |
|
42 | 42 | from rhodecode.model.auth_token import AuthTokenModel |
|
43 | 43 | from rhodecode.model.comment import CommentsModel |
|
44 | 44 | from rhodecode.model.db import ( |
|
45 | 45 | IntegrityError, or_, in_filter_generator, |
|
46 | 46 | Repository, UserEmailMap, UserApiKeys, UserFollowing, |
|
47 | 47 | PullRequest, UserBookmark, RepoGroup) |
|
48 | 48 | from rhodecode.model.meta import Session |
|
49 | 49 | from rhodecode.model.pull_request import PullRequestModel |
|
50 | 50 | from rhodecode.model.user import UserModel |
|
51 | 51 | from rhodecode.model.user_group import UserGroupModel |
|
52 | 52 | from rhodecode.model.validation_schema.schemas import user_schema |
|
53 | 53 | |
|
54 | 54 | log = logging.getLogger(__name__) |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | class MyAccountView(BaseAppView, DataGridAppView): |
|
58 | 58 | ALLOW_SCOPED_TOKENS = False |
|
59 | 59 | """ |
|
60 | 60 | This view has alternative version inside EE, if modified please take a look |
|
61 | 61 | in there as well. |
|
62 | 62 | """ |
|
63 | 63 | |
|
64 | 64 | def load_default_context(self): |
|
65 | 65 | c = self._get_local_tmpl_context() |
|
66 | 66 | c.user = c.auth_user.get_instance() |
|
67 | 67 | c.allow_scoped_tokens = self.ALLOW_SCOPED_TOKENS |
|
68 | 68 | |
|
69 | 69 | return c |
|
70 | 70 | |
|
71 | 71 | @LoginRequired() |
|
72 | 72 | @NotAnonymous() |
|
73 | 73 | @view_config( |
|
74 | 74 | route_name='my_account_profile', request_method='GET', |
|
75 | 75 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
76 | 76 | def my_account_profile(self): |
|
77 | 77 | c = self.load_default_context() |
|
78 | 78 | c.active = 'profile' |
|
79 | c.extern_type = c.user.extern_type | |
|
79 | 80 | return self._get_template_context(c) |
|
80 | 81 | |
|
81 | 82 | @LoginRequired() |
|
82 | 83 | @NotAnonymous() |
|
83 | 84 | @view_config( |
|
84 | 85 | route_name='my_account_password', request_method='GET', |
|
85 | 86 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
86 | 87 | def my_account_password(self): |
|
87 | 88 | c = self.load_default_context() |
|
88 | 89 | c.active = 'password' |
|
89 | 90 | c.extern_type = c.user.extern_type |
|
90 | 91 | |
|
91 | 92 | schema = user_schema.ChangePasswordSchema().bind( |
|
92 | 93 | username=c.user.username) |
|
93 | 94 | |
|
94 | 95 | form = forms.Form( |
|
95 | 96 | schema, |
|
96 | 97 | action=h.route_path('my_account_password_update'), |
|
97 | 98 | buttons=(forms.buttons.save, forms.buttons.reset)) |
|
98 | 99 | |
|
99 | 100 | c.form = form |
|
100 | 101 | return self._get_template_context(c) |
|
101 | 102 | |
|
102 | 103 | @LoginRequired() |
|
103 | 104 | @NotAnonymous() |
|
104 | 105 | @CSRFRequired() |
|
105 | 106 | @view_config( |
|
106 | 107 | route_name='my_account_password_update', request_method='POST', |
|
107 | 108 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
108 | 109 | def my_account_password_update(self): |
|
109 | 110 | _ = self.request.translate |
|
110 | 111 | c = self.load_default_context() |
|
111 | 112 | c.active = 'password' |
|
112 | 113 | c.extern_type = c.user.extern_type |
|
113 | 114 | |
|
114 | 115 | schema = user_schema.ChangePasswordSchema().bind( |
|
115 | 116 | username=c.user.username) |
|
116 | 117 | |
|
117 | 118 | form = forms.Form( |
|
118 | 119 | schema, buttons=(forms.buttons.save, forms.buttons.reset)) |
|
119 | 120 | |
|
120 | 121 | if c.extern_type != 'rhodecode': |
|
121 | 122 | raise HTTPFound(self.request.route_path('my_account_password')) |
|
122 | 123 | |
|
123 | 124 | controls = self.request.POST.items() |
|
124 | 125 | try: |
|
125 | 126 | valid_data = form.validate(controls) |
|
126 | 127 | UserModel().update_user(c.user.user_id, **valid_data) |
|
127 | 128 | c.user.update_userdata(force_password_change=False) |
|
128 | 129 | Session().commit() |
|
129 | 130 | except forms.ValidationFailure as e: |
|
130 | 131 | c.form = e |
|
131 | 132 | return self._get_template_context(c) |
|
132 | 133 | |
|
133 | 134 | except Exception: |
|
134 | 135 | log.exception("Exception updating password") |
|
135 | 136 | h.flash(_('Error occurred during update of user password'), |
|
136 | 137 | category='error') |
|
137 | 138 | else: |
|
138 | 139 | instance = c.auth_user.get_instance() |
|
139 | 140 | self.session.setdefault('rhodecode_user', {}).update( |
|
140 | 141 | {'password': md5(instance.password)}) |
|
141 | 142 | self.session.save() |
|
142 | 143 | h.flash(_("Successfully updated password"), category='success') |
|
143 | 144 | |
|
144 | 145 | raise HTTPFound(self.request.route_path('my_account_password')) |
|
145 | 146 | |
|
146 | 147 | @LoginRequired() |
|
147 | 148 | @NotAnonymous() |
|
148 | 149 | @view_config( |
|
149 | 150 | route_name='my_account_auth_tokens', request_method='GET', |
|
150 | 151 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
151 | 152 | def my_account_auth_tokens(self): |
|
152 | 153 | _ = self.request.translate |
|
153 | 154 | |
|
154 | 155 | c = self.load_default_context() |
|
155 | 156 | c.active = 'auth_tokens' |
|
156 | 157 | c.lifetime_values = AuthTokenModel.get_lifetime_values(translator=_) |
|
157 | 158 | c.role_values = [ |
|
158 | 159 | (x, AuthTokenModel.cls._get_role_name(x)) |
|
159 | 160 | for x in AuthTokenModel.cls.ROLES] |
|
160 | 161 | c.role_options = [(c.role_values, _("Role"))] |
|
161 | 162 | c.user_auth_tokens = AuthTokenModel().get_auth_tokens( |
|
162 | 163 | c.user.user_id, show_expired=True) |
|
163 | 164 | c.role_vcs = AuthTokenModel.cls.ROLE_VCS |
|
164 | 165 | return self._get_template_context(c) |
|
165 | 166 | |
|
166 | 167 | def maybe_attach_token_scope(self, token): |
|
167 | 168 | # implemented in EE edition |
|
168 | 169 | pass |
|
169 | 170 | |
|
170 | 171 | @LoginRequired() |
|
171 | 172 | @NotAnonymous() |
|
172 | 173 | @CSRFRequired() |
|
173 | 174 | @view_config( |
|
174 | 175 | route_name='my_account_auth_tokens_add', request_method='POST',) |
|
175 | 176 | def my_account_auth_tokens_add(self): |
|
176 | 177 | _ = self.request.translate |
|
177 | 178 | c = self.load_default_context() |
|
178 | 179 | |
|
179 | 180 | lifetime = safe_int(self.request.POST.get('lifetime'), -1) |
|
180 | 181 | description = self.request.POST.get('description') |
|
181 | 182 | role = self.request.POST.get('role') |
|
182 | 183 | |
|
183 | 184 | token = UserModel().add_auth_token( |
|
184 | 185 | user=c.user.user_id, |
|
185 | 186 | lifetime_minutes=lifetime, role=role, description=description, |
|
186 | 187 | scope_callback=self.maybe_attach_token_scope) |
|
187 | 188 | token_data = token.get_api_data() |
|
188 | 189 | |
|
189 | 190 | audit_logger.store_web( |
|
190 | 191 | 'user.edit.token.add', action_data={ |
|
191 | 192 | 'data': {'token': token_data, 'user': 'self'}}, |
|
192 | 193 | user=self._rhodecode_user, ) |
|
193 | 194 | Session().commit() |
|
194 | 195 | |
|
195 | 196 | h.flash(_("Auth token successfully created"), category='success') |
|
196 | 197 | return HTTPFound(h.route_path('my_account_auth_tokens')) |
|
197 | 198 | |
|
198 | 199 | @LoginRequired() |
|
199 | 200 | @NotAnonymous() |
|
200 | 201 | @CSRFRequired() |
|
201 | 202 | @view_config( |
|
202 | 203 | route_name='my_account_auth_tokens_delete', request_method='POST') |
|
203 | 204 | def my_account_auth_tokens_delete(self): |
|
204 | 205 | _ = self.request.translate |
|
205 | 206 | c = self.load_default_context() |
|
206 | 207 | |
|
207 | 208 | del_auth_token = self.request.POST.get('del_auth_token') |
|
208 | 209 | |
|
209 | 210 | if del_auth_token: |
|
210 | 211 | token = UserApiKeys.get_or_404(del_auth_token) |
|
211 | 212 | token_data = token.get_api_data() |
|
212 | 213 | |
|
213 | 214 | AuthTokenModel().delete(del_auth_token, c.user.user_id) |
|
214 | 215 | audit_logger.store_web( |
|
215 | 216 | 'user.edit.token.delete', action_data={ |
|
216 | 217 | 'data': {'token': token_data, 'user': 'self'}}, |
|
217 | 218 | user=self._rhodecode_user,) |
|
218 | 219 | Session().commit() |
|
219 | 220 | h.flash(_("Auth token successfully deleted"), category='success') |
|
220 | 221 | |
|
221 | 222 | return HTTPFound(h.route_path('my_account_auth_tokens')) |
|
222 | 223 | |
|
223 | 224 | @LoginRequired() |
|
224 | 225 | @NotAnonymous() |
|
225 | 226 | @view_config( |
|
226 | 227 | route_name='my_account_emails', request_method='GET', |
|
227 | 228 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
228 | 229 | def my_account_emails(self): |
|
229 | 230 | _ = self.request.translate |
|
230 | 231 | |
|
231 | 232 | c = self.load_default_context() |
|
232 | 233 | c.active = 'emails' |
|
233 | 234 | |
|
234 | 235 | c.user_email_map = UserEmailMap.query()\ |
|
235 | 236 | .filter(UserEmailMap.user == c.user).all() |
|
236 | 237 | |
|
237 | 238 | schema = user_schema.AddEmailSchema().bind( |
|
238 | 239 | username=c.user.username, user_emails=c.user.emails) |
|
239 | 240 | |
|
240 | 241 | form = forms.RcForm(schema, |
|
241 | 242 | action=h.route_path('my_account_emails_add'), |
|
242 | 243 | buttons=(forms.buttons.save, forms.buttons.reset)) |
|
243 | 244 | |
|
244 | 245 | c.form = form |
|
245 | 246 | return self._get_template_context(c) |
|
246 | 247 | |
|
247 | 248 | @LoginRequired() |
|
248 | 249 | @NotAnonymous() |
|
249 | 250 | @CSRFRequired() |
|
250 | 251 | @view_config( |
|
251 | 252 | route_name='my_account_emails_add', request_method='POST', |
|
252 | 253 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
253 | 254 | def my_account_emails_add(self): |
|
254 | 255 | _ = self.request.translate |
|
255 | 256 | c = self.load_default_context() |
|
256 | 257 | c.active = 'emails' |
|
257 | 258 | |
|
258 | 259 | schema = user_schema.AddEmailSchema().bind( |
|
259 | 260 | username=c.user.username, user_emails=c.user.emails) |
|
260 | 261 | |
|
261 | 262 | form = forms.RcForm( |
|
262 | 263 | schema, action=h.route_path('my_account_emails_add'), |
|
263 | 264 | buttons=(forms.buttons.save, forms.buttons.reset)) |
|
264 | 265 | |
|
265 | 266 | controls = self.request.POST.items() |
|
266 | 267 | try: |
|
267 | 268 | valid_data = form.validate(controls) |
|
268 | 269 | UserModel().add_extra_email(c.user.user_id, valid_data['email']) |
|
269 | 270 | audit_logger.store_web( |
|
270 | 271 | 'user.edit.email.add', action_data={ |
|
271 | 272 | 'data': {'email': valid_data['email'], 'user': 'self'}}, |
|
272 | 273 | user=self._rhodecode_user,) |
|
273 | 274 | Session().commit() |
|
274 | 275 | except formencode.Invalid as error: |
|
275 | 276 | h.flash(h.escape(error.error_dict['email']), category='error') |
|
276 | 277 | except forms.ValidationFailure as e: |
|
277 | 278 | c.user_email_map = UserEmailMap.query() \ |
|
278 | 279 | .filter(UserEmailMap.user == c.user).all() |
|
279 | 280 | c.form = e |
|
280 | 281 | return self._get_template_context(c) |
|
281 | 282 | except Exception: |
|
282 | 283 | log.exception("Exception adding email") |
|
283 | 284 | h.flash(_('Error occurred during adding email'), |
|
284 | 285 | category='error') |
|
285 | 286 | else: |
|
286 | 287 | h.flash(_("Successfully added email"), category='success') |
|
287 | 288 | |
|
288 | 289 | raise HTTPFound(self.request.route_path('my_account_emails')) |
|
289 | 290 | |
|
290 | 291 | @LoginRequired() |
|
291 | 292 | @NotAnonymous() |
|
292 | 293 | @CSRFRequired() |
|
293 | 294 | @view_config( |
|
294 | 295 | route_name='my_account_emails_delete', request_method='POST') |
|
295 | 296 | def my_account_emails_delete(self): |
|
296 | 297 | _ = self.request.translate |
|
297 | 298 | c = self.load_default_context() |
|
298 | 299 | |
|
299 | 300 | del_email_id = self.request.POST.get('del_email_id') |
|
300 | 301 | if del_email_id: |
|
301 | 302 | email = UserEmailMap.get_or_404(del_email_id).email |
|
302 | 303 | UserModel().delete_extra_email(c.user.user_id, del_email_id) |
|
303 | 304 | audit_logger.store_web( |
|
304 | 305 | 'user.edit.email.delete', action_data={ |
|
305 | 306 | 'data': {'email': email, 'user': 'self'}}, |
|
306 | 307 | user=self._rhodecode_user,) |
|
307 | 308 | Session().commit() |
|
308 | 309 | h.flash(_("Email successfully deleted"), |
|
309 | 310 | category='success') |
|
310 | 311 | return HTTPFound(h.route_path('my_account_emails')) |
|
311 | 312 | |
|
312 | 313 | @LoginRequired() |
|
313 | 314 | @NotAnonymous() |
|
314 | 315 | @CSRFRequired() |
|
315 | 316 | @view_config( |
|
316 | 317 | route_name='my_account_notifications_test_channelstream', |
|
317 | 318 | request_method='POST', renderer='json_ext') |
|
318 | 319 | def my_account_notifications_test_channelstream(self): |
|
319 | 320 | message = 'Test message sent via Channelstream by user: {}, on {}'.format( |
|
320 | 321 | self._rhodecode_user.username, datetime.datetime.now()) |
|
321 | 322 | payload = { |
|
322 | 323 | # 'channel': 'broadcast', |
|
323 | 324 | 'type': 'message', |
|
324 | 325 | 'timestamp': datetime.datetime.utcnow(), |
|
325 | 326 | 'user': 'system', |
|
326 | 327 | 'pm_users': [self._rhodecode_user.username], |
|
327 | 328 | 'message': { |
|
328 | 329 | 'message': message, |
|
329 | 330 | 'level': 'info', |
|
330 | 331 | 'topic': '/notifications' |
|
331 | 332 | } |
|
332 | 333 | } |
|
333 | 334 | |
|
334 | 335 | registry = self.request.registry |
|
335 | 336 | rhodecode_plugins = getattr(registry, 'rhodecode_plugins', {}) |
|
336 | 337 | channelstream_config = rhodecode_plugins.get('channelstream', {}) |
|
337 | 338 | |
|
338 | 339 | try: |
|
339 | 340 | channelstream_request(channelstream_config, [payload], '/message') |
|
340 | 341 | except ChannelstreamException as e: |
|
341 | 342 | log.exception('Failed to send channelstream data') |
|
342 | 343 | return {"response": 'ERROR: {}'.format(e.__class__.__name__)} |
|
343 | 344 | return {"response": 'Channelstream data sent. ' |
|
344 | 345 | 'You should see a new live message now.'} |
|
345 | 346 | |
|
346 | 347 | def _load_my_repos_data(self, watched=False): |
|
347 | 348 | |
|
348 | 349 | allowed_ids = [-1] + self._rhodecode_user.repo_acl_ids_from_stack(AuthUser.repo_read_perms) |
|
349 | 350 | |
|
350 | 351 | if watched: |
|
351 | 352 | # repos user watch |
|
352 | 353 | repo_list = Session().query( |
|
353 | 354 | Repository |
|
354 | 355 | ) \ |
|
355 | 356 | .join( |
|
356 | 357 | (UserFollowing, UserFollowing.follows_repo_id == Repository.repo_id) |
|
357 | 358 | ) \ |
|
358 | 359 | .filter( |
|
359 | 360 | UserFollowing.user_id == self._rhodecode_user.user_id |
|
360 | 361 | ) \ |
|
361 | 362 | .filter(or_( |
|
362 | 363 | # generate multiple IN to fix limitation problems |
|
363 | 364 | *in_filter_generator(Repository.repo_id, allowed_ids)) |
|
364 | 365 | ) \ |
|
365 | 366 | .order_by(Repository.repo_name) \ |
|
366 | 367 | .all() |
|
367 | 368 | |
|
368 | 369 | else: |
|
369 | 370 | # repos user is owner of |
|
370 | 371 | repo_list = Session().query( |
|
371 | 372 | Repository |
|
372 | 373 | ) \ |
|
373 | 374 | .filter( |
|
374 | 375 | Repository.user_id == self._rhodecode_user.user_id |
|
375 | 376 | ) \ |
|
376 | 377 | .filter(or_( |
|
377 | 378 | # generate multiple IN to fix limitation problems |
|
378 | 379 | *in_filter_generator(Repository.repo_id, allowed_ids)) |
|
379 | 380 | ) \ |
|
380 | 381 | .order_by(Repository.repo_name) \ |
|
381 | 382 | .all() |
|
382 | 383 | |
|
383 | 384 | _render = self.request.get_partial_renderer( |
|
384 | 385 | 'rhodecode:templates/data_table/_dt_elements.mako') |
|
385 | 386 | |
|
386 | 387 | def repo_lnk(name, rtype, rstate, private, archived, fork_of): |
|
387 | 388 | return _render('repo_name', name, rtype, rstate, private, archived, fork_of, |
|
388 | 389 | short_name=False, admin=False) |
|
389 | 390 | |
|
390 | 391 | repos_data = [] |
|
391 | 392 | for repo in repo_list: |
|
392 | 393 | row = { |
|
393 | 394 | "name": repo_lnk(repo.repo_name, repo.repo_type, repo.repo_state, |
|
394 | 395 | repo.private, repo.archived, repo.fork), |
|
395 | 396 | "name_raw": repo.repo_name.lower(), |
|
396 | 397 | } |
|
397 | 398 | |
|
398 | 399 | repos_data.append(row) |
|
399 | 400 | |
|
400 | 401 | # json used to render the grid |
|
401 | 402 | return json.dumps(repos_data) |
|
402 | 403 | |
|
403 | 404 | @LoginRequired() |
|
404 | 405 | @NotAnonymous() |
|
405 | 406 | @view_config( |
|
406 | 407 | route_name='my_account_repos', request_method='GET', |
|
407 | 408 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
408 | 409 | def my_account_repos(self): |
|
409 | 410 | c = self.load_default_context() |
|
410 | 411 | c.active = 'repos' |
|
411 | 412 | |
|
412 | 413 | # json used to render the grid |
|
413 | 414 | c.data = self._load_my_repos_data() |
|
414 | 415 | return self._get_template_context(c) |
|
415 | 416 | |
|
416 | 417 | @LoginRequired() |
|
417 | 418 | @NotAnonymous() |
|
418 | 419 | @view_config( |
|
419 | 420 | route_name='my_account_watched', request_method='GET', |
|
420 | 421 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
421 | 422 | def my_account_watched(self): |
|
422 | 423 | c = self.load_default_context() |
|
423 | 424 | c.active = 'watched' |
|
424 | 425 | |
|
425 | 426 | # json used to render the grid |
|
426 | 427 | c.data = self._load_my_repos_data(watched=True) |
|
427 | 428 | return self._get_template_context(c) |
|
428 | 429 | |
|
429 | 430 | @LoginRequired() |
|
430 | 431 | @NotAnonymous() |
|
431 | 432 | @view_config( |
|
432 | 433 | route_name='my_account_bookmarks', request_method='GET', |
|
433 | 434 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
434 | 435 | def my_account_bookmarks(self): |
|
435 | 436 | c = self.load_default_context() |
|
436 | 437 | c.active = 'bookmarks' |
|
437 | 438 | c.bookmark_items = UserBookmark.get_bookmarks_for_user( |
|
438 | 439 | self._rhodecode_db_user.user_id, cache=False) |
|
439 | 440 | return self._get_template_context(c) |
|
440 | 441 | |
|
441 | 442 | def _process_bookmark_entry(self, entry, user_id): |
|
442 | 443 | position = safe_int(entry.get('position')) |
|
443 | 444 | cur_position = safe_int(entry.get('cur_position')) |
|
444 | 445 | if position is None: |
|
445 | 446 | return |
|
446 | 447 | |
|
447 | 448 | # check if this is an existing entry |
|
448 | 449 | is_new = False |
|
449 | 450 | db_entry = UserBookmark().get_by_position_for_user(cur_position, user_id) |
|
450 | 451 | |
|
451 | 452 | if db_entry and str2bool(entry.get('remove')): |
|
452 | 453 | log.debug('Marked bookmark %s for deletion', db_entry) |
|
453 | 454 | Session().delete(db_entry) |
|
454 | 455 | return |
|
455 | 456 | |
|
456 | 457 | if not db_entry: |
|
457 | 458 | # new |
|
458 | 459 | db_entry = UserBookmark() |
|
459 | 460 | is_new = True |
|
460 | 461 | |
|
461 | 462 | should_save = False |
|
462 | 463 | default_redirect_url = '' |
|
463 | 464 | |
|
464 | 465 | # save repo |
|
465 | 466 | if entry.get('bookmark_repo') and safe_int(entry.get('bookmark_repo')): |
|
466 | 467 | repo = Repository.get(entry['bookmark_repo']) |
|
467 | 468 | perm_check = HasRepoPermissionAny( |
|
468 | 469 | 'repository.read', 'repository.write', 'repository.admin') |
|
469 | 470 | if repo and perm_check(repo_name=repo.repo_name): |
|
470 | 471 | db_entry.repository = repo |
|
471 | 472 | should_save = True |
|
472 | 473 | default_redirect_url = '${repo_url}' |
|
473 | 474 | # save repo group |
|
474 | 475 | elif entry.get('bookmark_repo_group') and safe_int(entry.get('bookmark_repo_group')): |
|
475 | 476 | repo_group = RepoGroup.get(entry['bookmark_repo_group']) |
|
476 | 477 | perm_check = HasRepoGroupPermissionAny( |
|
477 | 478 | 'group.read', 'group.write', 'group.admin') |
|
478 | 479 | |
|
479 | 480 | if repo_group and perm_check(group_name=repo_group.group_name): |
|
480 | 481 | db_entry.repository_group = repo_group |
|
481 | 482 | should_save = True |
|
482 | 483 | default_redirect_url = '${repo_group_url}' |
|
483 | 484 | # save generic info |
|
484 | 485 | elif entry.get('title') and entry.get('redirect_url'): |
|
485 | 486 | should_save = True |
|
486 | 487 | |
|
487 | 488 | if should_save: |
|
488 | 489 | # mark user and position |
|
489 | 490 | db_entry.user_id = user_id |
|
490 | 491 | db_entry.position = position |
|
491 | 492 | db_entry.title = entry.get('title') |
|
492 | 493 | db_entry.redirect_url = entry.get('redirect_url') or default_redirect_url |
|
493 | 494 | log.debug('Saving bookmark %s, new:%s', db_entry, is_new) |
|
494 | 495 | |
|
495 | 496 | Session().add(db_entry) |
|
496 | 497 | |
|
497 | 498 | @LoginRequired() |
|
498 | 499 | @NotAnonymous() |
|
499 | 500 | @CSRFRequired() |
|
500 | 501 | @view_config( |
|
501 | 502 | route_name='my_account_bookmarks_update', request_method='POST') |
|
502 | 503 | def my_account_bookmarks_update(self): |
|
503 | 504 | _ = self.request.translate |
|
504 | 505 | c = self.load_default_context() |
|
505 | 506 | c.active = 'bookmarks' |
|
506 | 507 | |
|
507 | 508 | controls = peppercorn.parse(self.request.POST.items()) |
|
508 | 509 | user_id = c.user.user_id |
|
509 | 510 | |
|
510 | 511 | # validate positions |
|
511 | 512 | positions = {} |
|
512 | 513 | for entry in controls.get('bookmarks', []): |
|
513 | 514 | position = safe_int(entry['position']) |
|
514 | 515 | if position is None: |
|
515 | 516 | continue |
|
516 | 517 | |
|
517 | 518 | if position in positions: |
|
518 | 519 | h.flash(_("Position {} is defined twice. " |
|
519 | 520 | "Please correct this error.").format(position), category='error') |
|
520 | 521 | return HTTPFound(h.route_path('my_account_bookmarks')) |
|
521 | 522 | |
|
522 | 523 | entry['position'] = position |
|
523 | 524 | entry['cur_position'] = safe_int(entry.get('cur_position')) |
|
524 | 525 | positions[position] = entry |
|
525 | 526 | |
|
526 | 527 | try: |
|
527 | 528 | for entry in positions.values(): |
|
528 | 529 | self._process_bookmark_entry(entry, user_id) |
|
529 | 530 | |
|
530 | 531 | Session().commit() |
|
531 | 532 | h.flash(_("Update Bookmarks"), category='success') |
|
532 | 533 | except IntegrityError: |
|
533 | 534 | h.flash(_("Failed to update bookmarks. " |
|
534 | 535 | "Make sure an unique position is used."), category='error') |
|
535 | 536 | |
|
536 | 537 | return HTTPFound(h.route_path('my_account_bookmarks')) |
|
537 | 538 | |
|
538 | 539 | @LoginRequired() |
|
539 | 540 | @NotAnonymous() |
|
540 | 541 | @view_config( |
|
541 | 542 | route_name='my_account_goto_bookmark', request_method='GET', |
|
542 | 543 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
543 | 544 | def my_account_goto_bookmark(self): |
|
544 | 545 | |
|
545 | 546 | bookmark_id = self.request.matchdict['bookmark_id'] |
|
546 | 547 | user_bookmark = UserBookmark().query()\ |
|
547 | 548 | .filter(UserBookmark.user_id == self.request.user.user_id) \ |
|
548 | 549 | .filter(UserBookmark.position == bookmark_id).scalar() |
|
549 | 550 | |
|
550 | 551 | redirect_url = h.route_path('my_account_bookmarks') |
|
551 | 552 | if not user_bookmark: |
|
552 | 553 | raise HTTPFound(redirect_url) |
|
553 | 554 | |
|
554 | 555 | # repository set |
|
555 | 556 | if user_bookmark.repository: |
|
556 | 557 | repo_name = user_bookmark.repository.repo_name |
|
557 | 558 | base_redirect_url = h.route_path( |
|
558 | 559 | 'repo_summary', repo_name=repo_name) |
|
559 | 560 | if user_bookmark.redirect_url and \ |
|
560 | 561 | '${repo_url}' in user_bookmark.redirect_url: |
|
561 | 562 | redirect_url = string.Template(user_bookmark.redirect_url)\ |
|
562 | 563 | .safe_substitute({'repo_url': base_redirect_url}) |
|
563 | 564 | else: |
|
564 | 565 | redirect_url = base_redirect_url |
|
565 | 566 | # repository group set |
|
566 | 567 | elif user_bookmark.repository_group: |
|
567 | 568 | repo_group_name = user_bookmark.repository_group.group_name |
|
568 | 569 | base_redirect_url = h.route_path( |
|
569 | 570 | 'repo_group_home', repo_group_name=repo_group_name) |
|
570 | 571 | if user_bookmark.redirect_url and \ |
|
571 | 572 | '${repo_group_url}' in user_bookmark.redirect_url: |
|
572 | 573 | redirect_url = string.Template(user_bookmark.redirect_url)\ |
|
573 | 574 | .safe_substitute({'repo_group_url': base_redirect_url}) |
|
574 | 575 | else: |
|
575 | 576 | redirect_url = base_redirect_url |
|
576 | 577 | # custom URL set |
|
577 | 578 | elif user_bookmark.redirect_url: |
|
578 | 579 | server_url = h.route_url('home').rstrip('/') |
|
579 | 580 | redirect_url = string.Template(user_bookmark.redirect_url) \ |
|
580 | 581 | .safe_substitute({'server_url': server_url}) |
|
581 | 582 | |
|
582 | 583 | log.debug('Redirecting bookmark %s to %s', user_bookmark, redirect_url) |
|
583 | 584 | raise HTTPFound(redirect_url) |
|
584 | 585 | |
|
585 | 586 | @LoginRequired() |
|
586 | 587 | @NotAnonymous() |
|
587 | 588 | @view_config( |
|
588 | 589 | route_name='my_account_perms', request_method='GET', |
|
589 | 590 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
590 | 591 | def my_account_perms(self): |
|
591 | 592 | c = self.load_default_context() |
|
592 | 593 | c.active = 'perms' |
|
593 | 594 | |
|
594 | 595 | c.perm_user = c.auth_user |
|
595 | 596 | return self._get_template_context(c) |
|
596 | 597 | |
|
597 | 598 | @LoginRequired() |
|
598 | 599 | @NotAnonymous() |
|
599 | 600 | @view_config( |
|
600 | 601 | route_name='my_account_notifications', request_method='GET', |
|
601 | 602 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
602 | 603 | def my_notifications(self): |
|
603 | 604 | c = self.load_default_context() |
|
604 | 605 | c.active = 'notifications' |
|
605 | 606 | |
|
606 | 607 | return self._get_template_context(c) |
|
607 | 608 | |
|
608 | 609 | @LoginRequired() |
|
609 | 610 | @NotAnonymous() |
|
610 | 611 | @CSRFRequired() |
|
611 | 612 | @view_config( |
|
612 | 613 | route_name='my_account_notifications_toggle_visibility', |
|
613 | 614 | request_method='POST', renderer='json_ext') |
|
614 | 615 | def my_notifications_toggle_visibility(self): |
|
615 | 616 | user = self._rhodecode_db_user |
|
616 | 617 | new_status = not user.user_data.get('notification_status', True) |
|
617 | 618 | user.update_userdata(notification_status=new_status) |
|
618 | 619 | Session().commit() |
|
619 | 620 | return user.user_data['notification_status'] |
|
620 | 621 | |
|
621 | 622 | @LoginRequired() |
|
622 | 623 | @NotAnonymous() |
|
623 | 624 | @view_config( |
|
624 | 625 | route_name='my_account_edit', |
|
625 | 626 | request_method='GET', |
|
626 | 627 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
627 | 628 | def my_account_edit(self): |
|
628 | 629 | c = self.load_default_context() |
|
629 | 630 | c.active = 'profile_edit' |
|
630 | 631 | c.extern_type = c.user.extern_type |
|
631 | 632 | c.extern_name = c.user.extern_name |
|
632 | 633 | |
|
633 | 634 | schema = user_schema.UserProfileSchema().bind( |
|
634 | 635 | username=c.user.username, user_emails=c.user.emails) |
|
635 | 636 | appstruct = { |
|
636 | 637 | 'username': c.user.username, |
|
637 | 638 | 'email': c.user.email, |
|
638 | 639 | 'firstname': c.user.firstname, |
|
639 | 640 | 'lastname': c.user.lastname, |
|
640 | 641 | 'description': c.user.description, |
|
641 | 642 | } |
|
642 | 643 | c.form = forms.RcForm( |
|
643 | 644 | schema, appstruct=appstruct, |
|
644 | 645 | action=h.route_path('my_account_update'), |
|
645 | 646 | buttons=(forms.buttons.save, forms.buttons.reset)) |
|
646 | 647 | |
|
647 | 648 | return self._get_template_context(c) |
|
648 | 649 | |
|
649 | 650 | @LoginRequired() |
|
650 | 651 | @NotAnonymous() |
|
651 | 652 | @CSRFRequired() |
|
652 | 653 | @view_config( |
|
653 | 654 | route_name='my_account_update', |
|
654 | 655 | request_method='POST', |
|
655 | 656 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
656 | 657 | def my_account_update(self): |
|
657 | 658 | _ = self.request.translate |
|
658 | 659 | c = self.load_default_context() |
|
659 | 660 | c.active = 'profile_edit' |
|
660 | 661 | c.perm_user = c.auth_user |
|
661 | 662 | c.extern_type = c.user.extern_type |
|
662 | 663 | c.extern_name = c.user.extern_name |
|
663 | 664 | |
|
664 | 665 | schema = user_schema.UserProfileSchema().bind( |
|
665 | 666 | username=c.user.username, user_emails=c.user.emails) |
|
666 | 667 | form = forms.RcForm( |
|
667 | 668 | schema, buttons=(forms.buttons.save, forms.buttons.reset)) |
|
668 | 669 | |
|
669 | 670 | controls = self.request.POST.items() |
|
670 | 671 | try: |
|
671 | 672 | valid_data = form.validate(controls) |
|
672 | 673 | skip_attrs = ['admin', 'active', 'extern_type', 'extern_name', |
|
673 | 674 | 'new_password', 'password_confirmation'] |
|
674 | 675 | if c.extern_type != "rhodecode": |
|
675 | 676 | # forbid updating username for external accounts |
|
676 | 677 | skip_attrs.append('username') |
|
677 | 678 | old_email = c.user.email |
|
678 | 679 | UserModel().update_user( |
|
679 | 680 | self._rhodecode_user.user_id, skip_attrs=skip_attrs, |
|
680 | 681 | **valid_data) |
|
681 | 682 | if old_email != valid_data['email']: |
|
682 | 683 | old = UserEmailMap.query() \ |
|
683 | 684 | .filter(UserEmailMap.user == c.user).filter(UserEmailMap.email == valid_data['email']).first() |
|
684 | 685 | old.email = old_email |
|
685 | 686 | h.flash(_('Your account was updated successfully'), category='success') |
|
686 | 687 | Session().commit() |
|
687 | 688 | except forms.ValidationFailure as e: |
|
688 | 689 | c.form = e |
|
689 | 690 | return self._get_template_context(c) |
|
690 | 691 | except Exception: |
|
691 | 692 | log.exception("Exception updating user") |
|
692 | 693 | h.flash(_('Error occurred during update of user'), |
|
693 | 694 | category='error') |
|
694 | 695 | raise HTTPFound(h.route_path('my_account_profile')) |
|
695 | 696 | |
|
696 | 697 | def _get_pull_requests_list(self, statuses): |
|
697 | 698 | draw, start, limit = self._extract_chunk(self.request) |
|
698 | 699 | search_q, order_by, order_dir = self._extract_ordering(self.request) |
|
699 | 700 | _render = self.request.get_partial_renderer( |
|
700 | 701 | 'rhodecode:templates/data_table/_dt_elements.mako') |
|
701 | 702 | |
|
702 | 703 | pull_requests = PullRequestModel().get_im_participating_in( |
|
703 | 704 | user_id=self._rhodecode_user.user_id, |
|
704 | 705 | statuses=statuses, |
|
705 | 706 | offset=start, length=limit, order_by=order_by, |
|
706 | 707 | order_dir=order_dir) |
|
707 | 708 | |
|
708 | 709 | pull_requests_total_count = PullRequestModel().count_im_participating_in( |
|
709 | 710 | user_id=self._rhodecode_user.user_id, statuses=statuses) |
|
710 | 711 | |
|
711 | 712 | data = [] |
|
712 | 713 | comments_model = CommentsModel() |
|
713 | 714 | for pr in pull_requests: |
|
714 | 715 | repo_id = pr.target_repo_id |
|
715 | 716 | comments = comments_model.get_all_comments( |
|
716 | 717 | repo_id, pull_request=pr) |
|
717 | 718 | owned = pr.user_id == self._rhodecode_user.user_id |
|
718 | 719 | |
|
719 | 720 | data.append({ |
|
720 | 721 | 'target_repo': _render('pullrequest_target_repo', |
|
721 | 722 | pr.target_repo.repo_name), |
|
722 | 723 | 'name': _render('pullrequest_name', |
|
723 | 724 | pr.pull_request_id, pr.pull_request_state, |
|
724 | 725 | pr.work_in_progress, pr.target_repo.repo_name, |
|
725 | 726 | short=True), |
|
726 | 727 | 'name_raw': pr.pull_request_id, |
|
727 | 728 | 'status': _render('pullrequest_status', |
|
728 | 729 | pr.calculated_review_status()), |
|
729 | 730 | 'title': _render('pullrequest_title', pr.title, pr.description), |
|
730 | 731 | 'description': h.escape(pr.description), |
|
731 | 732 | 'updated_on': _render('pullrequest_updated_on', |
|
732 | 733 | h.datetime_to_time(pr.updated_on)), |
|
733 | 734 | 'updated_on_raw': h.datetime_to_time(pr.updated_on), |
|
734 | 735 | 'created_on': _render('pullrequest_updated_on', |
|
735 | 736 | h.datetime_to_time(pr.created_on)), |
|
736 | 737 | 'created_on_raw': h.datetime_to_time(pr.created_on), |
|
737 | 738 | 'state': pr.pull_request_state, |
|
738 | 739 | 'author': _render('pullrequest_author', |
|
739 | 740 | pr.author.full_contact, ), |
|
740 | 741 | 'author_raw': pr.author.full_name, |
|
741 | 742 | 'comments': _render('pullrequest_comments', len(comments)), |
|
742 | 743 | 'comments_raw': len(comments), |
|
743 | 744 | 'closed': pr.is_closed(), |
|
744 | 745 | 'owned': owned |
|
745 | 746 | }) |
|
746 | 747 | |
|
747 | 748 | # json used to render the grid |
|
748 | 749 | data = ({ |
|
749 | 750 | 'draw': draw, |
|
750 | 751 | 'data': data, |
|
751 | 752 | 'recordsTotal': pull_requests_total_count, |
|
752 | 753 | 'recordsFiltered': pull_requests_total_count, |
|
753 | 754 | }) |
|
754 | 755 | return data |
|
755 | 756 | |
|
756 | 757 | @LoginRequired() |
|
757 | 758 | @NotAnonymous() |
|
758 | 759 | @view_config( |
|
759 | 760 | route_name='my_account_pullrequests', |
|
760 | 761 | request_method='GET', |
|
761 | 762 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
762 | 763 | def my_account_pullrequests(self): |
|
763 | 764 | c = self.load_default_context() |
|
764 | 765 | c.active = 'pullrequests' |
|
765 | 766 | req_get = self.request.GET |
|
766 | 767 | |
|
767 | 768 | c.closed = str2bool(req_get.get('pr_show_closed')) |
|
768 | 769 | |
|
769 | 770 | return self._get_template_context(c) |
|
770 | 771 | |
|
771 | 772 | @LoginRequired() |
|
772 | 773 | @NotAnonymous() |
|
773 | 774 | @view_config( |
|
774 | 775 | route_name='my_account_pullrequests_data', |
|
775 | 776 | request_method='GET', renderer='json_ext') |
|
776 | 777 | def my_account_pullrequests_data(self): |
|
777 | 778 | self.load_default_context() |
|
778 | 779 | req_get = self.request.GET |
|
779 | 780 | closed = str2bool(req_get.get('closed')) |
|
780 | 781 | |
|
781 | 782 | statuses = [PullRequest.STATUS_NEW, PullRequest.STATUS_OPEN] |
|
782 | 783 | if closed: |
|
783 | 784 | statuses += [PullRequest.STATUS_CLOSED] |
|
784 | 785 | |
|
785 | 786 | data = self._get_pull_requests_list(statuses=statuses) |
|
786 | 787 | return data |
|
787 | 788 | |
|
788 | 789 | @LoginRequired() |
|
789 | 790 | @NotAnonymous() |
|
790 | 791 | @view_config( |
|
791 | 792 | route_name='my_account_user_group_membership', |
|
792 | 793 | request_method='GET', |
|
793 | 794 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
794 | 795 | def my_account_user_group_membership(self): |
|
795 | 796 | c = self.load_default_context() |
|
796 | 797 | c.active = 'user_group_membership' |
|
797 | 798 | groups = [UserGroupModel.get_user_groups_as_dict(group.users_group) |
|
798 | 799 | for group in self._rhodecode_db_user.group_member] |
|
799 | 800 | c.user_groups = json.dumps(groups) |
|
800 | 801 | return self._get_template_context(c) |
@@ -1,156 +1,159 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | |
|
23 | 23 | from pyramid.httpexceptions import HTTPFound |
|
24 | 24 | from pyramid.view import view_config |
|
25 | 25 | |
|
26 | 26 | from rhodecode.apps._base import BaseAppView, DataGridAppView |
|
27 | 27 | from rhodecode.apps.ssh_support import SshKeyFileChangeEvent |
|
28 | 28 | from rhodecode.events import trigger |
|
29 | 29 | from rhodecode.lib import helpers as h |
|
30 | 30 | from rhodecode.lib import audit_logger |
|
31 | 31 | from rhodecode.lib.auth import LoginRequired, NotAnonymous, CSRFRequired |
|
32 | 32 | from rhodecode.model.db import IntegrityError, UserSshKeys |
|
33 | 33 | from rhodecode.model.meta import Session |
|
34 | 34 | from rhodecode.model.ssh_key import SshKeyModel |
|
35 | 35 | |
|
36 | 36 | log = logging.getLogger(__name__) |
|
37 | 37 | |
|
38 | 38 | |
|
39 | 39 | class MyAccountSshKeysView(BaseAppView, DataGridAppView): |
|
40 | 40 | |
|
41 | 41 | def load_default_context(self): |
|
42 | 42 | c = self._get_local_tmpl_context() |
|
43 | 43 | c.user = c.auth_user.get_instance() |
|
44 | 44 | |
|
45 | 45 | c.ssh_enabled = self.request.registry.settings.get( |
|
46 | 46 | 'ssh.generate_authorized_keyfile') |
|
47 | 47 | |
|
48 | 48 | return c |
|
49 | 49 | |
|
50 | 50 | @LoginRequired() |
|
51 | 51 | @NotAnonymous() |
|
52 | 52 | @view_config( |
|
53 | 53 | route_name='my_account_ssh_keys', request_method='GET', |
|
54 | 54 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
55 | 55 | def my_account_ssh_keys(self): |
|
56 | 56 | _ = self.request.translate |
|
57 | 57 | |
|
58 | 58 | c = self.load_default_context() |
|
59 | 59 | c.active = 'ssh_keys' |
|
60 | 60 | c.default_key = self.request.GET.get('default_key') |
|
61 | 61 | c.user_ssh_keys = SshKeyModel().get_ssh_keys(c.user.user_id) |
|
62 | 62 | return self._get_template_context(c) |
|
63 | 63 | |
|
64 | 64 | @LoginRequired() |
|
65 | 65 | @NotAnonymous() |
|
66 | 66 | @view_config( |
|
67 | 67 | route_name='my_account_ssh_keys_generate', request_method='GET', |
|
68 | 68 | renderer='rhodecode:templates/admin/my_account/my_account.mako') |
|
69 | 69 | def ssh_keys_generate_keypair(self): |
|
70 | 70 | _ = self.request.translate |
|
71 | 71 | c = self.load_default_context() |
|
72 | 72 | |
|
73 | 73 | c.active = 'ssh_keys_generate' |
|
74 | 74 | if c.ssh_key_generator_enabled: |
|
75 | private_format = self.request.GET.get('private_format') \ | |
|
76 | or SshKeyModel.DEFAULT_PRIVATE_KEY_FORMAT | |
|
75 | 77 | comment = 'RhodeCode-SSH {}'.format(c.user.email or '') |
|
76 |
c.private, c.public = SshKeyModel().generate_keypair( |
|
|
78 | c.private, c.public = SshKeyModel().generate_keypair( | |
|
79 | comment=comment, private_format=private_format) | |
|
77 | 80 | c.target_form_url = h.route_path( |
|
78 | 81 | 'my_account_ssh_keys', _query=dict(default_key=c.public)) |
|
79 | 82 | return self._get_template_context(c) |
|
80 | 83 | |
|
81 | 84 | @LoginRequired() |
|
82 | 85 | @NotAnonymous() |
|
83 | 86 | @CSRFRequired() |
|
84 | 87 | @view_config( |
|
85 | 88 | route_name='my_account_ssh_keys_add', request_method='POST',) |
|
86 | 89 | def my_account_ssh_keys_add(self): |
|
87 | 90 | _ = self.request.translate |
|
88 | 91 | c = self.load_default_context() |
|
89 | 92 | |
|
90 | 93 | user_data = c.user.get_api_data() |
|
91 | 94 | key_data = self.request.POST.get('key_data') |
|
92 | 95 | description = self.request.POST.get('description') |
|
93 | 96 | fingerprint = 'unknown' |
|
94 | 97 | try: |
|
95 | 98 | if not key_data: |
|
96 | 99 | raise ValueError('Please add a valid public key') |
|
97 | 100 | |
|
98 | 101 | key = SshKeyModel().parse_key(key_data.strip()) |
|
99 | 102 | fingerprint = key.hash_md5() |
|
100 | 103 | |
|
101 | 104 | ssh_key = SshKeyModel().create( |
|
102 | 105 | c.user.user_id, fingerprint, key.keydata, description) |
|
103 | 106 | ssh_key_data = ssh_key.get_api_data() |
|
104 | 107 | |
|
105 | 108 | audit_logger.store_web( |
|
106 | 109 | 'user.edit.ssh_key.add', action_data={ |
|
107 | 110 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
108 | 111 | user=self._rhodecode_user, ) |
|
109 | 112 | Session().commit() |
|
110 | 113 | |
|
111 | 114 | # Trigger an event on change of keys. |
|
112 | 115 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
113 | 116 | |
|
114 | 117 | h.flash(_("Ssh Key successfully created"), category='success') |
|
115 | 118 | |
|
116 | 119 | except IntegrityError: |
|
117 | 120 | log.exception("Exception during ssh key saving") |
|
118 | 121 | err = 'Such key with fingerprint `{}` already exists, ' \ |
|
119 | 122 | 'please use a different one'.format(fingerprint) |
|
120 | 123 | h.flash(_('An error occurred during ssh key saving: {}').format(err), |
|
121 | 124 | category='error') |
|
122 | 125 | except Exception as e: |
|
123 | 126 | log.exception("Exception during ssh key saving") |
|
124 | 127 | h.flash(_('An error occurred during ssh key saving: {}').format(e), |
|
125 | 128 | category='error') |
|
126 | 129 | |
|
127 | 130 | return HTTPFound(h.route_path('my_account_ssh_keys')) |
|
128 | 131 | |
|
129 | 132 | @LoginRequired() |
|
130 | 133 | @NotAnonymous() |
|
131 | 134 | @CSRFRequired() |
|
132 | 135 | @view_config( |
|
133 | 136 | route_name='my_account_ssh_keys_delete', request_method='POST') |
|
134 | 137 | def my_account_ssh_keys_delete(self): |
|
135 | 138 | _ = self.request.translate |
|
136 | 139 | c = self.load_default_context() |
|
137 | 140 | |
|
138 | 141 | user_data = c.user.get_api_data() |
|
139 | 142 | |
|
140 | 143 | del_ssh_key = self.request.POST.get('del_ssh_key') |
|
141 | 144 | |
|
142 | 145 | if del_ssh_key: |
|
143 | 146 | ssh_key = UserSshKeys.get_or_404(del_ssh_key) |
|
144 | 147 | ssh_key_data = ssh_key.get_api_data() |
|
145 | 148 | |
|
146 | 149 | SshKeyModel().delete(del_ssh_key, c.user.user_id) |
|
147 | 150 | audit_logger.store_web( |
|
148 | 151 | 'user.edit.ssh_key.delete', action_data={ |
|
149 | 152 | 'data': {'ssh_key': ssh_key_data, 'user': user_data}}, |
|
150 | 153 | user=self._rhodecode_user,) |
|
151 | 154 | Session().commit() |
|
152 | 155 | # Trigger an event on change of keys. |
|
153 | 156 | trigger(SshKeyFileChangeEvent(), self.request.registry) |
|
154 | 157 | h.flash(_("Ssh key successfully deleted"), category='success') |
|
155 | 158 | |
|
156 | 159 | return HTTPFound(h.route_path('my_account_ssh_keys')) |
@@ -1,103 +1,110 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | |
|
23 | 23 | from pyramid.view import view_config |
|
24 | 24 | from pyramid.httpexceptions import HTTPFound |
|
25 | 25 | |
|
26 | 26 | from rhodecode.apps._base import RepoGroupAppView |
|
27 | 27 | from rhodecode.lib import helpers as h |
|
28 | 28 | from rhodecode.lib import audit_logger |
|
29 | 29 | from rhodecode.lib.auth import ( |
|
30 | 30 | LoginRequired, HasRepoGroupPermissionAnyDecorator, CSRFRequired) |
|
31 | from rhodecode.model.db import User | |
|
31 | 32 | from rhodecode.model.permission import PermissionModel |
|
32 | 33 | from rhodecode.model.repo_group import RepoGroupModel |
|
33 | 34 | from rhodecode.model.forms import RepoGroupPermsForm |
|
34 | 35 | from rhodecode.model.meta import Session |
|
35 | 36 | |
|
36 | 37 | log = logging.getLogger(__name__) |
|
37 | 38 | |
|
38 | 39 | |
|
39 | 40 | class RepoGroupPermissionsView(RepoGroupAppView): |
|
40 | 41 | def load_default_context(self): |
|
41 | 42 | c = self._get_local_tmpl_context() |
|
42 | 43 | |
|
43 | 44 | return c |
|
44 | 45 | |
|
45 | 46 | @LoginRequired() |
|
46 | 47 | @HasRepoGroupPermissionAnyDecorator('group.admin') |
|
47 | 48 | @view_config( |
|
48 | 49 | route_name='edit_repo_group_perms', request_method='GET', |
|
49 | 50 | renderer='rhodecode:templates/admin/repo_groups/repo_group_edit.mako') |
|
50 | 51 | def edit_repo_group_permissions(self): |
|
51 | 52 | c = self.load_default_context() |
|
52 | 53 | c.active = 'permissions' |
|
53 | 54 | c.repo_group = self.db_repo_group |
|
54 | 55 | return self._get_template_context(c) |
|
55 | 56 | |
|
56 | 57 | @LoginRequired() |
|
57 | 58 | @HasRepoGroupPermissionAnyDecorator('group.admin') |
|
58 | 59 | @CSRFRequired() |
|
59 | 60 | @view_config( |
|
60 | 61 | route_name='edit_repo_group_perms_update', request_method='POST', |
|
61 | 62 | renderer='rhodecode:templates/admin/repo_groups/repo_group_edit.mako') |
|
62 | 63 | def edit_repo_groups_permissions_update(self): |
|
63 | 64 | _ = self.request.translate |
|
64 | 65 | c = self.load_default_context() |
|
65 | 66 | c.active = 'perms' |
|
66 | 67 | c.repo_group = self.db_repo_group |
|
67 | 68 | |
|
68 | 69 | valid_recursive_choices = ['none', 'repos', 'groups', 'all'] |
|
69 | 70 | form = RepoGroupPermsForm(self.request.translate, valid_recursive_choices)()\ |
|
70 | 71 | .to_python(self.request.POST) |
|
71 | 72 | |
|
72 | 73 | if not c.rhodecode_user.is_admin: |
|
73 | 74 | if self._revoke_perms_on_yourself(form): |
|
74 | 75 | msg = _('Cannot change permission for yourself as admin') |
|
75 | 76 | h.flash(msg, category='warning') |
|
76 | 77 | raise HTTPFound( |
|
77 | 78 | h.route_path('edit_repo_group_perms', |
|
78 | 79 | repo_group_name=self.db_repo_group_name)) |
|
79 | 80 | |
|
80 | 81 | # iterate over all members(if in recursive mode) of this groups and |
|
81 | 82 | # set the permissions ! |
|
82 | 83 | # this can be potentially heavy operation |
|
83 | 84 | changes = RepoGroupModel().update_permissions( |
|
84 | 85 | c.repo_group, |
|
85 | 86 | form['perm_additions'], form['perm_updates'], form['perm_deletions'], |
|
86 | 87 | form['recursive']) |
|
87 | 88 | |
|
88 | 89 | action_data = { |
|
89 | 90 | 'added': changes['added'], |
|
90 | 91 | 'updated': changes['updated'], |
|
91 | 92 | 'deleted': changes['deleted'], |
|
92 | 93 | } |
|
93 | 94 | audit_logger.store_web( |
|
94 | 95 | 'repo_group.edit.permissions', action_data=action_data, |
|
95 | 96 | user=c.rhodecode_user) |
|
96 | 97 | |
|
97 | 98 | Session().commit() |
|
98 | 99 | h.flash(_('Repository Group permissions updated'), category='success') |
|
99 | PermissionModel().flush_user_permission_caches(changes) | |
|
100 | ||
|
101 | affected_user_ids = None | |
|
102 | if changes.get('default_user_changed', False): | |
|
103 | # if we change the default user, we need to flush everyone permissions | |
|
104 | affected_user_ids = User.get_all_user_ids() | |
|
105 | PermissionModel().flush_user_permission_caches( | |
|
106 | changes, affected_user_ids=affected_user_ids) | |
|
100 | 107 | |
|
101 | 108 | raise HTTPFound( |
|
102 | 109 | h.route_path('edit_repo_group_perms', |
|
103 | 110 | repo_group_name=self.db_repo_group_name)) |
@@ -1,122 +1,135 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2011-2019 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import logging |
|
22 | 22 | |
|
23 | 23 | from pyramid.httpexceptions import HTTPFound |
|
24 | 24 | from pyramid.view import view_config |
|
25 | 25 | |
|
26 | 26 | from rhodecode.apps._base import RepoAppView |
|
27 | 27 | from rhodecode.lib import helpers as h |
|
28 | 28 | from rhodecode.lib import audit_logger |
|
29 | 29 | from rhodecode.lib.auth import ( |
|
30 | 30 | LoginRequired, HasRepoPermissionAnyDecorator, CSRFRequired) |
|
31 | from rhodecode.lib.utils2 import str2bool | |
|
32 | from rhodecode.model.db import User | |
|
31 | 33 | from rhodecode.model.forms import RepoPermsForm |
|
32 | 34 | from rhodecode.model.meta import Session |
|
33 | 35 | from rhodecode.model.permission import PermissionModel |
|
34 | 36 | from rhodecode.model.repo import RepoModel |
|
35 | 37 | |
|
36 | 38 | log = logging.getLogger(__name__) |
|
37 | 39 | |
|
38 | 40 | |
|
39 | 41 | class RepoSettingsPermissionsView(RepoAppView): |
|
40 | 42 | |
|
41 | 43 | def load_default_context(self): |
|
42 | 44 | c = self._get_local_tmpl_context() |
|
43 | 45 | return c |
|
44 | 46 | |
|
45 | 47 | @LoginRequired() |
|
46 | 48 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
47 | 49 | @view_config( |
|
48 | 50 | route_name='edit_repo_perms', request_method='GET', |
|
49 | 51 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
50 | 52 | def edit_permissions(self): |
|
51 | 53 | _ = self.request.translate |
|
52 | 54 | c = self.load_default_context() |
|
53 | 55 | c.active = 'permissions' |
|
54 | 56 | if self.request.GET.get('branch_permissions'): |
|
55 | 57 | h.flash(_('Explicitly add user or user group with write+ ' |
|
56 | 58 | 'permission to modify their branch permissions.'), |
|
57 | 59 | category='notice') |
|
58 | 60 | return self._get_template_context(c) |
|
59 | 61 | |
|
60 | 62 | @LoginRequired() |
|
61 | 63 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
62 | 64 | @CSRFRequired() |
|
63 | 65 | @view_config( |
|
64 | 66 | route_name='edit_repo_perms', request_method='POST', |
|
65 | 67 | renderer='rhodecode:templates/admin/repos/repo_edit.mako') |
|
66 | 68 | def edit_permissions_update(self): |
|
67 | 69 | _ = self.request.translate |
|
68 | 70 | c = self.load_default_context() |
|
69 | 71 | c.active = 'permissions' |
|
70 | 72 | data = self.request.POST |
|
71 | 73 | # store private flag outside of HTML to verify if we can modify |
|
72 | 74 | # default user permissions, prevents submission of FAKE post data |
|
73 | 75 | # into the form for private repos |
|
74 | 76 | data['repo_private'] = self.db_repo.private |
|
75 | 77 | form = RepoPermsForm(self.request.translate)().to_python(data) |
|
76 | 78 | changes = RepoModel().update_permissions( |
|
77 | 79 | self.db_repo_name, form['perm_additions'], form['perm_updates'], |
|
78 | 80 | form['perm_deletions']) |
|
79 | 81 | |
|
80 | 82 | action_data = { |
|
81 | 83 | 'added': changes['added'], |
|
82 | 84 | 'updated': changes['updated'], |
|
83 | 85 | 'deleted': changes['deleted'], |
|
84 | 86 | } |
|
85 | 87 | audit_logger.store_web( |
|
86 | 88 | 'repo.edit.permissions', action_data=action_data, |
|
87 | 89 | user=self._rhodecode_user, repo=self.db_repo) |
|
88 | 90 | |
|
89 | 91 | Session().commit() |
|
90 | 92 | h.flash(_('Repository access permissions updated'), category='success') |
|
91 | 93 | |
|
92 | PermissionModel().flush_user_permission_caches(changes) | |
|
94 | affected_user_ids = None | |
|
95 | if changes.get('default_user_changed', False): | |
|
96 | # if we change the default user, we need to flush everyone permissions | |
|
97 | affected_user_ids = User.get_all_user_ids() | |
|
98 | PermissionModel().flush_user_permission_caches( | |
|
99 | changes, affected_user_ids=affected_user_ids) | |
|
93 | 100 | |
|
94 | 101 | raise HTTPFound( |
|
95 | 102 | h.route_path('edit_repo_perms', repo_name=self.db_repo_name)) |
|
96 | 103 | |
|
97 | 104 | @LoginRequired() |
|
98 | 105 | @HasRepoPermissionAnyDecorator('repository.admin') |
|
99 | 106 | @CSRFRequired() |
|
100 | 107 | @view_config( |
|
101 | 108 | route_name='edit_repo_perms_set_private', request_method='POST', |
|
102 | 109 | renderer='json_ext') |
|
103 | 110 | def edit_permissions_set_private_repo(self): |
|
104 | 111 | _ = self.request.translate |
|
105 | 112 | self.load_default_context() |
|
106 | 113 | |
|
114 | private_flag = str2bool(self.request.POST.get('private')) | |
|
115 | ||
|
107 | 116 | try: |
|
108 | 117 | RepoModel().update( |
|
109 |
self.db_repo, **{'repo_private': |
|
|
118 | self.db_repo, **{'repo_private': private_flag, 'repo_name': self.db_repo_name}) | |
|
110 | 119 | Session().commit() |
|
111 | 120 | |
|
112 | 121 | h.flash(_('Repository `{}` private mode set successfully').format(self.db_repo_name), |
|
113 | 122 | category='success') |
|
114 | 123 | except Exception: |
|
115 | 124 | log.exception("Exception during update of repository") |
|
116 | 125 | h.flash(_('Error occurred during update of repository {}').format( |
|
117 | 126 | self.db_repo_name), category='error') |
|
118 | 127 | |
|
128 | # NOTE(dan): we change repo private mode we need to notify all USERS | |
|
129 | affected_user_ids = User.get_all_user_ids() | |
|
130 | PermissionModel().trigger_permission_flush(affected_user_ids) | |
|
131 | ||
|
119 | 132 | return { |
|
120 | 133 | 'redirect_url': h.route_path('edit_repo_perms', repo_name=self.db_repo_name), |
|
121 |
'private': |
|
|
134 | 'private': private_flag | |
|
122 | 135 | } |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
|
1 | NO CONTENT: modified file | |
The requested commit or file is too big and content was truncated. Show full diff |
General Comments 0
You need to be logged in to leave comments.
Login now